Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Autofac's aggregate services in Microsoft's dependency injection framework

There is a C# asp.net core project that I'm seeing the number of injected interfaces in some constructors keeps growing. It's expected it may exceed 30 or 40 interfaces in some cases.

A bit of googling got me to Autofac's Aggregate Services. My question is that whether there's an equivalent in asp.net core's DI framework to avoid passing many interfaces to constructors?

like image 680
Arash Avatar asked Oct 27 '17 18:10

Arash


People also ask

Which framework is used for dependency injection?

.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in .NET is a built-in part of the framework, along with configuration, logging, and the options pattern.

Which is the NuGet package referred to use dependency injection in MVC core?

ASP.NET MVC 4 Dependency Injection features. Unity integration using Unity. Mvc3 NuGet Package.

What are the types of dependency injection C#?

There are three types of DIs: Constructor Injection. Setter Injection. Method Injection.

Is Autofac needed in .NET Core?

Autofac is the most widely used DI/IoC container for ASP.NET, and it is fully compatible with.NET Core as well. . NET Core has a built-in dependency injection framework that is ready to use. Even though the default DI may provide sufficient functionality, there are several limitations when using it.


1 Answers

As Evk mentioned in the comments, the dependency injection container at Microsoft.Extensions.DependencyInjection is deliberately a very simple one. If you are in need of more powerful features, you should consider switching to a full fletched DI container instead. ASP.NET Core is built to allow swapping out the DI container, and it’s actually not too difficult to do so. Autofac has a guide on how to do it.

That being said, Autofac’s aggregate services are not that much magic. Sure, you could build something like Autofac did and use Castle DynamicProxy to automatically implement the aggregate service. But you could also simply create such an aggregate service by hand:

public class MyAggregateService
{
    public IFirstService FirstService { get; }
    public ISecondService SecondService { get; }
    public IThirdService ThirdService { get; }
    public IFourthService FourthService { get; }

    public MyAggregateService (IFirstService first, ISecondService second, IThirdService third, IFourthService fourth)
    {
        FirstService = first;
        SecondService = second;
        ThirdService = third;
        FourthService = fourth;
    }
}

// then register that in the container
services.AddTransient<MyAggregateService>();

// and depend on it in the controller
public MyController (MyAggregateService aggregateService)
{ … }

Sure, you have to write a bit more, but it’s actually not that much more. And if you can do without those advanced features that Autofac offers, this is actually pretty simple and quickly done.

like image 83
poke Avatar answered Oct 20 '22 11:10

poke