我正在使用工作单元模式进行过渡。我创建了一个DbContext来处理连接,提交,回滚工作。
在数据访问和业务层类中,我通过构造函数注入IDbContext。
public TestDAL(IDbContext dbContext) : base(dbContext) {}
public TestService(IDbContext dbContext, ITestDAL testDAL) {}
我从autofac移开了简单的进样器。我对生活方式不匹配感到异常。
-[生活方式不匹配] TestService(异步作用域)取决于DbContext(瞬态)实现的IDbContext。
-[生活方式不匹配] TestDAL(异步作用域)取决于DbContext(瞬态)实现的IDbContext。
我将DAL和Service类注册为作用域,并将DbContext注册为瞬态。
var ServiceRegistrations =
from type in Assembly.GetExecutingAssembly().GetTypes()
where type.Name.EndsWith("Service") && type.IsClass
from service in type.GetInterfaces()
select new { service, type };
foreach (var reg in ServiceRegistrations)
{
container.Register(reg.service, reg.type, Lifestyle.Scoped);
}
container.Register<IDbContext, DbContext>(Lifestyle.Transient);
我阅读了一些有关DbContext的信息,这些信息应该是瞬态的,它不是线程安全的。我也认为创建瞬态不需要DAL和Service类。我该如何解决这种情况?顺便说一句,这在autofac上不是问题。