2

How do I wire structuremap to inject properties when builing instances of interface IDummy.

Let's say that I have a concrete class called Dummy which implements interface IDummy.

The Dummy class got two properties, the first one called DataContext implements IDataContext, the second property is just a basic string called MyDummyString..

Then a second IDummy implementation called DummyConcrete2, only has one property, MyDummyString(as above).

How do I wire this in Structure Map, so when I request concrete DummyConcrete2, properties are by default injected. Have googled a lot, but haven't been able to figure it out yet. The StructureMap documentation seems to be a few versions old(a lot of deprecated methods)..

Any comment that could shed some light on this would be great!

Thanks!

Robin
  • 21
  • 2
  • You don't ask a DI Container (StructureMap or other containers) for concrete types, so it makes no sense to talk about requresting DummyContrete2. Reqeusting IDummy would make sense. – Mark Seemann Feb 23 '10 at 14:41
  • Let's say that I have different concrete implementations of an interface. And I want to build a specific concrete for the only sake of have the concretes properties autowired. Shouldent I be able to do that? That's basic DI.. Or am I attacking the issue from the wrong side? – Robin Feb 23 '10 at 14:46
  • Like Mark I agree that typically you should not take a dependency on concrete types and try to stick to interfaces. That said. StructureMap is quite capable of returning you concrete types and even supporting their configuration. I commonly use concrete types for simple objects that won't benefit too much from an abstracting interface e.g. some parameter object like DatabaseSettings. – KevM Feb 23 '10 at 17:53
  • If you have several concrete types and need to pick one of them, Abstract Factory is the most common solution: See here for an example: http://stackoverflow.com/questions/2168704/wcf-dependency-injection-and-abstract-factory/2168882#2168882 – Mark Seemann Feb 23 '10 at 18:39

1 Answers1

1

As Mark mentions in his comment you typical do not take dependencies directly on concrete objects. But you can configure how StructureMap constructs concrete objects. Here is an example using the latest configuration DSL.

public interface IFoo { }
public class Foo : IFoo { }
public class Foo2 : IFoo { }
public interface IDummy
{
    IFoo Foo { get; set; }
}
public class Dummy : IDummy
{
    public IFoo Foo { get; set; }
}
public class Dummy2 : IDummy
{
    public IFoo Foo { get; set; }
}

[TestFixture]
public class configuring_concrete_types
{
    [Test]
    public void should_use_configured_setter()
    {
        var container = new Container(cfg =>
        {
            cfg.ForConcreteType<Dummy>().Configure.Setter<IFoo>().Is(new Foo());
            cfg.ForConcreteType<Dummy2>().Configure.Setter<IFoo>().Is(new Foo2());
        });

        container.GetInstance<Dummy>().Foo.ShouldBeOfType<Foo>();
        container.GetInstance<Dummy2>().Foo.ShouldBeOfType<Foo2>();
    }
}

I hope this gets you moving in the right direction.

KevM
  • 2,496
  • 1
  • 22
  • 25