Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

structuremap

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?

like image 310
Pure.Krome Avatar asked Dec 30 '25 15:12

Pure.Krome


1 Answers

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");
like image 54
KevM Avatar answered Jan 01 '26 05:01

KevM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!