Monday, 15 August 2011

Why can't I change the scope of my object with ServiceStacks IoC? -



Why can't I change the scope of my object with ServiceStacks IoC? -

given next code configure method:

ormliteconnectionfactory dbfactory = new ormliteconnectionfactory(configutils.getconnectionstring("oracle:feconnection"), oracleormlitedialectprovider.instance); container.register<idbconnectionfactory>(dbfactory)).reusedwithin(reusescope.request); // <== not work // these work container.register<ipreprocessorrepository>(c => new cachedpreprocessorrepository(dbfactory, c.resolve<icacheclient>())).reusedwithin(reusescope.default); container.register<ipreprocessor>(c => new directapipreprocessor(c.resolve<ipreprocessorrepository>(), c.resolve<ivalidator<leadinformation>>())).reusedwithin(reusescope.default);

how can create sure dbfactory instanciated used in other registrations per request?

thank you, stephen

you can't alter scope of this:

container.register<idbconnectionfactory>(dbfactory) .reusedwithin(reusescope.request);

because you're passing in instance of object, , not factory function ioc need able instantiate instances of object itself. ioc can in case homecoming instance, making singleton.

to able alter scope need register delegate can create instance, i.e:

container.register<idbconnectionfactory>(c => new ormliteconnectionfactory(...)) .reusedwithin(reusescope.request);

but never want connection or client factories idbconnectionfactory or iredisclientsmanager since they're designed used singletons.

i.e. they're thread-safe singleton factories used create single client/connection instances:

using (var db = container.resolve<idbconnectionfactory>().open()) { //... } using (var redis = container.resolve<iredisclientsmanager>().getclient()) { //... }

servicestack

No comments:

Post a Comment