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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:32:37+00:00 2026-05-22T18:32:37+00:00

Is it possible for autofac to create a generic factory that can only resolve

  • 0

Is it possible for autofac to create a generic factory that can only resolve types of a specific base class?

I’m currently seeing how feasible it is to retrofit a ‘brownfield’ c# project to use autofac. The project requires creating a tree of components of differing types but of the same base class. These components require authentication and database services (among other things) so it makes it hard to just have empty constructors.

So the factory would be so a user can create objects deriving a base class, use autofac to scan for these classes, and provide a factory for them. I thought it might make it easier to build component object trees if you didn’t have to provide factories for each class.

As an aside, or is this a sign of bad design? Should the components and the tree do as little as possible and we pass this tree to other services to provide processing and rendering?

Something like (where ? is the mystery factory)

public MyBase { 
    public Add(MyBase x) {..}
}

public DerivedA: MyBase {}

public DerivedB : MyBase {}

public DerivedC : DerivedA {}

public SomethingElse
{
     private ? _myBaseFact;

     public SomethingElse(? myBaseFact) { 
            _myBaseFact = myBaseFact;
     }

     public BuildComponentTree() {
        DerivedA a = _myBaseFact<DerivedA>();
        DerivedB b = _myBaseFact<DerivedB>();
        DerivedC c = _myBaseFact<DerivedC>();
        a.Add(b);
        b.Add(c);
     }
}

edit: I have been asked for a more concrete example, which is fair 🙂 There are framework components and I wanted to allow for the developers to create their own components without having to register with autofac. Having a factory class would mean that the developer would have to add their custom components to this factory right? Please excuse the example, this is similar to how the framework works when I encountered it.

FwComponent {
    public FwComponent (DataProvider, AuthManager, OtherModule)
    public Add(FwComponent x);
    public Setup();
    public Process();
    public Render();
}

Page : FwComponent {
    public Page(DataProvider, AuthManager, OtherModule): base(..)
    public string Title;
    ...
}

Input: FwComponent {
    public Input(DataProvider, AuthManager, OtherModule): base(..)
    public string Name;
    public int length;
    ...
}

Button: FwComponent {
    public Button(DataProvider, AuthManager, OtherModule): base(..)
    public string Name;
    ...
}

----

MyCustomButton : Button {
     public MyCustomButton(DataProvider, AuthManager, OtherModule): base(..)
}

MyPersonalPage : FwComponent {

    IContext _container;

    public MyPersonalPage(DataProvider, AuthManager, OtherModule, IContext container): base(..)

    public Setup() {
         var input = _container.Resolve<Input>();
         input.Name = 'xxx';
         input.length = 10;
         Add(input);

         var butt = _container.Resolve<MyCustomButton>();
         butt.name = "xxx";
         Add(butt);    
    }

    public Process() {
        DataProvider.DoStuff();
    }
}
  • 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-22T18:32:38+00:00Added an answer on May 22, 2026 at 6:32 pm

    In the code sample you gave, you seem to request for a specific type using _myBaseFact<DerivedA>() and _myBaseFact<DerivedB>(). It’s hard to see what you are trying to accomplish, but this seems like a code smell to me, because you still have a dependency on the concrete types.

    As I see it you have two choices: Either you inject a factory and abstract away the derived types, -or- you directly inject the derived types into the constructor. Using a factory is useful in two scenario’s:

    1: You have some sort of (not type based) argument that allows the factory to identify the returned type, as shown in the following example:

    public class MyBaseFactory : IMyBaseFactory
    {
        public MyBase CreateMyBase(IUserContext user)
        {
            // Create a MyBase based on a user object.
        }
    }
    

    2: Your factory has multiple factory methods:

    public class MyBaseFactory : IMyBaseFactory
    {
        private Autofac.IContainer container;
    
        public MyBase CreateMyBaseForScenarioA()
        {
            return container.Resolve<DerivedA>();
        }
    
        public MyBase CreateMyBaseForScenarioB()
        {
            return container.Resolve<DerivedB>();
        }
    }
    

    Note that both factory methods return a MyBase and never the specific type. The whole idea about polymorphism is that you shouldn’t care about the concrete type.

    IMO it doesn’t matter whether Autofac could create a factory for you or not. You shouldn’t rely on framework specific constructs like that, but depend on good application design. If you go for the factory approach, define an interface for that factory and inject an instance based on that interface. This makes the design very clean and it communicates it’s intend very nicely.

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

Sidebar

Related Questions

Is it possible to easily configure autofac so it will only resolve using non-obsolete
Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: Qt equivalent of PathAppend? Is there a class that handles file paths
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
I user Autofac and wonder if it is possible to come around the DependencyResolutionException
Possible Duplicate: How can I increment a date by one day in Java? I
I am currently using Autofac, but am open to commentary regarding other IOC containers
I know using Autofac , it is possible to host a WCF service. What
Possible Duplicate: How to: URL re-writing in PHP? How can a website use an

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.