With StructureMap can I create a Singleton object And provide constructor arguments?

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

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

Eg. (this does not work)...

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

Suggestions?

+3
source share
1 answer

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

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

Source: https://habr.com/ru/post/1720680/


All Articles