With StructureMap is it possible to make a Singleton object AND provide constructor arguments?

只愿长相守 提交于 2020-01-13 17:37:04

问题


I can't seem to figure out how to define a object as a singleton AND define two arguments for the constructor.

I can do either / or .. just not at the same time.

Eg. (this doesn't work)...

ForRequestedType<IFoo>()
    .TheDefaultIsConcreteType<Foo>()
    .CacheBy(InstanceScope.Singleton)
    .WithCtorArg("alpha").EqualToAppSetting("Alpha")
    .WithCtorArg("beta").EqualToAppSetting("Beta");

Suggestions?


回答1:


You are very close. The trick is that you need to use the alternate default DSL language TheDefault.Is.OfConcreteType

ForRequestedType<IFoo>()
    .CacheBy(InstanceScope.Singleton)
    .TheDefault.Is.OfConcreteType<Foo>()
    .WithCtorArg("alpha").EqualToAppSetting("alpha")
    .WithCtorArg("beta").EqualToAppSetting("beta");


来源:https://stackoverflow.com/questions/1600759/with-structuremap-is-it-possible-to-make-a-singleton-object-and-provide-construc

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