CallCodeFlow - aceryan-consulting/aceryansoft.codeflow GitHub Wiki
After loading our customers and orders data, the next step is to clean them by replacing missing values, removing incoherent data , etc ...
We can create for instance 3 activities to fill missing data with methods (Mean/Media, Most frequent, knn) and 1 activity to remove incoherent data. Our code now look like this.
public void Run()
{
var kernel = BuildDiContainer();
Func<Type, string, object> serviceResolver = (typ, str) => string.IsNullOrEmpty(str)
? kernel.Get(typ) : kernel.Get(typ, str);
var aicodeFlow = new CodeFlow();
aicodeFlow.StartNew(cfg => {
cfg.WithContext(() => new AiContext())
.UseMiddleware<IExceptionHandlingMiddleware>()
.UseActivityFilter<ILogActivityFilter>()
.WithServiceResolver(serviceResolver);
})
.Do<ILoadCustomerActivity>()
.Do<ILoadCustomerOrdersActivity>()
.Do<IFillMissingDataWithMeanMedianActivity>() // fill and clean logic
.Do<IFillMissingDataWithMostFrequentActivity>()
.Do<IFillMissingDataWithKNNActivity>()
.Do<IRemoveIncoherentDataActivity>()
.Close();
aicodeFlow.Execute();
}
Now let's assume that the data fill and clean logic is working like a charm and we realize that we need to reuse this section in multiples codeflow.
To share this codeflow section, we can wrap it in a function and use the CallCodeFlow syntax as shown bellow.
public void Run()
{
var kernel = BuildDiContainer();
Func<Type, string, object> serviceResolver = (typ, str) => string.IsNullOrEmpty(str)
? kernel.Get(typ) : kernel.Get(typ, str);
var aicodeFlow = new CodeFlow();
aicodeFlow.StartNew(cfg => {
cfg.WithContext(() => new AiContext())
.UseMiddleware<IExceptionHandlingMiddleware>()
.UseActivityFilter<ILogActivityFilter>()
.WithServiceResolver(serviceResolver);
})
.Do<ILoadCustomerActivity>()
.Do<ILoadCustomerOrdersActivity>()
.CallCodeFlow(FillAndCleanCustomerData)
.Close();
aicodeFlow.Execute();
}
private void FillAndCleanCustomerData(ICallFlowApi callBlock)
{
callBlock.Do<IFillMissingDataWithMeanMedianActivity>()
.Do<IFillMissingDataWithMostFrequentActivity>()
.Do<IFillMissingDataWithKNNActivity>()
.CallCodeFlow(CleanCustomerData); // just to show that CallCodeFlow syntax allow inner calls
}
private void CleanCustomerData(ICallFlowApi callBlock)
{
callBlock.Do<IRemoveIncoherentDataActivity>();
}
Continue reading: Function