Using read-only entities
By treating entities as read-only, we allow NHibernate to skip the memory and resource intensive dirty checking, which determines how and if an entity should be updated in the database. In Chapter 2, Models and Mappings, we learned how to configure the read-only behavior in a mapping. Here we'll see how the same thing can be accomplished programmatically, at runtime.
Getting ready
Complete the Getting Ready instructions at the beginning of Chapter 4, Queries.
How to do it...
Add a new folder named
ReadOnly
to theQueryRecipes
project.Create a class named
Recipe
in the folder:using NH4CookbookHelpers.Queries; using NH4CookbookHelpers.Queries.Model; using NHibernate; namespace QueryRecipes.ReadOnly { public class Recipe : QueryRecipe { private bool _readOnly=true; protected override void Run(ISessionFactory sessionFactory) { RunWithReadOnlySession(sessionFactory); RunWithQuery(sessionFactory); RunWithSetReadOnly(sessionFactory); ...