4

I have used four layers in creating a sample project in ASP.net Core as shown below

enter image description here

I also implemented dependency injection in startup.cs in my API project. and it's working perfect.

services.AddTransient<IUserRepository, UserRepository>();

I have two questions to ask.

  1. Is it possible to dependency injection in my Infrastructure layer, not in API layer? If yes, can you guide me on how to do it?

  2. Correct me if i'm wrong, If Asp.Net Core has dependency Injection by default, then we don't need Autofac (or similar third party DI plugins). right? Let me rephrase the question. What's the use of Autofac in Asp Core?

Any advice would be helpful.

Thank you.

Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76
  • What do you exactly think about dependency injection in the infra layer? Do you mean by injecting something into your repository? – Johnny Dec 23 '16 at 13:32
  • Related: https://stackoverflow.com/questions/30681477/dependency-injection-in-asp-net-core-vnext That answers your question "If Asp.Net Core has dependency Injection by default, then we don't need Autofac [...] right?" – Steven Dec 23 '16 at 15:00
  • About "how to use DI in a root project", the answer is: you apply the Dependency Injection pattern to your classes and you compose all components in your [Composition Root](http://blog.ploeh.dk/2011/07/28/CompositionRoot/). Also see: https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application – Steven Dec 23 '16 at 15:03

1 Answers1

6
  1. 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;
     } 
}
  1. 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.
Danny van der Kraan
  • 5,344
  • 6
  • 31
  • 41
  • Even I am stuck with same issue. Your suggestion is good. But is there any way, by which we can move, all registration code in infrastructure project library. As in current case, we need to maintain Startup.cs and infrastructure project. – Purnima Naik Jan 25 '17 at 06:59
  • Do you mean registrations from other projects other than the webapp and the infrastructure project? – Danny van der Kraan Jan 25 '17 at 12:53
  • I mean all registrations just from infrastructure project. – Purnima Naik Jan 25 '17 at 14:10
  • All you have to do is call Configure of the infrastructure project in the Startup class. Maintenance of Startup this way is next to none this way. – Danny van der Kraan Jan 26 '17 at 11:37