简体   繁体   English

AutoFac构造函数多个接口

[英]AutoFac constructor multiple Interface

I am using Autofac in my project. 我在我的项目中使用Autofac。 I want to use a simple interface to resolve them. 我想使用一个简单的界面来解决它们。 Not generic repository. 不是通用存储库。

I was using Castle in my old projects. 我在旧项目中使用Castle。 It has a class which have static methods. 它有一个具有静态方法的类。 I used it in my constructor method like this; 我在这样的构造函数方法中使用了它;

IService.ProductService.GetMyProducts();

In Autofac i couldnt find anything like above. 在Autofac中,我找不到上述任何内容。 Please help me. 请帮我。 I dont wanna use a lot of Interfaces in my constructor. 我不想在构造函数中使用很多接口。

private IGeneralRepository generalRep;
        private IDenemeRepository denemeRep;
        private IGokberkRepository GokberkRep;

        public HomeController(IDenemeRepository dr,IGokberkRepository gr, IGeneralRepository ger)
        {
            generalRep = ger;
            denemeRep = dr;
            GokberkRep = gr;
        }

I can think of two ways to reduce number of injected services in your constructor. 我可以想到两种减少构造器中注入服务数量的方法。

First , in Autofac you can have a single parameter of type IComponentContext , and when your service is resolved from a container instance, IComponentContext dependency is automatically resolved to the instance of the container. 首先 ,在Autofac中,您可以拥有一个类型为IComponentContext参数,并且当您从容器实例解析服务时, IComponentContext依赖关系将自动解析为容器实例。 Then you can resolve the rest of your dependencies from it: 然后,您可以从中解决其余的依赖关系:

// constructor of your component
public MyComponent(IComponentContext components)
{
    _serviceA = components.Resolve<IServiceA>();
    _serviceB = components.Resolve<IServiceB>();
}

BTW, in Castle Windsor you had to explicitly register the instance of the container in order to make the above method work. 顺便说一句,在温莎城堡中,您必须显式注册该容器的实例才能使上述方法起作用。

Second way is creating one "composite" service which contains all (or the most common) services the application needs. 第二种方法是创建一个“复合”服务,其中包含应用程序需要的所有(或最常见的)服务。 Then inject that service and get all the others from it: 然后注入该服务并从中获取所有其他服务:

// composite service - implement this interface as shown below
public interface ICommonServices
{
     IServiceA ServiceA { get; }
     IServiceB ServiceB { get; }
}

The composite service implementation: 组合服务实现:

// a class that implements ICommonServices interface
public class CommonServices : ICommonServices
{
    public CommonServices(IServiceA serviceA, IServiceB serviceB)
    {
        this.ServiceA = serviceA;
        this.ServiceB = serviceB;
    }

    public IServiceA ServiceA { get; private set; }
    public IServiceB ServiceB { get; private set; }
}

Note that you must register the composite service and the inner services in the container. 请注意,您必须在容器中注册复合服务和内部服务。

Now you can have only one parameter in your constructor: 现在,您的构造函数中只能有一个参数:

public MyComponent(ICommonServices services)
{
    _services = services;
}

Using the inner services: 使用内部服务:

public void SomeMethod()
{
    _services.ServiceA.DoSomething();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM