简体   繁体   English

使用Unity IOC为IUnitOfWork注册多个DbContext

[英]Register multiple DbContext for IUnitOfWork using Unity IOC

I am using repository pattern with unit of work using IUnitOfWork via 我通过IUnitOfWork通过工作单元使用存储库模式

https://github.com/ziyasal-archive/RepositoryT.EntityFramework/tree/master/RepositoryT.EntityFramework 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 示例IOC注册在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? 如果项目中有多个DbContext,并且需要注册IUnitOfWork,如何在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 当我解决时,它将始终返回我Sample2DataContext

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

Unity will only let you have one "default" mapping. Unity仅会让您拥有一个“默认”映射。 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. 如果希望将一个“从”类型( IUnitOfWork )映射到多个“到”类型( EfUnitOfWork<Sample1DataContext>EfUnitOfWork<Sample2DataContext> ,...),那么您将需要使用命名注册。

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. 在这种情况下,我使用typeof(Sample1DataContext).Name作为注册名称。

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> : 例如,检索EfUnitOfWork<Sample1DataContext>

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

Usually IUnitOfWork will be a dependency for another type such as a service. 通常, IUnitOfWork将是另一种类型(例如服务)的依赖项。 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: 例如,要注册一个映射到具体Service且依赖于IUnitOfWork的接口IService ,并且您希望使用EfUnitOfWork<Sample2DataContext>类型,可以类似于以下内容进行注册:

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. 如果您需要为一个服务注入多个IUnitOfWork实例,则只需将适当的参数添加到InjectionConstructor中即可。 So if the constructor for Service was Service(IUnitOfWork data1Context, IUnitOfWork data2Context) you could do it like this: 因此,如果Service的构造函数是Service(IUnitOfWork data1Context, IUnitOfWork data2Context) ,则可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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