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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:25:46+00:00 2026-05-13T17:25:46+00:00

I have a factory class that decides which of four available subclasses it should

  • 0

I have a factory class that decides which of four available subclasses it should instantiate and return. As you would expect, all subclasses implement the same interface:

public static class FooFactory{
     public IFoo CreateFoo(FooEnum enum){
            switch (enum)
            {
                case Foo1:
                    return new Foo1();
                case Foo2:
                    return new Foo2();
                 case Foo3:
                    return new Foo3(IBar);//has a constructor dependency on IBar
                case Foo4:
                    return new Foo4();
                 default:
                    throw new Exception("invalid foo!");
            }
     }
}

As you can see, one of the subclasses has a dependency defined in its constructor.

Some points of interest:

  • We’re using Spring.NET as our IoC.
  • All subclasses of IFoo are domain objects and therefore are not being instantiated by Spring.NET. I’d like to keep things this way if at all possible.
  • The application has a hand written Data Access Layer (puke) so no ORM is in play here.

I’m trying to figure out how best to pass the IBar dependency into Foo3 from FooFactory. I get the feeling that this might be a problem best resolved via IoC but I can’t quite grok how. I also want to keep FooFactory as unit testable as possible: i.e. I’d prefer not have to have dependencies on Spring.NET in my test code.

Thanks for reading.

  • 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-13T17:25:46+00:00Added an answer on May 13, 2026 at 5:25 pm

    Change FooFactory to an Abstract Factory and inject the IBar instance into the concrete implementation, like this:

    public class FooFactory : IFooFactory {
         private readonly IBar bar;
    
         public FooFactory(IBar bar)
         {
             if (bar == null)
             {
                 throw new ArgumentNullException("bar");
             }
    
             this.bar = bar;
         }
    
         public IFoo CreateFoo(FooEnum enum){
                switch (enum)
                {
                    case Foo1:
                        return new Foo1();
                    case Foo2:
                        return new Foo2();
                     case Foo3:
                        return new Foo3(this.bar);
                    case Foo4:
                        return new Foo4();
                     default:
                        throw new Exception("invalid foo!");
                }
         }
    }
    

    Notice that FooFactory is now a concrete, non-static class implementing the IFooFactory interface:

    public interface IFooFactory
    {
        IFoo CreateFoo(FooEnum emum);
    }
    

    Everywhere in your code where you need an IFoo instance, you will then take a dependency on IFooFactory and use its CreateFoo method to create the instance you need.

    You can wire up FooFactory and its dependencies using any DI Container worth its salt.

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

Sidebar

Related Questions

I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a factory class that populates objects with data. I want to implementing
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
In my console application have an abstract Factory class Listener which contains code for
I have a factory class that serves out a bunch of properties. Now, the
Platform: C# 2.0 WinForms I have a factory class that provides an instantiation of
I have a factory class that creates objects with circular references. I'd like them
I have a Generic Class Factory class that has two methods one utilizes the
I have a factory class that creates an object based on a parameter it
So I have a factory class and I'm trying to work out what the

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.