Rich Hildebrand
Rich Hildebrand

Reputation: 1885

StructureMap - How do I use multiple objects inheriting from the same interface

When using StructureMap I would like class A to be injected with Bar and class B to be injected with Baz.

How would I configure / setup this relationship with StructureMap?

public class Bar : IFoo {}
public class Baz : IFoo {}

public class A
{
   private IFoo _foo;

   public A(IFoo foo)
   {
      _foo = foo; 
   }
}

public class B
{
   private IFoo _foo;

   public B(IFoo foo)
   {
      _foo = foo;
   }
}

Upvotes: 2

Views: 185

Answers (1)

demoncodemonkey
demoncodemonkey

Reputation: 11957

From this answer I think you need to do something like this:

For<IFoo>().Add<Bar>().Named("bar");
For<IFoo>().Add<Baz>().Named("baz");

For<A>()
    .Use<A>()
    .Ctor<IFoo>()
    .Named("bar");

For<B>()
    .Use<B>()
    .Ctor<IFoo>()
    .Named("baz");

Upvotes: 2

Related Questions