开发者

NHibernate Get() followed by Flush or Commit?

开发者 https://www.devze.com 2023-02-26 15:36 出处:网络
My ISession object\'s FlushMode is FlushMode.Commit. I use the unit of work and repository pattern as defined here:

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))
      开发者_JS百科  {
            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.


NHibernate assumes that all database operations are done within transactions, so people use them explicitly instead of having NHibernate the 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消