c# - Is there a way to set different lifecycle for each forwared type? -
castle windsor doesn't allow registrations contain duplicate implementation type. allows type forwarding means can map implementation type more 1 service types long register once.
asp.net vnext's dependency injection library requires same implementation type registered multiple service types. however, these service types have different lifecyles shown here.
public static ienumerable<iservicedescriptor> defaultservices() { var describer = new servicedescriber(); yield homecoming describer.transient<ifakeservice, fakeservice>(); yield homecoming describer.scoped<ifakescopedservice, fakeservice>(); yield homecoming describer.singleton<ifakesingletonservice, fakeservice>(); yield homecoming describer.transient<ifakefallbackservice, fakeservice>(); }
and here's forward method signature:
public componentregistration<tservice> forward(params type[] types)
as can see, doesn't take lifecycle parameters. ninject test suite passing. can supported in castle windsor too?
in castle windsor can register same implementing type more once. e.g.
container.register( component.for<ifakeservice>().implementedby<fakeservice>(), component.for<ireallyfakeservice>().implementedby<fakeservice>());
i think whole concept of forwarding types ceases create sense if want different lifestyles, since thought behind forwarding same instance of typea each interface register typea.
for example, above registrations gave if ran
var fakeservice = container.resolve<ifakeservice>(); var reallyfakeservice = container.resolve<ireallyfakeservice>(); object.referenceequals(fakeservice, reallyfakeservice);// => false
then 2 different instances. however, if instead utilize registration
container.register( component .for<ifakeservice>() .forward<ireallyfakeservice>() .implementedby<fakeservice>());
then object.referenceequals(fakeservice, reallyfakeservice);
homecoming true
. however, if want different lifestyles interfaces ceases true , you're multiple registrations anyway.
c# dependency-injection castle-windsor asp.net-core
No comments:
Post a Comment