我有一个返回 UOW 的工厂
class UnitOfWorkFactory : IUnitOfWorkFactory
{
private UnitOfWork.Factory factory;
public ISession Session;
public IRepository Repository { get; set; }
public UnitOfWorkFactory(UnitOfWork.Factory factory,
ISession session, IRepository repository)
{
this.factory = factory;
this.Session = session;
Repository = repository;
}
public IUnitOfWork Create()
{
return factory(Session, Repository);
}
}
第一次调用工厂返回 UOW 的新实例。但是,在随后的调用中,它返回以前创建的实例,而不是新的。我使用说明开发人员“autofac”来使用委托工厂
public class UnitOfWork : IUnitOfWork
{
public ISession Session;
public IRepository Repository { get; set; }
private Transaction transaction;
public UnitOfWork(ISession session, IRepository repository)
{
this.Session = session;
Repository = repository;
transaction = Session.Transaction;
}
public delegate IUnitOfWork Factory(ISession transaction,
IRepository repository);
public void Dispose()
{
if (transaction.IsActive)
{
//Rollback
}
Session.Dispose();
}
public void Commit()
{
transaction.Commit();
}
}
注册到国际奥委会
builder.RegisterType<Repository>().As<IRepository>().InstancePerDependency();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWorkFactory>().As<IUnitOfWorkFactory>().InstancePerLifetimeScope();