Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

feeding dependencies to a factory class via IoC?

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.

like image 766
Mitch A Avatar asked Feb 15 '10 21:02

Mitch A


People also ask

Is IoC a factory pattern?

The reason Dependency Injection (DI) and Factory Patterns are similar is because they are two implementations of Inversion of Control (IoC) which is a software architecture. Put simply they are two solutions to the same problem.

Is Di a factory pattern?

Factory PatternDI is only aware of the dependencies. It does not know anything about the container or factory. It adds coupling between objects, factory, and dependency. It requires both a dependent object and a factory object to work properly.

What is dependency injection framework?

Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern. A dependency is an object that another object depends on. Examine the following MessageWriter class with a Write method that other classes depend on: C# Copy.

Is dependency injection a creational pattern?

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.


1 Answers

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.

like image 102
Mark Seemann Avatar answered Oct 31 '22 09:10

Mark Seemann