2

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;
   }
}
1

1 Answer 1

2

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");
Sign up to request clarification or add additional context in comments.

1 Comment

Nice edit! I didn't realise there was a Named method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.