Search code examples
c#dependency-injectionioc-containerautofacservice-locator

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. 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.

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.

Here is an example with the asp.net web api:

This is a api controller of a asp.net webapi project:

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
        }
    }
}

Solution

  • 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

    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.


    Edit
    You misunderstood Service locator pattern with 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.

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

    DI looks like this

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