简体   繁体   中英

Multiple implementations for one interface with DI

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?

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).

Multiple implementations with Func<> I am using the same logic as before, interface IMovement , three implementations, Cat, Dog, and Human.

I add an Enum, the MovementEnum . Because this approach uses DI to register a Func<> which returns and interface implementation depending on a specific key, you can use another type to represent the Enum.

public enum MovementEnum
{
  Cat = 1,
  Dog = 2,
  Human = 3
}

public class Cat : IMovement
{
  public string Walk()
  {
    return “Im a Cat, walking!”;
  }
}
public class Dog : IMovement
{
  public string Walk()
  {
    return “Im a Dog, walking!”;
  }
}
public class Human : IMovement
{
  public string Walk()
  {
    return “Im a human, walking!”;
  }
}

To register first, you need to register the classes like this.

builder.Services.AddScoped<Dog>();
builder.Services.AddScoped<Human>();
builder.Services.AddScoped<Cat>();

And then register the Func<> with the Enum and the Interface. To choose between each class, I use a switch with the Enum.

builder.Services.AddTransient<Func<MovementEnum, IMovement>>(movementProvider => key =>
{
  switch (key)
  {
    case MovimentEnum.Cat:
      return movementProvider.GetService<Cat>();
    case MovimentEnum.Dog:
      return movementProvider.GetService<Dog>();
    case MovimentEnum.Human:
      return movementProvider.GetService<Human>();
    default:
      return null;
  }
});

To use this in your controller, inject the Func<> like this.

[ApiController]
[Route("[controller]/[action]")]
public class MovementController : ControllerBase
{

    private readonly IMovement _dogMovement;
    private readonly IMovement _catMovement;
    private readonly IMovement _humanMovement;
    public MovementController(Func<MovementEnum, IMovement> serviceResolver)
    {
        _dogMovement = serviceResolver(MovementEnum.Dog);
        _catMovement = serviceResolver(MovementEnum.Cat);
        _humanMovement = serviceResolver(MovementEnum.Human);
    }

    [HttpGet]
    public string GetCat()
    {
        return _catMovement.Walk();
    }

    [HttpGet]
    public string GetDog()
    {
        return _dogMovement.Walk();
    }

    [HttpGet]
    public string GetHuman()
    {
        return _humanMovement.Walk();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM