我有一个通用的UnitOfWork模式实现这些的UnitOfWork对象是依赖于我的服务类。 下面的片段应该帮助读者了解我的代码设置:
IUnitOfWork接口
public interface IUnitOfWork<out TContext> where TContext : IDbContext
的UnitOfWork类
public sealed class UnitOfWork<TContext> : IDisposable, IUnitOfWork<IDbContext> where TContext : IDbContext
{
private static readonly ILog Log = LogManager.GetLogger(typeof(UnitOfWork<TContext>));
private readonly IDbContext _dbContext;
private Dictionary<string, IRepository> _repositories;
private IDbTransaction Transaction { get; set; }
public UnitOfWork(IDbContext context)
{
_dbContext = context;
}
}
容器注册:
builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>));
builder.RegisterType<ReconciliationDbContext>().As<IDbContext>();
builder.RegisterType<GenevaDataDbContext>().As<IDbContext>();
builder.RegisterType<OpenStaarsDbContext>().As<IDbContext>();
builder.RegisterType<UnitOfWork<ReconciliationDbContext>>().Keyed<IUnitOfWork<IDbContext>>(ContextKey.Recon);
builder.RegisterType<UnitOfWork<OpenStaarsDbContext>>().Keyed<IUnitOfWork<IDbContext>>(ContextKey.OpenStaars);
builder.RegisterType<CommentsService>().As<ICommentsService>().WithAttributeFiltering();
的DbContext类:
public class ReconciliationDbContext : BaseDbContext<ReconciliationDbContext>, IDbContext
{
private const string DbSchema = "BoxedPosition";
public ReconciliationDbContext() : base("Reconciliation")
{
}
}
public class OpenStaarsDbContext : BaseDbContext<OpenStaarsDbContext>, IDbContext
{
public OpenStaarsDbContext() : base("OpenStaars")
{
}
}
CommentsService类:
public class CommentsService : ICommentsService
{
private readonly IUnitOfWork<IDbContext> _reconciliationUoW;
public CommentsService([KeyFilter(ContextKey.Recon)] IUnitOfWork<IDbContext> reconciliationUoW)
{
_reconciliationUoW = reconciliationUoW;
}
}
解决ICommentsService:
var commentsService = container.Resolve<ICommentsService>();
现在,当我试图解决ICommentsService类型,实例化的UnitOfWork依赖。 然而,UnitOfWork._dbContext属性值将为OpenStaarsDbContext类型。 这是特别奇怪考虑到我们的注册。
它变成如果我们通过OpenStaarsDbContext后注册GenevaDataDbContext重新整理我们的IDbContext登记的更奇怪。 现在_dbContext计算结果为GenevaDataDbContext实例。
我怎样才能解决这个问题,使CommentsService的reconciliationUoW依赖有ReconciliationDbContext的正确实例?