0

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
  • 1
    When you resolve what? Commented Jul 25, 2017 at 16:08

1 Answer 1

1

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)));
Sign up to request clarification or add additional context in comments.

2 Comments

what if i have Service class using both Datacontext?
Also you can get all instances if you use IEnumerable<IUnitOfWork> alItems in the constructor. Also a word of warning I really liked unity IOC BUT Microsoft is doing a Silverlight with it, and it's a bit abandoned. So unless you are creating a Prism app I would not recommend using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.