StructureMap singleton usage (A class implementing two interface)

≯℡__Kan透↙ 提交于 2019-11-27 07:04:51

You can use the Forward<,>() registration to tell StructureMap to resolve a type using the resolution of a different type. This should do what you expect:

ObjectFactory.Initialize(x =>
{
    x.For<IInterface1>().Singleton().Use<MyClass>();
    x.Forward<IInterface1, IInterface2>();
});
Chris Marisic

I would register the MyClass itself and then pull that out of the context for the Use statements of the individual interfaces.

ForSingletonOf<MyClass>().Use<MyClass>();

For<IInterface1>().Use(ctx => ctx.GetInstance<MyClass>());
For<IInterface2>().Use(ctx => ctx.GetInstance<MyClass>());

Try looking at the different overloads to Use, especially Func overload. From there you can tell how StructureMap should create your instance with another object already registred.

An ObjectFactory is intended to create multiple instances. If you want a singleton, write a singleton class (perhaps with public IInterface1 and IInterface2 properties as accessors).

It seems sketchily documented, but perhaps you can use a Container instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!