CodeFlowActivity - aceryan-consulting/aceryansoft.codeflow GitHub Wiki

We are writing a library using artificial intelligence to predict customer behaviors (loyalty, spending, etc ...).

It is obvious at this stage that our prediction algorithm need data.

First we create some model to store data

public enum Gender
{
	Male, 
	Female
}

public class Customer
{
	public string CustomerId { get; set; }
	public Gender Gender { get; set; }
	public int Age { get; set; } 
	public decimal AnnualIncome { get; set; } 
	public int SpendingScore { get; set; } 
}

public class Order
{
	public string CustomerId { get; set; }
	public string OrderId { get; set; } 
	public string Category { get; set; } 
	public int Quantity { get; set; }
	public decimal UnitPrice { get; set; } 
	public decimal Cost { get; set; } 
} 

Then we create our first codeflow activity to load Customer data

A codeflow Activity is a class implementing ICodeFlowActivity.

public class LoadCustomerActivity : ICodeFlowActivity
{
	private readonly ICustomerAndOrderService _customerAndOrderService;

	public LoadCustomerActivity(ICustomerAndOrderService customerAndOrderService)
	{
		_customerAndOrderService = customerAndOrderService;
	}
	public IExecutionContext Execute(ICodeFlowContext context, params object[] inputs)
	{
		var customers = _customerAndOrderService.GetCustomers();
		context.SetCollection<Customer>("Customers", customers);
		return new ExecutionContext() { ActivityName = "LoadCustomerActivity", Status = Status.Succeeded };
	}
}

// service definition 
public interface ICustomerAndOrderService
{
	List<Customer> GetCustomers();
	List<Order> GetCustomerOrders(string customerId);
}

public class SampleCustomerAndOrderService : ICustomerAndOrderService
{
	public List<Order> GetCustomerOrders(string customerId)
	{
            // we keep it simple, we are not learning how to load data here :)  
		return new List<Order>(); 
	}

	public List<Customer> GetCustomers()
	{
		return new List<Customer>();
	}
} 

Finally we can use our first activity to load data

 var exceptionHandlingMiddleware = new ExceptionHandlingMiddleware();
var logActivityFilter = new LogActivityFilter();

var custAndOrderService = new SampleCustomerAndOrderService();
var loadCustomerActivity = new LoadCustomerActivity(custAndOrderService);

var aicodeFlow = new CodeFlow();
aicodeFlow.StartNew(cfg => {
	cfg.WithContext(() => new AiContext())
	.UseMiddleware(exceptionHandlingMiddleware)
	.UseActivityFilter(logActivityFilter);
})
.Do(loadCustomerActivity) 
.Close();

aicodeFlow.Execute();

Continue reading: Dependency injection

⚠️ **GitHub.com Fallback** ⚠️