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

  • Home
  • SEARCH
  • 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 903949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:58:18+00:00 2026-05-15T15:58:18+00:00

After reading more and more about IoC containers, I read this post about not

  • 0

After reading more and more about IoC containers, I read this post about not having IoC.Resolve() etc in your code.

I’m really curious to know then, how can I remove the dependency on the container?

I’ll want to write code like the following:

public void Action()
{
    using(IDataContext dc = IoC.Resolve<IDataContext>())
    {
        IUserRepository repo = IoC.Resolve<IUserRepository>();
        // Do stuff with repo...
    }
}

But how can I get rid of the IoC.Resolve calls? Maybe I need a better understanding of DI…

Thanks in advance.

  • 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-05-15T15:58:20+00:00Added an answer on May 15, 2026 at 3:58 pm

    Generally speaking, most dependencies can be injected into your class at the time it is created. However, in this particular case, you need a component that must be created on demand at the time of use. In such cases, it is very difficult to completely remove dependency on an IoC container. My approach has always been to create a factory that is injected into the class at creation time, which in turn encapsulates all direct IoC usage. This allows your factories to be mocked for testing, rather than the IoC container itself…which tends to be a lot easier:

    // In Presentation.csproj
    class PresentationController
    {
        public PresentationController(IDataContextFactory dataContextFactory, IRepositoryFactory repositoryFactory)
        {
            #region .NET 4 Contract
            Contract.Requires(dataContextFactory != null);
            Contract.Requires(repositoryFactory != null);
            #endregion
    
            _dataContextFactory = dataContextFactory;
            _repositoryFactory = repositoryFactory;
        }
    
        private readonly IDataContextFactory _dataContextFactory;
        private readonly IRepositoryFactory _repositoryFactory;
    
        public void Action()
        {
            using (IDataContext dc = _dataContextFactory.CreateInstance())
            {
                var repo = _repositoryFactory.CreateUserRepository();
                // do stuff with repo...
            }
        }
    }
    
    // In Factories.API.csproj
    interface IDataContextFactory
    {
        IDataContext CreateInstance();
    }
    
    interface IRepositoryFactory
    {
        IUserRepository CreateUserRepository();
        IAddressRepository CreateAddressRepository();
        // etc.
    }
    
    // In Factories.Impl.csproj
    class DataContextFactory: IDataContextFactory
    {
        public IDataContext CreateInstance()
        {
            var context = IoC.Resolve<IDataContext>();
            // Do any common setup or initialization that may be required on 'context'
            return context;
        }
    }
    
    class RepositoryFactory: IRepositoryFactory
    {
        public IUserRepository CreateUserRepository()
        {
            var repo = IoC.Resolve<IUserRepository>();
            // Do any common setup or initialization that may be required on 'repo'
            return repo;
        }
    
        public IAddressRepository CreateAddressRepository()
        {
            var repo = IoC.Resolve<IAddressRepository>();
            // Do any common setup or initialization that may be required on 'repo'
            return repo;
        }
    
        // etc.
    }
    

    The benefit of this approach is, while you can not completely eliminate the IoC dependency itself, you can encapsulate it in a single kind of object (a factory), decoupling the bulk of your code from the IoC container. This improves your codes agility in light of, say, switching from one IoC container to another (i.e. Windsor to Ninject).

    It should be noted, an interesting consequence of this, is that your factories are usually injected into their dependents by the same IoC framework they use. If you are using Castle Windsor, for example, you would create configuration that tells the IoC container to inject the two factories into your business component when it is created. The business component itself may also have a factory…or, it may simply be injected by the same IoC framework into a higher-level component, etc. etc., ad inf.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a conceptual question. After reading about Jquery Mobile, it got
Update: After some more reading I see that this problem is totally general, you
After reading http://www.pragprog.com/magazines/2010-03/javascript-its-not-just-for-browsers-any-more I'm wondering which is the best IDE to develop server-side javascript
After reading the Bash man pages and with respect to this post , I
After reading about valHooks in a jQuery defect and more recently seen in a
After reading this article about validating with service layer I have some doubts. First:
After reading this Testability and Entity Framework 4.0 article about 75 times yesterday, it
After reading a bit more about how Gnutella and other P2P networks function, I
I've been reading about XML, XSL, XPath, etc. I want to start this small
Recently, I've got a dangerous idea into my head after reading this blog post.

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.