我正在研究使用 NHibernate 实现 IRepository 模式,我有一个问题,我无法在网上回答。
假设我有 3 个存储库,PersonRepository、PersonAddressRepository 和 PersonAccountRepository。现在假设业务逻辑规定存在调用 PersonRepository.Deactivate()、PersonAddressRepository.Deactivate() 和 PersonAccountRepository.Deactivate() 的“停用人员”流程。
我希望能够按照以下方式做一些事情。
using (ITransaction transaction = session.BeginTransaction()) {
session.Update(Person);
session.Update(PersonAddress);
session.Update(PersonAccount);
}
这样,如果其中任何一个更新失败,整个过程都会在数据库中回滚。现在我对 NHibernate 的理解是你只能为每个对象创建一个 Session 所以..
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Person).Assembly);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
using (ISession session = sessionFactory.OpenSession()) {
using (ITransaction transaction = session.BeginTransaction()) {
session.Save(Person);
}
这是正确的还是我错了?关于 NHibernate 的多表更新和事务的最佳实践是什么。
提前致谢。