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?
– Jéf Bueno
That, implementing his Idbcontext.
– Wilson Santos
Where does your
IUnitOfWork
is being used? Note thatFinanceiroDbContext
is already a Unitofwork. You should be careful what you are doing. See here why– Bruno Costa
How do you plan to 'differentiate' the multiple implementations of the Idbcontext interface? For example, assuming I have a class
Reposito
, with a builderReposito(IDbContext db)
, what context should be injected? It depends on what?– Genos
Why two contexts?
– Leonel Sanches da Silva
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
– Marcell Alves