简体   繁体   English

IoC:程序集之间的依赖注入和整体实例

[英]IoC: Dependency Injection and overall instances between assemblies

I heared this should be possible, but I can not imagine how this should work. 我听说这应该可行,但是我无法想象这应该如何工作。

I am using Dependency Injection (autofac) for my project. 我在我的项目中使用依赖注入(autofac)。 I develop a project with someone else and call the methods of his classes (I use his assembly). 我与其他人一起开发一个项目,并调用他的类的方法(我使用他的程序集)。

Then I get an instance of an object which the other person should use for his operations. 然后,我得到另一个人应该用于其操作的对象的实例。 We want to avoid passing this object instance on every method and use autofac. 我们希望避免在每种方法上都传递此对象实例,而应使用autofac。

Can he resolve this instance in his assembly project without passing any parameter? 他可以在不传递任何参数的情况下在装配项目中解析此实例吗? I think we have to pass at least the DI-Container... But I heard that the concept of Dependency Injection should make it possible that you can resolve objects in the whole "execution context" and get the same one. 我认为我们至少必须通过DI容器...但是我听说依赖注入的概念应该使您有可能可以在整个“执行上下文”中解析对象并获得相同的对象。

Here is an example with the asp.net web api: 这是asp.net Web api的示例:

This is a api controller of a asp.net webapi project: 这是一个asp.net webapi项目的api控制器:

public class DocumentsController : ApiController
{
    // GET /api/documents
    public HttpResponseMessage Get()
    {
        // Here I call the method of the other developer, 
        // security/authorization should be handled in 
        // his method!
        // In this context the WebAPI provides the 
        // IPrincipal of the current user in this 
        // variable => "HttpContext.Current.User" but we 
        // don't want to pass it on every method call
        ClassFromOtherAssembly.GetDocuments();

        HttpResponseMessage response = 
            Request.CreateResponse<IEnumerable<Document>>(
                HttpStatusCode.OK, documents);

        return response;
    }
}

This is the class of the other developer. 这是其他开发人员的类。 He should deliver the documents and check if the user is authorized: 他应交付文件并检查用户是否被授权:

public class ClassFromOtherAssembly
{
    public List<Documents> GetDocuments()
    {
        //Security check
        IPrincipal principal = 
            DI_Container.Resolve(IPrincipal);

        if(principal.IsInRole("Admin"))
        {
            //return the list
        }
        else
        {
            //return empty list
        }
    }
}

No, do not pass the container itself, you will end up with a Service Locator patter, and if you do a quick search you will understand that this pattern has a rotten smell. 不,不要通过容器本身,您将得到“ 服务定位器”模式,如果进行快速搜索,您将了解到这种模式有腐烂的味道。

public class Foo
{
    private IContainer container;
    private IBar bar;

    public Foo( IContainer container) //no-no
    {
         this.container = container;
         this.bar = container.Resolve<IBar>();
    }
}

Instead use proper DI, like 而是使用适当的DI,例如

public class Foo
{
    private IBar bar;

    public Foo(IBar bar)
    {
        this.bar = bar;
    }
}

It doesn't really matter in which assembly your types are. 您的类型在哪个程序集中并不重要。 This is the whole point of IoC and DI - to decouple parts of the application and make you depend on abstractions, rather than concrete implementations. 这就是IoC和DI的重点-分离应用程序的各个部分,并使您依赖抽象,而不是具体的实现。


Edit 编辑
You misunderstood Service locator pattern with DI. 您用DI误解了服务定位器模式。 "Instead of passing a parameter we want to use Dependency Injection" - passing a parameter is a DI, in contrast, resolving a type from a static container is a service locator. “而不是传递参数,我们要使用依赖注入”-传递参数是DI,相反,从静态容器解析类型是服务定位器。

 public class DocumentsController : ApiController { public HttpResponseMessage Get() { ClassFromOtherAssembly.GetDocuments(); //this is Service locator //really bad for testability and maintenance ... } } 

DI looks like this DI看起来像这样

 public class DocumentsController : ApiController { private IDocumentProvider; public DocumentsController(IDocumentProvider provider) { this.provider = provider; } public HttpResponseMessage Get() { provider.GetDocuments(); //this is DI ... } } 

You're using the ServiceLocator (anti-pattern) by calling Resolve directly from GetDocuments() 您通过直接从GetDocuments()调用Resolve来使用ServiceLocator(反模式GetDocuments()

Use inversion of control with constructor injection to pass in the IPrinciple thus: 通过构造函数注入使用控件的反转来传递IPrinciple:

public class ClassFromOtherAssembly
{
    private IPrincipal principal;

    public ClassFromOtherAssembly(IPrincipal principal)
    {
        this.principal = principal;
    }

    public List<Documents> GetDocuments()
    {
        //Security check
        if (principal.IsInRole("Admin"))
        {
            //return the list
        }
        else
        {
            //return empty list
        }
    }
}

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

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