- Yes of course. Works like any other DI out there. What I do for example is:
Give your Infrastructure layer a Config class with a Configure method that expects the IServeCollection in its signature, like so:
public static class InfrastructureConfiguration
{
public static void Configure(IServiceCollection services)
{
}
}
You can call this Configure method from the ConfigureServices method in the Startup class.
In the configure method you hook up what you need, like for instance you've got MyAwesomeInfraClass you can do this:
public static class InfrastructureConfiguration
{
public static void Configure(IServiceCollection services)
{
services.AddTransient<MyAwesomeInfraClass>();
}
}
Now you can inject MyAwesomeInfraClass anywhere you'd like, for instance in a controller, like so:
public class HomeController : Controller
{
private readonly MyAwesomeInfraClass _myAwesomeInfraClass;
public HomeController(MyAwesomeInfraClass myAwesomeInfraClass){
_myAwesomeInfraClass = myAwesomeInfraClass;
}
}
- If the default container from .NET Core provides you with everything you need, then you need to look no further. But we use to replace the default container with Autofac and then Autofac's container with NServiceBus's, because it's the container that is being shared throughout our distributed system and because it has a really cool feature to just scan the assemblies and register components automatically. This is an example. It really depends on your use case.