SimpleInjector - Register a type for all it's interfaces

Posted by Karl Cassar on Stack Overflow See other posts from Stack Overflow or by Karl Cassar
Published on 2012-10-18T16:13:58Z Indexed on 2012/10/18 17:01 UTC
Read the original article Hit count: 200

Is it possible to register a type for all it's implementing interfaces? E.g, I have a:

public class Bow : IWeapon
{

    #region IWeapon Members

    public string Attack()
    {
        return "Shooted with a bow";
    }

    #endregion
}

public class HumanFighter 

{
    private readonly IWeapon weapon = null;
    public HumanFighter(IWeapon weapon)
    {
        this.weapon = weapon;
    }

    public string Fight()
    {

        return this.weapon.Attack();
    }

}

    [Test]
    public void Test2b()
    {
        Container container = new Container();
        container.RegisterSingle<Bow>();
        container.RegisterSingle<HumanFighter>();

        // this would match the IWeapon to the Bow, as it
        // is implemented by Bow
        var humanFighter1 = container.GetInstance<HumanFighter>(); 

        string s = humanFighter1.Fight();
    }

© Stack Overflow or respective owner

Related posts about dependency-injection

Related posts about simple-injector