CalcEngine
is designed to implement and execute any type of calculator.
By building the library based on the open / close principle, it is possible to add new calculators without changing any existing code.
It is useful when different solutions are available for calculating a parameter based on different inputs.
see Industrial Electricity Calculators for samples.
You should install CalcEngine with NuGet:
Install-Package Arax.CalcEngine
Or via the .NET Core command line interface:
dotnet add package Arax.CalcEngine
//add engine and calculators in current assembly
builder.Services.AddCalculator();
//to add calculators from another assemplies
builder.Services.InstallCalculators(assembly1,assembly2)
//first define parameter with IParam<TResult>
public record SumParam(double Left, double Right) : IParam<double>;
//define calculator
public class SumCalculator:ICalculator<SumParam,double>
{
public async Task<double> Calc(SumParam param,CancellationToken cancellationToken)
{
//do calculation
//return result
}
}
//or for calculators that calculate synchronously
public class SumCalculator:SyncCalculator<SumParam,double>
{
protected override double Calc(SumParam param)
{
// calculation logic
// return result
}
}
[ApiController]
[Route("[controller]")]
public class CalculatorController : ControllerBase
{
private readonly ICalcEngine _calcEngine;
public CalculatorController(ICalcEngine calcEngine)
{
_calcEngine = calcEngine;
}
[HttpGet(Name = "GetSum")]
public async Task<double> Get(double left,double right)
{
var param=new SumParam(left, right);
var result = await _calcEngine.Calc(param);
return result;
}
}