c# - Register multiple DbContext for IUnitOfWork using Unity IOC -
i using repository pattern unit of work using iunitofwork via
sample ioc registration given under https://github.com/ziyasal-archive/repositoryt.entityframework/blob/master/repositoryt.entityframework.autofacconsolesample/ioc.cs
when have multiple dbcontext in project , need register iunitofwork how can correct registration ioc? seems pick last registration example
container.registertype<iunitofwork, efunitofwork<sample1datacontext>>(new containercontrolledlifetimemanager()); container.registertype<iunitofwork, efunitofwork<sample2datacontext>>(new containercontrolledlifetimemanager());
when resolve return me sample2datacontext
https://github.com/ziyasal-archive/repositoryt.entityframework/issues/11
unity let have 1 "default" mapping. if wish map 1 "from" type (iunitofwork
) multiple "to" types (efunitofwork<sample1datacontext>
, efunitofwork<sample2datacontext>
, ...) need use named registrations.
container.registertype<iunitofwork, efunitofwork<sample1datacontext>>( typeof(sample1datacontext).name, new containercontrolledlifetimemanager()); container.registertype<iunitofwork, efunitofwork<sample2datacontext>>( typeof(sample2datacontext).name, new containercontrolledlifetimemanager());
in case i'm using typeof(sample1datacontext).name
name of registration.
then when resolving, name of registration need used resolve desired concrete type. example retrieve efunitofwork<sample1datacontext>
:
container.resolve<iunitofwork>(typeof(sample1datacontext).name);
usually iunitofwork
dependency type such service. example register interface iservice
maps concrete service
, dependent on iunitofwork
, wish use efunitofwork<sample2datacontext>
type register similar to:
container.registertype<iservice, service>( new injectionconstructor( new resolvedparameter<iunitofwork>(typeof(sample2datacontext).name)));
if need inject multiple iunitofwork
instances 1 service add appropriate parameters injectionconstructor. if constructor service service(iunitofwork data1context, iunitofwork data2context)
this:
container.registertype<iservice, service>( new injectionconstructor( new resolvedparameter<iunitofwork>(typeof(sample1datacontext).name)), new resolvedparameter<iunitofwork>(typeof(sample2datacontext).name)));
Comments
Post a Comment