Ejemplo n.º 1
0
 public RepositoryWriteInfrastructure(IDbContextFactory _factory, IRepositoryInfrastructureBase <TDbContext> _infrastructureBase, RepositoryOptions <TDbContext> _unitOfWorkOptions, IServiceProvider _serviceProvider)
 {
     masterDbContext    = _factory.GetMaster <TDbContext>();
     infrastructureBase = _infrastructureBase;
     unitOfWorkOptions  = _unitOfWorkOptions;
     serviceProvider    = _serviceProvider;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 切换主库的上下文
 /// </summary>
 /// <returns></returns>
 public Task SwitchMasterDbContextAsync()
 {
     if (!IsMaster && unitOfWorkOptions.IsOpenMasterSlave)
     {
         slaveDbContext = factory.GetMaster <TDbContext>();
     }
     IsMaster = true;
     return(Task.CompletedTask);
 }
Ejemplo n.º 3
0
 public RepositoryReadInfrastructure(IDbContextFactory _factory, IRepositoryInfrastructureBase <TDbContext> _infrastructureBase, RepositoryOptions <TDbContext> _unitOfWorkOptions)
 {
     factory            = _factory;
     infrastructureBase = _infrastructureBase;
     unitOfWorkOptions  = _unitOfWorkOptions;
     //验证是否开启了事务 如果开启了事务 就获取主库的连接
     //或者没有开启读写分离 直接使用 主库的数据
     if (_unitOfWorkOptions.IsBeginTran || !_unitOfWorkOptions.IsOpenMasterSlave)
     {
         slaveDbContext = _factory.GetMaster <TDbContext>();
         IsMaster       = true;
     }
     else
     {
         slaveDbContext = _factory.GetSlave <TDbContext>();
         _infrastructureBase.SwitchSlaveAsync(slaveDbContext).ConfigureAwait(false).GetAwaiter().GetResult();
     }
 }