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:
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.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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With