Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:47:49+00:00 2026-06-07T04:47:49+00:00

I heared this should be possible, but I can not imagine how this should

  • 0

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
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:47:51+00:00Added an answer on June 7, 2026 at 4:47 am

    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
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure I heard this right, but VS2010 no longer has support for Wix?
We have all heard of how in a for loop, we should do this:
I heard this term many times when I came across some protocol implementations, but
Well I know some kinds of UML's diagrams, but I haven't heard about this
I'm using a ListView to show Items in a List. The user can select
I have been using WPF for a new project which is far from done.
I've heared that you (in some cases) can prevent timeouts by sending the HTTP-header
I haven't ever heard it's possible, but asking with the hope that it might.
I am an Objective-C newbie, who still does not have a Mac, but still
Rendering a 3D-map from an Openstreetmap data can be pretty cumbersome . But what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.