NHibernate commit db changes without explict call to save or update

后端 未结 3 1634
太阳男子
太阳男子 2020-12-16 23:54

I use Nhibernate 2.0 in ASP.NET. I start the transaction at the begging of the page and commit the transaction at the end. During the page: - I get an object - I change the

3条回答
  •  忘掉有多难
    2020-12-17 00:27

    During the page: - I get an object

    If you get an object from a session then you are misunderstanding Update. Update is for attaching an existing entity to a session. If you get an entity from a session it is already attached to that session so Update is meaningless.

    SaveOrUpdate vs. Update in this case doesn't matter -- same thing.

    NHibernate tracks changes to the object in session. When you commit a transaction or flush a session it is going to check for any changes (which there are) and then commit those to the database. The whole point of this is that it isn't your job to track which objects are changed (dirty), it is NHibernates.

    Other ORM may require that you track the changes yourself and call some kind of Update explicitly on any object changed that you want to persist, but NH doesn't work that way.

    So to answer your question, if validation fails you don't want to commit the transaction.

    NH is also opinionated towards the Unit of Work pattern. So if you do the commit in a different logical part of the program from your business logic that is validating work, it probably will cause friction.

提交回复
热议问题