使用以下类似于使用工作单元模式

时间:2011-10-07 07:47:11

标签: nhibernate fluent-nhibernate unit-of-work

使用nHiberbnate会话/事务组合是否执行与工作单元模式相同的功能?下面是网上发现的一些代码..

using (var session = sessionFactory.OpenSession())
  {
    using (var transaction = session.BeginTransaction())
    {
      // create a couple of Stores each with some Products and Employees
      var barginBasin = new Store { Name = "Bargin Basin" };
      var superMart = new Store { Name = "SuperMart" };

      var potatoes = new Product { Name = "Potatoes", Price = 3.60 };
      var fish = new Product { Name = "Fish", Price = 4.49 };
      var milk = new Product { Name = "Milk", Price = 0.79 };
      var bread = new Product { Name = "Bread", Price = 1.29 };
      var cheese = new Product { Name = "Cheese", Price = 2.10 };
      var waffles = new Product { Name = "Waffles", Price = 2.41 };

      var daisy = new Employee { FirstName = "Daisy", LastName = "Harrison" };
      var jack = new Employee { FirstName = "Jack", LastName = "Torrance" };
      var sue = new Employee { FirstName = "Sue", LastName = "Walkters" };
      var bill = new Employee { FirstName = "Bill", LastName = "Taft" };
      var joan = new Employee { FirstName = "Joan", LastName = "Pope" };

      // add products to the stores, there's some crossover in the products in each
      // store, because the store-product relationship is many-to-many
      AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese);
      AddProductsToStore(superMart, bread, cheese, waffles);

      // add employees to the stores, this relationship is a one-to-many, so one
      // employee can only work at one store at a time
      AddEmployeesToStore(barginBasin, daisy, jack, sue);
      AddEmployeesToStore(superMart, bill, joan);

      // save both stores, this saves everything else via cascading
      session.SaveOrUpdate(barginBasin);
      session.SaveOrUpdate(superMart);

      transaction.Commit();
    }
  }
}

2 个答案:

答案 0 :(得分:0)

不一定。 unitofwork.Start()可能包含以下内容

  1. 公开会话
  2. 进行一些初始化(例如,添加过滤器)
  3. 开始交易
  4. unitofwork.end()可能包含以下内容

    1.提交或回滚交易  2.结束会议

答案 1 :(得分:0)

Martin Fowler:

维护受业务事务影响的对象列表,并协调写入更改和解决并发问题。

是的,NH会议是一个工作单元的例子。