Simple Injector + Uow + DDD + Multiple Contexts + Entity Framework

Asked

Viewed 809 times

8

I need to know how to apply Ioc to two contexts.

The scenario is as follows:

I have a Layer called Core (allocates classes that I can reuse in other layers), where I put the interface of Idbcontext, Iunitofwork and Unitofwork (among other classes that are not giving me problems for now).

I have two contexts (Administrativodbcontext and Financeirodbcontext), separated into different layers with their respective sets of Dbset, because I have no need to expose some tables for unrelated contexts.

Unit of Work:

public class UnitOfWork : IUnitOfWork
{

    private readonly IDbContext _dbContext;

    public UnitOfWork(IDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    //... restante da classe...

}

So I need to implement separate Ioc in their respective contexts that are also separate:

Administrative Backend Layer:

public class AdministrativoBootstrap 
{
    public static void RegisterServices(Container container)
    {
        container.RegisterPerWebRequest<IDbContext, AdministrativoDbContext>();
        container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
    }
}

Financial Infrastructure Layer:

public class FinanceiroBootstrap 
{
    public static void RegisterServices(Container container)
    {
        container.RegisterPerWebRequest<IDbContext, FinanceiroDbContext>();
        container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
    }
}

After configuring, I initialize them in the Application (MVC):

public class SimpleInjectorInitializer
{
    public static void Initialize()
    {
          InitializeContainer(container);  
          container.Verify();
    }
}


private static void InitializeContainer(Container container)
{
    AdministrativoBootstrap.RegisterServices(container);
    FinanceiroBootstrap.RegisterServices(container);
}

This triggers an "ambiguous relation" error, where there can be no two classes implementing the same interface (Idbcontext).

I used this option inside the initializer:

container.Options.AllowOverridingRegistrations = true;

However, when I see the object in Debug, it does not have the Dbset of the first context (Administrativodbcontexts), because it overwrites, since sequentially it calls the excerpt Financeirobootstrap.Registerservices(container);.

I believe the problem is to use the same Unit of Work, but in this question someone replies that there can only be one.

Anyone who can help me thank me immensely.

  • You’re using Entity Framework, right?

  • That, implementing his Idbcontext.

  • 2

    Where does your IUnitOfWork is being used? Note that FinanceiroDbContext is already a Unitofwork. You should be careful what you are doing. See here why

  • How do you plan to 'differentiate' the multiple implementations of the Idbcontext interface? For example, assuming I have a class Reposito, with a builder Reposito(IDbContext db), what context should be injected? It depends on what?

  • Why two contexts?

  • In the case of an application that follows the DDD, it is interesting to maintain a context for each bounded context: https://msdn.microsoft.com/en-us/magazine/jj883952.aspx

Show 1 more comment

1 answer

1

A solution to your problem would be to create an interface and a Unitofwork class for each context. In this way, it eliminates the problem of ambiguous relationship. In the case of context class mapping, you can omit the reference to the Idbcontext interface, the code would look like this:

container.RegisterPerWebRequest<AdministrativoDbContext>();

and

container.RegisterPerWebRequest<FinanceiroDbContext>();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.