c# - Multiple distinct instances of object graph in Castle Windsor -
this simplified version of setup. have application consumes library, , library provides client accessing web service. application wraps in xyzservice provides higher level operations.
the library provides parameterised windsor installer, registers webapiclient , dependencies. installer takes configuration object (amongst other things) specifies uri web service.
this fine long ever want talk single instance of web service. in case there 2 different instances of web service, , not interchangeable. @ startup want app constructed references 2 distinct instances of xyzservice, first object-graph configured access (say) http://server.green/
, second object-graph configured access (say) http://server.red/
.
here's diagram:
on left if have 2 constructor parameters of type ixyzservice. 2 instances of webapiclient (since it's got transient lifetime), both share same configuration (since installer created instance , registered that).
on right want. i'm not sure how provide 2 iwebapiclientconfigurations , tell castle windsor when it's resolving ixyzservices app should create 2 separate object graphs, first 1 configuration , second other.
what tried
i thought @ first typed factory might wanted. created interface this:
public interface ixyzservicefactory { ixyzservice getxyzservice(iwebapiclientconfiguration webapiclientconfiguration); void release(ixyzservice xyzservice); }
and registered this:
container.register( component.for<ixyzservicefactory>() .asfactory());
i changed app take factory , use request 2 instances of ixyzservice, passing different configuration each time. thought configuration might used when tried construct webapiservice. however:
- it didn't work - configuration object passed in ignored , still got configuration instance provided library's installer.
- even if had worked, have been nasty, because cluttered app knowledge of configuration objects , of lifetime of xyzservice.
what want
ideally, want know how tell windsor when calls app's constructor:
public app(ixyzservice xyzservice1, ixyzservice xyzservice2)
...that should construct object graph each normal, except when comes resolve iwebapiclientconfiguration, first graph should 1 instance , second graph should another. want instantiate , register instances of iwebapiclientconfiguration in app's windsor installer.
i'd prefer not duplicate library's windsor installer in app, want registers except webapiclientconfiguration. if there's no way around put doing that.
while constrained in can library, still interested in answers recommend how handle sort of situation without constraints.
notes
i'm using castle windsor 2.5.3. know quite old. if solution requires willing try upgrade, assuming there no breaking changes stop me using library , existing installer. if there quite difficult upgrade.
i've tried remove extraneous details without making things abstract motivation no longer clear. please let me know if there ways can clarify question without making excessively long.
in 3.x versions of castle windsor can register multiple implementations as named , explicitly specify named dependencies on other registrations. (of course registration missing implementations, lifestyles... not important here):
container.register( // register config containing http://server.green/, set name, // , register named ixyzservice depending on config. component.for<iwebapiclientconfiguration>().named("greenconfig"), component.for<ixyzservice>().named("greenservice") .dependson(typeof (iwebapiclientconfiguration), "greenconfig"), // register config of same interface containing http://server.red/, // , named ixyzservice uses it. component.for<iwebapiclientconfiguration>().named("redconfig"), component.for<ixyzservice>().named("redservice") .dependson(typeof (iwebapiclientconfiguration), "redconfig"));
then can resolve service using name key:
var greenservice = container.resolve<ixyzservice>("greenservice"); var redservice = container.resolve<ixyzservice>("redservice");
and each of instances use specified named dependency.
there should (different) way specify dependencies in castle windsor 2.5.3 version well, although haven't worked it, don't how achieve that.
Comments
Post a Comment