开发者

feeding dependencies to a factory class via IoC?

开发者 https://www.devze.com 2022-12-20 10:42 出处:网络
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:

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:开发者_StackOverflow中文版
                    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.


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.


sounds like you want your cake and to eat it too. you need to commit to your IOC strategy.

you will produce mo an betta code and the chicks will dig you more too.... ;p

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号