我正在尝试将我的域模型编写为尽可能持久的域模型。我现在唯一要做的就是标记每个属性和方法virtual
,因为NHibernate要求延迟加载。NHibernate ITransaction和纯粹域名模型
在我域模型组件我定义了一些库接口:
public interface IRepository<TEntity> where TEntity : EntityBase {
TEntity Get(int id);
/* ... */
}
public interface IProductRepository : IRepository<Product> { ... }
然后,我有一个数据汇编。这个会引用NHibernate,它知道它的存在。这是实现那些存储库接口的程序集:
public abstract class Repository<TEntity> : IRepository<TEntity> {
public TEntity Get(ind id) { ... }
/* ... */
}
public class ProductRepository : Repository<Product>, IProductRepository {
/* ... */
}
等等。
现在我想实现交易功能到我的资料库。为此,我会在我的IRepository接口上添加一个BeginTransaction
方法。但是,我无法将其返回类型定义为NHibernate.ITransaction
,因为我想保持领域模型持久化无知,并且不会被迫从我的领域模型组件中引用NHibernate的程序集。
你会怎么做?
你会简单地实现void BeginTransaction()
,一个void Commit()
,并且在接口上void RollBack()
方法,让库执行管理ITransaction
对象内部?
或者你会找到一种方法公开ITransaction
对象让客户直接用它来管理事务,而不是使用存储库的方法?
谢谢!