Why should I use the Unit of Work template on top of an NHibernate session?

When will I write an UoW implementation on top of what has already been provided by NHibernate? Any real world examples?

+5
source share
3 answers

The unit of work that you describe is already provided by NHibernate, so there is no reason to do such a unit of work.

What we have in our WCF service is a higher-level work unit that contains important information for our application for the current unit of work. This includes abstracting ISIB NHN for us. When you break this, you have code that fits into three categories.

  • , . , . NHibernate, iBatis ORM. , , Load, Rollback, Save .. , .

  • , ISession , NHibernate . , .

  • , ISession. .

1. ISession, , . .

  • , NHibernate 100%. iBatis - . , .

  • NHibernate, , . , 1. , . 2. , , NHibernate.

, , , , , . , .

+5

- - - - IDisposable.Dispose

. , . ( , , ..)

+1

(.. ), , ISession . , , , . Fowler " ":

class UnitOfWork... 

   public void registerNew(DomainObject obj) {
      Assert.notNull("id not null", obj.getId());
      Assert.isTrue("object not dirty", !dirtyObjects.contains(obj));
      Assert.isTrue("object not removed", !removedObjects.contains(obj));
      Assert.isTrue("object not already registered new", !newObjects.contains(obj));
      newObjects.add(obj);
   }
   public void registerDirty(DomainObject obj) {
      Assert.notNull("id not null", obj.getId());
      Assert.isTrue("object not removed", !removedObjects.contains(obj));
      if (!dirtyObjects.contains(obj) && !newObjects.contains(obj)) {
         dirtyObjects.add(obj);
      }
   }
   public void registerRemoved(DomainObject obj) {
      Assert.notNull("id not null", obj.getId());
      if (newObjects.remove(obj)) return;
      dirtyObjects.remove(obj);
      if (!removedObjects.contains(obj)) {
         removedObjects.add(obj);
      }
   }
   public void registerClean(DomainObject obj) {
      Assert.notNull("id not null", obj.getId());
   }
0

All Articles