equadrat.Framework.Math.Uom (8.3.6)
Installation
dotnet nuget add source --name equadrat-oss --username your_username --password your_token dotnet add package --source equadrat-oss --version 8.3.6 equadrat.Framework.Math.UomAbout this package
A framework to extend the Microsoft .net framework with extra math functionality.
equadrat Framework Math
The idea of the equadrat Framework is to provide APIs to build applications with an architecture of highly decoupled components. Every single component, aspect or pattern is represented by interfaces. The framework provides a standard implementation for these but each type can be replaced by a custom implementation.
The framework is not about reimplementing components, but making them available using common interfaces.
Getting started
You can use most of the components by simply instantiating them. This is maybe the easiest way to evaluate the framework.
If you're using an IOC/DI container such as Microsoft Extensions DependencyInjection or if you want to use the built-in IOC framework, you can use the bootstrapper and bootstrapper modules to register the framework to the IOC.
Usage
Register to IOC/DI container
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
// Use the module registry to register bootstrapper modules. Usually each assembly has its own bootstrapper module.
// This will add their service declarations to the IServiceCollection.
// This requires equadrat.Framework.Core.Microsoft.Extensions.Hosting NuGet package.
builder.Services.RegisterBootstrapperModules(moduleRegistry => moduleRegistry.MathFramework().RegisterUomModule());
// Declare other services as usual.
// builder.Services.AddSingleton<...>(...);
var app = builder.Build();
// Optional: initialize all services declared by the registered bootstrapper modules. Calling this method is required for all environments which doesn't support IHostedService, such es browsers/webassembly.
app.Services.InitBootstrapperModules();
app.Run();
}
Construct UOMs
IServiceProvider serviceProvider = ...;
var unitFactory = serviceProvider.GetRequiredService<IMathUnitFactory>().GetGeneric<eMathScalePrefix, int>();
var uomFactory = serviceProvider.GetRequiredService<IMathUomFactory>().GetGeneric<decimal, eMathScalePrefix, int>();
var conversionPathRegistry = serviceProvider.GetRequiredService<IMathUomConversionPathRegistry>().GetGeneric<decimal, eMathScalePrefix, int>();
// Construct the units.
using (unitFactory.New(MathUnitSymbols.Length.Meter).Deconstruct(out var meterUnit))
using (unitFactory.New(MathUnitSymbols.Length.InchI).Deconstruct(out var inchUnit))
{
// Register the conversion from inch to meter, the reverse conversion will be derived automatically.
conversionPathRegistry.RegisterConversionPath(inchUnit, meterUnit, x => x.MultiplyBy(2.54m, scalarScale:eMathScalePrefix.Centi));
using (var valueA = uomFactory.New(0.5m, meterUnit)) // Create value A in meter.
using (var valueB = uomFactory.New(10m, inchUnit)) // Create value B in inch.
using (var sum = valueA.Add(valueB)) // Sum = A + B
{
Assert.AreEqual(0.754m, sum.Quantity);
Assert.AreEqual(75.4m, sum.Value);
Assert.AreEqual(eMathScalePrefix.Centi, sum.Scale);
Assert.AreEqual(meterUnit, sum.Unit);
}
}
Convert UOMs into other units
IServiceProvider serviceProvider = ...;
var unitFactory = serviceProvider.GetRequiredService<IMathUnitFactory>().GetGeneric<eMathScalePrefix, int>();
var uomFactory = serviceProvider.GetRequiredService<IMathUomFactory>().GetGeneric<decimal, eMathScalePrefix, int>();
var conversionPathRegistry = serviceProvider.GetRequiredService<IMathUomConversionPathRegistry>().GetGeneric<decimal, eMathScalePrefix, int>();
// Construct the units.
using (unitFactory.New(MathUnitSymbols.Length.Meter).Deconstruct(out var meterUnit))
using (unitFactory.New(MathUnitSymbols.Length.InchI).Deconstruct(out var inchUnit))
{
// Register the conversion from inch to meter, the reverse conversion will be derived automatically.
conversionPathRegistry.RegisterConversionPath(inchUnit, meterUnit, x => x.MultiplyBy(2.54m, scalarScale: eMathScalePrefix.Centi));
// Convert meter to inch.
using (var value = uomFactory.New(0.254m, meterUnit))
using (var result = value.Convert(inchUnit))
{
Assert.AreEqual(10m, result.Quantity);
Assert.AreEqual(inchUnit, result.Unit);
}
// Convert inch to meter.
using (var value = uomFactory.New(10m, inchUnit))
using (var result = value.Convert(meterUnit))
{
Assert.AreEqual(0.254m, result.Quantity);
Assert.AreEqual(meterUnit, result.Unit);
}
}
Using a UOM converter
IServiceProvider serviceProvider = ...;
var unitFactory = serviceProvider.GetRequiredService<IMathUnitFactory>().GetGeneric<eMathScalePrefix, int>();
var uomFactory = serviceProvider.GetRequiredService<IMathUomFactory>().GetGeneric<decimal, eMathScalePrefix, int>();
var conversionPathRegistry = serviceProvider.GetRequiredService<IMathUomConversionPathRegistry>().GetGeneric<decimal, eMathScalePrefix, int>();
// Construct the units.
using (unitFactory.New(MathUnitSymbols.Length.Meter).Deconstruct(out var meterUnit))
using (unitFactory.New(MathUnitSymbols.Length.InchI).Deconstruct(out var inchUnit))
{
// Register the conversion from inch to meter, the reverse conversion will be derived automatically.
conversionPathRegistry.RegisterConversionPath(inchUnit, meterUnit, x => x.MultiplyBy(2.54m, scalarScale: eMathScalePrefix.Centi));
// Create a UOM converter which will convert to meter.
using (uomFactory.GetConverter(meterUnit).Deconstruct(out var uomConverter))
{
// Convert meter to meter.
using (var value = uomFactory.New(0.5m, meterUnit)) // Create a value in meter.
using (var result = uomConverter.Convert(value)) // Convert the value into meter.
{
Assert.AreEqual(0.5m, result.Quantity);
Assert.AreEqual(meterUnit, result.Unit);
}
// Convert inch to meter.
using (var value = uomFactory.New(10m, inchUnit)) // Create a value in inch.
using (var result = uomConverter.Convert(value)) // Convert the value into meter.
{
Assert.AreEqual(0.254m, result.Quantity);
Assert.AreEqual(meterUnit, result.Unit);
}
}
// Create a UOM converter which will convert to inch.
using (uomFactory.GetConverter(inchUnit).Deconstruct(out var uomConverter))
{
// Convert meter to meter.
using (var value = uomFactory.New(0.5m, inchUnit)) // Create a value in inch.
using (var result = uomConverter.Convert(value)) // Convert the value into inch.
{
Assert.AreEqual(0.5m, result.Quantity);
Assert.AreEqual(inchUnit, result.Unit);
}
// Convert inch to meter.
using (var value = uomFactory.New(0.254m, meterUnit)) // Create a value in meter.
using (var result = uomConverter.Convert(value)) // Convert the value into inch.
{
Assert.AreEqual(10m, result.Quantity);
Assert.AreEqual(inchUnit, result.Unit);
}
}
}
Have a look at the example applications on equadrat.net and in the wiki.
Compatibility
The plan is to keep the equadrat Framework compatible to these .net versions as long as possible.
| .net version | min | max | automated tests* |
|---|---|---|---|
| Core | 3.1 | ≥ 10.x | yes |
| Standard | 2.0 | ≥ 2.1 | no |
| Framework | 4.6.2 | ≥ 4.8.x | no |
*The automated tests run under Linux for .net Core 6.x and above.
Version scheme and breaking changes
| Version change | Scope |
|---|---|
| Major | Overhaul of the project |
| Minor | Some types have changed |
| Revision | Fully backward compatible |
| Build | Set in preview versions only |
Additional documentation
You can find some guides regarding specific features on my website: www.equadrat.net
Feedback
You can contact me on my website: www.equadrat.net
License
Please respect the license and check equadrat.Framework.Math.License.md before using the package.
Release notes
v8.3.6
- Adjusted code to work with limitations of Microsoft Dependency Injection.
- Adjusted behaviors of UOM operators when one or both members are empty.
v8.3.5
- Initial Release