Multiple implementations for one interface with DI

15,957

Your inversion of control container is not a factory per se. Your case is a perfect fit for the factory pattern.

Create a new abstract factory which is used to create your monsters:

public interface IMonsterFactory
{
    Zombie CreateZombie(string name);
    Vampire CreateVampire(int age);
}

And then register its implementation in Autofac.

Finally use the factory in your class:

class Graveyard : ILocation
{
  IMonsterFactory _monsterFactory;

  public Graveyard(IMonsterFactory factory)
  {
    _monsterFactory = factory;
  }

  public void PresentLocalCreeps()
  {
    var vampire = _monsterFactory.CreateVampire(300);
    vampire.IntroduceYourself();

    var zombie = _monsterFactory.CreateZombie("Rob");
    zombie.IntroduceYourself();
  }
}

You can of course use specific monster factories too if you want. None the less, using interfaces will imho make your code a lot more readable.

Update

But how would I implement the factory? On the one hand the factory should not use the IOC container to create the monsters, because that's considered evil (degrades the DI pattern to the service locator anti-pattern).

I'm getting so tired of hearing that SL is an anti-pattern. It's not. As with all patterns, if you use it incorrectly it will give you a disadvantage. That applies for ALL patterns. http://blog.gauffin.org/2012/09/service-locator-is-not-an-anti-pattern/

But in this case I don't see why you can't create the implementations directly in your factory? That's what the factory is for:

public class PreferZombiesMonsterFactory : IMonsterFactory
{
    public Zombie CreateZombie(string name)
    {
        return new SuperAwesomeZombie(name);
    }

    public Vampire CreateVampire(int age)
    {
        return new BooringVampire(age);
    }
}

It's not more complicated than that.

On the other hand the factory should not create the monsters itself, because that would bypass the IOC-container and tightly couple the factory and the monsters. Or am I on the wrong track again? ;-)

It doesn't matter that the factory is tighly coupled to the monster implementations. Because that's the purpose of the factory: To abstract away the object creation, so that nothing else in your code is aware of the concretes.

You could create SuperDeluxeMonsterFactory, MonstersForCheapNonPayingUsersFactory etc. All other code in your application wouldn't be aware of that you are using different monsters (by using different factories).

Each time you have to change concretes you either switch factory or you just modify the existing factory. No other code will be affected as long as your monster implementations do not violate Liskovs Substitution Principle.

Factory vs IoC container

So what's the difference between a factory and a IoC container then? The IoC is great at resolving dependencies for your classes and maintain the lifetimes (the container can for instance dispose all disposables automatically when a HTTP request ends)..

The factory on the other hand excels at creating objects for you. It does that and nothing else.

Summary

So if you somewhere in your code need to get a specific type of an implementation you typically should use a factory. The factory itself CAN use the IoC as a service locator internally (to resolve dependencies). That is OK since it's a implementation detail in the factory which do not affect anything else in your application.

Use the IoC container (through dependency injection) if you want to resolve a service (and do not care which implementation you get, or if you get a previously created instance).

Share:
15,957

Related videos on Youtube

Boris
Author by

Boris

Updated on September 16, 2022

Comments

  • Boris
    Boris over 1 year

    Right now I'm trying to teach myself the Dependency Injection pattern with the IOC-container from Autofac. I've come up with a very simple example, which is presented below. Although the example is simple, I fail to get it working properly.

    Here are my classes/interfaces:

    Two monsters, both implementing the IMonster interface:

    interface IMonster
    {
      void IntroduceYourself();
    }
    
    class Vampire : IMonster
    {
      public delegate Vampire Factory(int age);
    
      int mAge; 
    
      public Vampire(int age)
      {
        mAge = age;
      }
    
      public void IntroduceYourself()
      {
        Console.WriteLine("Hi, I'm a " + mAge + " years old vampire!");
      }
    }
    
    class Zombie : IMonster
    {
      public delegate Zombie Factory(string name);
    
      string mName;
    
      public Zombie(string name)
      {
        mName = name;
      }
    
      public void IntroduceYourself()
      {
        Console.WriteLine("Hi, I'm " + mName + " the zombie!");
      }
    }
    

    Then there's my graveyard:

    interface ILocation
    {
      void PresentLocalCreeps();
    }
    
    class Graveyard : ILocation
    {
      Func<int, IMonster>    mVampireFactory;
      Func<string, IMonster> mZombieFactory;
    
      public Graveyard(Func<int, IMonster> vampireFactory, Func<string, IMonster> zombieFactory)
      {
        mVampireFactory = vampireFactory;
        mZombieFactory  = zombieFactory;
      }
    
      public void PresentLocalCreeps()
      {
        var vampire = mVampireFactory.Invoke(300);
        vampire.IntroduceYourself();
    
        var zombie = mZombieFactory.Invoke("Rob");
        zombie.IntroduceYourself();
      }
    }
    

    And finally my main:

    static void Main(string[] args)
    {
      // Setup Autofac
      var builder = new ContainerBuilder();
      builder.RegisterType<Graveyard>().As<ILocation>();
      builder.RegisterType<Vampire>().As<IMonster>();
      builder.RegisterType<Zombie>().As<IMonster>();
      var container = builder.Build();
    
      // It's midnight!
      var location = container.Resolve<ILocation>();
      location.PresentLocalCreeps();
    
      // Waiting for dawn to break...
      Console.ReadLine(); 
      container.Dispose();
    }
    

    And this is my problem: During runtime, Autofac throws an exception on this line:

    var vampire = mVampireFactory.Invoke(300);
    

    It seems that the mVampireFactory is actually trying to instantiate a zombie. Of course this won't work since the zombie's constructor won't take an int.

    Is there a simple way to fix this? Or did I get the way Autofac works completely wrong? How would you solve this problem?

    • Davin Tryon
      Davin Tryon about 11 years
      I think you might be looking for Named Services.
    • MattDavey
      MattDavey about 11 years
      How is autofac resolving the two constructor arguments to the Graveyard class?
    • Fendy
      Fendy about 11 years
      MattDavey has a correct answer. Seems like the resolver cannot find the particular of Func<int, IMonster> and Func<string, IMonster> for both the constructor, thus replace with null. Maybe registering both of the func into the resolver can correct the problem
  • Boris
    Boris about 11 years
    But how would I implement the factory? On the one hand the factory should not use the IOC container to create the monsters, because that's considered evil (degrades the DI pattern to the service locator anti-pattern). On the other hand the factory should not create the monsters itself, because that would bypass the IOC-container and tightly couple the factory and the monsters. Or am I on the wrong track again? ;-)
  • jgauffin
    jgauffin about 11 years
  • Fabske
    Fabske about 11 years
    This is a good answer. I'll just add some info on a IoC that I know: Castle Windsor. It contains a type of component that you write and plug in the container, and the container will use to choose what implementation it'll return. Check this: docs.castleproject.org/Windsor.Handler-Selectors.ashx
  • Steven
    Steven about 11 years
    It's okay for a factory to call into the container when that factory implementation is part of the composition rooot. This wont be an SL implementation.
  • Fendy
    Fendy about 11 years
    @Boris I have additional thought about the answer provided form jgauffin. I'd loved to have one or more comments regarding it.
  • Konrad
    Konrad almost 6 years
    Can you as well just make it static?