开发者

StructureMap inject parent/child relationship

I am new to StructureMap. Any guidance for the following questi开发者_如何学Con is appreciated. Thanks!

I have got some code like:

public interface IFoo
{
    IBar Child { get; }
    void SayIt();
}

public class Foo: IFoo
{
     public IBar Child {get; set;}
     public Foo(string message) {...}
     public void SayIt() {...}
}

public interface IBar
{
    IFoo Parent {get;}
}

public class Bar: IBar
{
    private IFoo parent;
    public IFoo Parent { get {return parent;} }
    public Bar(IFoo parent)
    {
        this.parent = parent;
    }

}

I am constructing IFoo using the following ObjectFactory

ObjectFactory.Configure(x =>
            {
                var foo = x.For<IFoo>().Use<Foo>();
                //x.For<IBar>().Use<Bar>().Ctor<IFoo>().Is(foo);

                x.SetAllProperties(c =>
                    {
                        c.OfType<IBar>();

                    }
                );
            }
            );

I am constructing foo objects like:

var foo = ObjectFactory.With<string>("Hello world").GetInstance<IFoo>();

However, I have difficulties configuring the Child property of IFoo to be an instance of IBar with the parent property pointing back to the instance that I'm creating, e.g. the one with "Hello World" as the message. How can I accomplish this? Thanks!


You can't do this because StructureMap will throw a bi-directional dependency exception. Most object graphs that look like this don't belong in the container because they are data objects. For example, xml document objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜