Register multiple DbContext for IUnitOfWork using Unity IOC

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

Question

I am using the repository approach with an IUnitOfWork-based unit of work.

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

Under https://github.com/ziyasal-archive/RepositoryT.EntityFramework/blob/master/RepositoryT.EntityFramework.AutofacConsoleSample/IoC.cs, an example IOC registration is provided.

How do you correctly register an IUnitOfWork when your project has numerous DbContexts and you need to use IoC? For instance, it seems to get the most recent registration.

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

When I resolve, it always sends me a response. Sample2DataContext

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

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

Accepted Answer

You are only permitted to have one "default" mapping in Unity. To map a single "from" type (IUnitOfWork (to many "to" kindsEfUnitOfWork<Sample1DataContext> , EfUnitOfWork<Sample2DataContext> ,...), you must use named registrations.

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

Here, I'm usingtypeof(Sample1DataContext).Name as the registration's name.

After that, to determine the necessary concrete type, the name of the registration must be utilised. to fetch one exampleEfUnitOfWork<Sample1DataContext> :

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

Usually IUnitOfWork will be a need for a different category, like a service. To register an interface, for instanceIService It relates to a certainService Additionally, that depends onIUnitOfWork When you want to make advantage ofEfUnitOfWork<Sample2DataContext> you may register something like:

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

If you need many injectionsIUnitOfWork Simply add the necessary parameters to the InjectionConstructor if there are only instances needed for one service. Consequently, if the Service function Object() { [native code] } wasService(IUnitOfWork data1Context, IUnitOfWork data2Context) This is one way to go about it:

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