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 303079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:09:21+00:00 2026-05-12T07:09:21+00:00

On this AutoFac Best Practices page ( http://code.google.com/p/autofac/wiki/BestPractices ), they say: Don’t Pass the

  • 0

On this AutoFac “Best Practices” page (http://code.google.com/p/autofac/wiki/BestPractices), they say:

Don’t Pass the Container Around
Giving components access to the container, or storing it in a public static property, or making functions like Resolve() available on a global ‘IoC’ class defeats the purpose of using dependency injection. Such designs have more in common with the Service Locator pattern.
If components have a dependency on the container, look at how they’re using the container to retrieve services, and add those services to the component’s (dependency injected) constructor arguments instead.

So what would be a better way to have one component “dynamically” instantiate another? Their second paragraph doesn’t cover the case where the component that “may” need to be created will depend on the state of the system. Or when component A needs to create X number of component B.

  • 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-12T07:09:21+00:00Added an answer on May 12, 2026 at 7:09 am

    To abstract away the instantiation of another component, you can use the Factory pattern:

    public interface IComponentBFactory
    {
        IComponentB CreateComponentB();
    }
    
    public class ComponentA : IComponentA
    {
        private IComponentBFactory _componentBFactory;
    
        public ComponentA(IComponentBFactory componentBFactory)
        {
            _componentBFactory = componentBFactory;
        }
    
        public void Foo()
        {
            var componentB = _componentBFactory.CreateComponentB();
    
            ...
        }
    }
    

    Then the implementation can be registered with the IoC container.

    A container is one way of assembling an object graph, but it certainly isn’t the only way. It is an implementation detail. Keeping the objects free of this knowledge decouples them from infrastructure concerns. It also keeps them from having to know which version of a dependency to resolve.

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

Sidebar

Related Questions

I have started to use Autofac following this tutorials: http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html Simple class with no
I notice the latest alpha of SolrNet supports Autofac integration. This wiki page discusses
Looking at the deprecated downloads for Autofac out at code.google.com, I see: Release 1.3.2
In this Autofac IoC article they show an example of mapping an interface to
this is my code that I write it but I want to use LINQ
I have registered this with Autofac: builder.Register(c => ViewerFactory.CreateDefault()).SingleInstance(); I'm not sure, but I
let's say this scenario: public class B {}; public class C { public C(B
I have this method: private static void StartLicensePlateMonitor(Autofac.IContainer container) { var monitor = container.Resolve<LicensePlate.LicensePlateUploadMonitor>();
I need to register my Autofac container with specific interface, for this case I
This question is about IoC in general, but I'm using Autofac, so an Autofac

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.