简体   繁体   中英

Register multiple DbContext for IUnitOfWork using Unity IOC

I am using repository pattern with unit of work using IUnitOfWork via

https://github.com/ziyasal-archive/RepositoryT.EntityFramework/tree/master/RepositoryT.EntityFramework

sample IOC registration is given under https://github.com/ziyasal-archive/RepositoryT.EntityFramework/blob/master/RepositoryT.EntityFramework.AutofacConsoleSample/IoC.cs

When you have multiple DbContext in your project and you need to register IUnitOfWork how can you do correct registration with IoC? it seems to pick up the last registration for example

        Container.RegisterType<IUnitOfWork, EfUnitOfWork<Sample1DataContext>>(new ContainerControlledLifetimeManager());
        Container.RegisterType<IUnitOfWork, EfUnitOfWork<Sample2DataContext>>(new ContainerControlledLifetimeManager());

When i Resolve it will always return me Sample2DataContext

https://github.com/ziyasal-archive/RepositoryT.EntityFramework/issues/11

Unity will only let you have one "default" mapping. If you wish to map one "from" type ( IUnitOfWork ) to multiple "to" types ( EfUnitOfWork<Sample1DataContext> , EfUnitOfWork<Sample2DataContext> , ...) then you will need to use named registrations.

Container.RegisterType<IUnitOfWork, EfUnitOfWork<Sample1DataContext>>(
    typeof(Sample1DataContext).Name, new ContainerControlledLifetimeManager());
Container.RegisterType<IUnitOfWork, EfUnitOfWork<Sample2DataContext>>(
    typeof(Sample2DataContext).Name, new ContainerControlledLifetimeManager());

In this case I'm using typeof(Sample1DataContext).Name as the name of the registration.

Then when resolving, the name of the registration will need to be used to resolve the desired concrete type. For example to retrieve EfUnitOfWork<Sample1DataContext> :

Container.Resolve<IUnitOfWork>(typeof(Sample1DataContext).Name);

Usually IUnitOfWork will be a dependency for another type such as a service. For example to register an interface IService that maps to a concrete Service and that is dependent on IUnitOfWork and you wish to use the EfUnitOfWork<Sample2DataContext> type you could register similar to:

Container.RegisterType<IService, Service>(
    new InjectionConstructor(
        new ResolvedParameter<IUnitOfWork>(typeof(Sample2DataContext).Name)));

If you need to inject multiple IUnitOfWork instances for one service then just add the appropriate parameters to the InjectionConstructor. So if the constructor for Service was Service(IUnitOfWork data1Context, IUnitOfWork data2Context) you could do it like this:

Container.RegisterType<IService, Service>(
    new InjectionConstructor(
        new ResolvedParameter<IUnitOfWork>(typeof(Sample1DataContext).Name)),
        new ResolvedParameter<IUnitOfWork>(typeof(Sample2DataContext).Name)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM