Register multiple DbContext for IUnitOfWork using Unity IOC

c# entity-framework entity-framework-6 repository-pattern unity-container

Question

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

1
0
7/25/2017 3:35:24 PM

Accepted Answer

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)));
1
7/25/2017 8:11:23 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow