Using session.Refresh

Especially in desktop applications, it may be necessary to reload an entity to reflect recent changes made in a different session. In this recipe, we'll use session.Refresh to refresh an entity's data as it is being manipulated by two sessions.

Getting ready

Following Configuring NHibernate with App.config from Chapter 2, setup a console application for NHibernate with our Eg.Core model from Chapter 1.

How to do it...

  1. Add the following code to your Main method.
    var sessionA = sessionFactory.OpenSession();
    var sessionB = sessionFactory.OpenSession();
    
    Guid productId;
    Product productA;
    Product productB;
    
    productA = new Product()
    {
      Name = "Lawn Chair",
      Description = "Lime Green, Comfortable",
      UnitPrice = 10.00M
    };
    
    using (var tx = sessionA.BeginTransaction())
    {
      Console.WriteLine("Saving product.");
      productId = (Guid) sessionA.Save(productA);
      tx.Commit();
    }
    
    using (var tx = sessionB.BeginTransaction())
    {
      Console.WriteLine("Changing price.");
      productB = sessionB.Get<Product>(productId);
      productB.UnitPrice = 15.00M;
      tx.Commit();
    }
    
    using (var tx = sessionA.BeginTransaction())
    {
      Console.WriteLine("Price was {0:c}",
        productA.UnitPrice);
    
      sessionA.Refresh(productA);
    
      Console.WriteLine("Price is {0:c}", 
        productA.UnitPrice);
      tx.Commit();
    }
    
    sessionA.Close();
    sessionB.Close();
    
    Console.ReadKey();
  2. Run your application. You will see the following output:
    How to do it...

How it works...

In this contrived example, we open two sessions and manipulate two instances of the same entity. In session A, we save a newly created product – a $10 lime green lawn chair. Then in session B, we get the very same lawn chair. We now have two instances of the same entity. One is associated with session A and the other with session B.

We change the price of session B's lawn chair to $15. Notice that we don't call any method to save or update the database. Because session B loaded the lawn chair, it is tracking the changes and will automatically update the database when the session is flushed. This happens automatically when we commit the transaction. This is called automatic dirty checking. Session A's instance of lawn chair is still priced at $10.

When we call sessionA.Refresh, NHibernate will update session A's lawn chair with fresh data from the database. Now session A's lawn chair shows the new $15 price.

There's more...

session.Refresh is especially important in desktop applications, where we may have several sessions running simultaneously to handle multiple databound forms, and we want our saved changes on one form to be reflected immediately on another form displaying the same entity.

In this scenario, you will most likely set up some sort of message publishing between forms so that saving an entity on one form broadcasts an "I saved this entity" message to other forms displaying the same entity. When a form receives such a message, it calls session.Refresh to get the new data.

See also

  • Setting up session per presenter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.131.255