c# - ServiceStack multiple implementations of same interface in Funq.Container -
in servicestack application, have funq configured inject session per request this:
container.register<nhibernate.isessionfactory>(sessionfactoryfordb1); container.register<nhibernate.isession>(c => c.resolve<nhibernate.isessionfactory>() .opensession()) .reusedwithin(funq.reusescope.request);
my service looks this, , works fine:
public class mynhawareservice : service { public isession session { get; set; } public object any(dosomething request) { ... } }
now, problem comes in when want add second nhibernate database mix own session factory:
container.register<nhibernate.isessionfactory>(sessionfactoryfordb1); container.register<nhibernate.isession>(c => c.resolve<nhibernate.isessionfactory>() .opensession()) .reusedwithin(funq.reusescope.request); // add different session factory container.register<nhibernate.isessionfactory>(sessionfactoryfordb2);
i've been experimenting variety of ways funq can used, , thought had found way forward when discovered 'registernamed()" method, still doesn't help, can't use except tryresolve() within service.
this seems should possible, i'm beating head against wall trying work out...any advice appreciated.
you have couple ways of going this.
option 1: unique interfaces
this option create distinct interface each nhibernate database can uniquely resolved funq.
for example:
interface factorya : nhibernate.isessionfactory { } interface factoryb : nhibernate.isessionfactory { }
you proceed now. same applies session. see here little more detail process:
how register multiple idbconnectionfactory instances using funq in servicestack.net
option 2: named instance
this option less familiar with, can name instances using funq:
container.register<nhibernate.isessionfactory>("factorya",sessionfactoryfordb1);
and in service, resolve thusly:
servicestackhost.instance.container.resolvenamed<nhibernate.isessionfactory>("factorya");
this option uses service location, find less attractive.
Comments
Post a Comment