EnumFactory 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EnumFactory --version 1.3.0                
NuGet\Install-Package EnumFactory -Version 1.3.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.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnumFactory --version 1.3.0                
#r "nuget: EnumFactory, 1.3.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.3.0

// Install EnumFactory as a Cake Tool
#tool nuget:?package=EnumFactory&version=1.3.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 enumeration values can be done seamlessly as pure extension.
  • 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 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 562 4/13/2021
1.8.1 499 4/12/2021
1.8.0 531 4/12/2021
1.7.1 472 4/12/2021
1.7.0 510 4/12/2021
1.6.0 500 4/11/2021
1.5.0 489 4/11/2021
1.4.0 476 4/11/2021
1.3.0 517 4/11/2021
1.2.0 433 4/11/2021

- support for custom lifecycle of factory and variant classes added