views:

205

answers:

1

Hi!

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!

A: 

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