EnumFactory 1.9.0

dotnet add package EnumFactory --version 1.9.0
NuGet\Install-Package EnumFactory -Version 1.9.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EnumFactory" Version="1.9.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnumFactory --version 1.9.0
#r "nuget: EnumFactory, 1.9.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EnumFactory as a Cake Addin
#addin nuget:?package=EnumFactory&version=1.9.0

// Install EnumFactory as a Cake Tool
#tool nuget:?package=EnumFactory&version=1.9.0

EnumFactory

  • Simple Factory Pattern, convention based, with 1-1 mapping between enum values and named instances.
  • Variant classes being instantiated by factory can remain clean: just follow the proper naming.
  • Reduces boilerplate code by having factory implementation and DI registration together in one-liner.
  • Adding new variant classes and enum values is pure extension (in line with open/closed principle).
  • DI friendly: injectable factory, no service-locator anti-pattern (in line with .NET DI).
  • Lifecycle friendly: scoped, transient and singleton lifecycles are options for all elements.
  • Variant classes can implement a common interface or inherit from common (abstract) class.
  • No reflection after the setup: types map is cached for each specific factory type.

QuickStart example

  • add any enumeration describing the variant classes that factory should pick up:
public enum OrderType 
{ 
	LocalOrder, 
	
	AmazonOrder 
} 
  • add the abstraction for objects that will be constructed by the factory, and some implementations. Abstraction can be interface or (abstract) class. in this case, it's the IOrderService interface and all it's implementations. Note: you can have as many implementations as you want, but there must be one for each enum value, as per naming convention (others are ignored).

  • mapped implementation class names must start with the related enum value and end with last word from abstraction name (case sensitive). In this example, for LocalOrder enum value and IOrderService interface we should have LocalOrderService class, and so on.

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 IFactory<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 IFactory<OrderType, IOrderService> _factory;

	public OrderController(IFactory<OrderType, IOrderService> factory)
	{
	    _factory = factory;
	}
	
	[HttpPut]
	public async Task UpdateOrder([FromBody]Order order)
	{
	    //for OrderType.LocalOrder, you'll get LocalOrderService.
	    var service = _factory.GetService(order.OrderType);
		
	    service.UpdateOrder(order); 
	}
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.9.0 528 4/13/2021
1.8.1 474 4/12/2021
1.8.0 502 4/12/2021
1.7.1 450 4/12/2021
1.7.0 483 4/12/2021
1.6.0 475 4/11/2021
1.5.0 460 4/11/2021
1.4.0 449 4/11/2021
1.3.0 490 4/11/2021
1.2.0 410 4/11/2021

- support for custom lifecycle of factory and variant classes added
- enum-types map is cashed per EnumFactory<,> closed generic type
- naming convention is made more flexible (refer the README.md)