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

Follow the Getting ready step in the Save entities to the database recipe in this chapter.

How to do it…

  1. Add a new folder named SessionRefresh to the SessionRecipes project.
  2. Add a class named Recipe to the folder:
    using System;
    using NH4CookbookHelpers;
    using NH4CookbookHelpers.Queries.Model;
    using NHibernate;
    
    namespace SessionRecipes.SessionRefresh
    {
      public class Recipe : QueryRecipe
      {
        protected override void Run(ISessionFactory sessionFactory)
        {
          using (var sessionA = sessionFactory.OpenSession())
          using (var sessionB = sessionFactory.OpenSession())
          {
    
            int productId;
    
            var productA = new Product()
            {
              Name = "Lawn Chair",
              Description = "Lime Green, Comfortable",
              UnitPrice = 10.00M
            };
    
            using (var tx = sessionA.BeginTransaction())
            {
              Console.WriteLine("Saving product in session A.");
              productId = (int)sessionA.Save(productA);
              tx.Commit();
            }
    
            using (var tx = sessionB.BeginTransaction())
            {
              Console.WriteLine("Changing price in session B.");
              var productB = sessionB.Get<Product>(productId);
              productB.UnitPrice = 15.00M;
              tx.Commit();
            }
    
            using (var tx = sessionA.BeginTransaction())
            {
              Console.WriteLine("Price was {0:c}",
                productA.UnitPrice);
    
              Console.WriteLine("Refreshing");
    
              sessionA.Refresh(productA);
    
              Console.WriteLine("Price is {0:c}",
                productA.UnitPrice);
              tx.Commit();
            }
          }
        }
      }
    }
  3. Run your application and start the SessionRefresh recipe. 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.

A session.Refresh will also cascade to associated entities, if the association's mapping has cascade="refresh" specified (it's implied by cascade="all"). In that case, Refresh might trigger multiple database queries!

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
52.15.80.101