EnumFactory 1.2.0
See the version list below for details.
dotnet add package EnumFactory --version 1.2.0
NuGet\Install-Package EnumFactory -Version 1.2.0
<PackageReference Include="EnumFactory" Version="1.2.0" />
paket add EnumFactory --version 1.2.0
#r "nuget: EnumFactory, 1.2.0"
// Install EnumFactory as a Cake Addin #addin nuget:?package=EnumFactory&version=1.2.0 // Install EnumFactory as a Cake Tool #tool nuget:?package=EnumFactory&version=1.2.0
EnumFactory
Simple Factory Pattern, naming convention based, with 1-to-1 mapping between custom enum values and named instances. Variant classes being instantiated by factory can remain clean: all you need is to follow the naming convention. Reduces boilerplate factory implementation code to one-liners. Improves open/closed principle, by closing off the factory DI registration code: it stays the same forever, even when new variant classes and enumeration values are added along the way. Supports scoped, transient and singleton lifecycles for factory and variant classes. Variant classes can implement a common interface or inherit from common (abstract) class.
QuickStart example
- add any enumeration describing the variant classes that factory should pick up:
public enum OrderType
{
LocalOrder,
AmazonOrder
}
- add interface I(Some)Suffix and implementations with class names following the format (EnumValue)Suffix. Suffix can be anything (Service, Manager, Reader etc.) as long as it's unique for the interface and all implementations.
public interface IOrderService
{
Task UpdateOrder(Order order);
}
public class LocalOrderService : IOrderService
{
public Task UpdateOrder(Order order) { }
}
public class AmazonOrderService : IOrderService
{
public Task UpdateOrder(Order order) { }
}
- use IServiceCollection extension method to register IEnumFactory<OrderType, IOrderService>:
services.AddEnumFactory<OrderType, IOrderService>();
- default lifecycle is Scoped, but you can chose other:
services.AddEnumFactory<OrderType, IOrderService>(Lifecycle.Singleton);
- inject factory where needed and enjoy IOrderService services! It's that simple 😃
public class OrderController : Controller
{
private readonly IEnumFactory<OrderType, IOrderService> _factory;
public OrderController(IEnumFactory<OrderType, IOrderService> factory)
{
_factory = factory;
}
[HttpPost]
public async Task UpdateOrder([FromBody]Order order)
{
//if order.OrderType is OrderType.Local, you will get LocalOrderService instance.
var service = _factory.GetService(order.OrderType);
service.UpdateOrder(order);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net5.0
- Microsoft.Extensions.DependencyInjection (>= 5.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
QuickStart example (order types and services):
- add any enumeration, e.g. OrderType { LocalOrder, AmazonOrder }
- add interface IOrderService and implementations: LocalOrderService, AmazonOrderService
- use extension method services.AddEnumFactory<OrderType, IOrderService>()
- inject IEnumFactory<OrderType, IOrderService>
- call _factory.GetService(OrderType.Local)
- enjoy LocalOrderService services! It's that simple :)