NHibernate Get() 后跟 Flush 还是 Commit?
我的 ISession 对象的 FlushMode 是 FlushMode.Commit。
我使用此处定义的工作单元和存储库模式: http://nhforge.org/wikis/patternsandpractices /nhibernate-and-the-unit-of-work-pattern.aspx
我记得看到过一些例子,其中有些人调用 Get() 后立即调用 Flush 或事务 犯罪。我们是他们疯了,还是有理由这样做?
根据我的测试:
[TestMethod]
public void TestMethod1()
{
Employee e;
IRepository<Employee> empRepo;
using(UnitOfWork.Start(Enums.Databases.MyDatabase))
{
empRepo = new Repository<Employee>();
e = empRepo.GetByID(21);
}
Debug.WriteLine(e.UserName);
}
我的 GetByID 存储库函数仅调用 Session.Get(id),我可以在输出窗口中查看用户名(会话被终止后)...那么在得到() ?如果那里有保存的话我会理解。
My ISession object's FlushMode is FlushMode.Commit.
I use the unit of work and repository pattern as defined here:
http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx
I recall seeing some examples where some people call a Get() immediately followed by a Flush or a transaction commit. We're they just off their rocker, or is there a reason to do this?
From my test:
[TestMethod]
public void TestMethod1()
{
Employee e;
IRepository<Employee> empRepo;
using(UnitOfWork.Start(Enums.Databases.MyDatabase))
{
empRepo = new Repository<Employee>();
e = empRepo.GetByID(21);
}
Debug.WriteLine(e.UserName);
}
My GetByID repository function just calls Session.Get(id) and I can view the username in the output window (after the session is killed)... so whats the point of any sort of Flush or transaction commit after a Get() ? I would understand if there was a save in there somewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NHibernate 假定所有数据库操作都是在事务内完成的,因此人们显式地使用它们,而不是让
NHibernateRDBMS 隐式地使用它们。Ayende 在他的文章 NH 教授警报:不鼓励使用隐式事务。
编辑:今天学到了一些新东西。使用隐式事务的不是 NHibernate,而是 DB。
NHibernate assumes that all database operations are done within transactions, so people use them explicitly instead of having
NHibernatethe RDBMS use them implicitly.Ayende explains this in more detail in his post NH Prof Alerts: Use of implicit transactions is discouraged.
Edit: Learned something new today. It's not NHibernate using implicit transactions but the DB.