Yet another IOC container for .NET, designed from the ground up to support the ASP.NET Core DI abstractions.
Because FactoryFactory implements Microsoft’s abstractions, you can use it as a drop-in replacement for the bare-bones Microsoft container with minimal effort. All you need to do is to add the FactoryFactory.AspNet.DependencyInjection to your project from NuGet:
Install-Package FactoryFactory.AspNet.DependencyInjection -Pre
Then add a call to .UseFactoryFactory() to your web host builder in
Program.cs:
using FactoryFactory.AspNet.DependencyInjection;
/* snip */
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseFactoryFactory() // <-- Add this line here
.UseStartup<Startup>();
For getting started, that is all you need to do. FactoryFactory will
automatically pick up on any services that you have registered in the usual way
in the ConfigureServices(IServiceCollection) method in Startup.cs.
If you want to use any of the advanced functionality that FactoryFactory offers,
such as decorators, interceptors, custom lifecycles or conditional injection,
you will need to add an extra method to your Startup.cs class to expose the
FactoryFactory API. To do this, add a method ConfigureContainer(Module module)
to the class as follows:
public void ConfigureContainer(Module module)
{
module.Define<IClock>().As<Clock>().Singleton();
module.Decorate<IClock>().With<TimeZoneDecorator>();
}
You can have both ConfigureServices and ConfigureContainer methods in your
Startup.cs class: you don’t have to choose between one and the other. If you
have both, FactoryFactory will call ConfigureServices(IServiceCollection)
first, then your new ConfigureContainer(Module) method second.
Project maintained by jammycakes • Hosted on GitHub Pages — Theme by mattgraham (with modifications)