Sunday, 21 April 2013

Managing Configuration and ISessionFactory Access with TinyNH, the Simple NHibernate Infrastructure

TinyNH is a demo project and series of articles designed to help you get your project up and running with a simple solid NHibernate persistence infrastructure. See contents for a full list of articles.


So far, we've created some domain model entity classes and added a ConfigurationBuilder class to configure NHibernate.

To start working with your objects, you first need to create your Configuration, then use it to build an ISessionFactory. In a really simple application, like the TinyNH ProductImporter console application, a couple of lines of code is sufficient:

Look at ProductImporter/Program.cs on github if you can't see the code in-line above.

However, there are a number of important points to consider if you are using NHibernate in the context of a web application or any other long-running service:
  • Configuration and ISessionFactory instances are expensive to create and should only be created once during the lifetime of your application.
  • We need to ensure that Configuration and ISessionFactory initialisation happens only once and in a thread-safe manner, even in a multi-threaded environment such as a web application.
  • Initialisation can fail due to database outages. Specifically Configuration.BuildSessionFactory will fail if the application cannot connect to the database. Do you want your web site to become unavailable, just because the database happened to be down for a couple of seconds, coinciding with when your application started up? 

The TinyNH ConfigurationStore is designed to take care of the above concerns.

Here it is:

Look at ConfigurationStore.cs on github if you can't see the code in-line above.

ThreadSafeInitializer is a utility class that is used to initialise the Configuration and ISessionFactory in a thread-safe manner. It is similar to System.Lazy but it will retry initialisation if a previous attempt fails - see my article about System.Lazy's exception caching features for background.

How to do it

If you need it, add this functionality to your solution as follows:

  1. Add the ConfigurationStore.cs class to your solution, within the same namespace as the ConfigurationBuilder class described in the previous article.
  2. Add ThreadSafeInitializer.cs to a suitable location in your solution. You may have a "slot" used for common utility classes or you could add it to the same namespace as ConfigurationStore.
  3. Add some code that runs during start-up of your application to initialise your ConfigurationStore.

In our example web application (TinyNH.DemoStore.Admin project) we do this in Global.asax. In keeping with our super-simple approach, we make the ConfigurationStore available as a public property of our Global application class.

Look at Global.asax.cs on github if you can't see the code in-line above.

You might shudder at the thought of accessing dependencies via static properties and prefer to use an IoC container to manage dependencies between objects. The ConfigurationStore is still a useful mechanism to ensure thread-safe initialisation of your Configuration / ISessionFactory during application start-up and can be used to provide the instances to your IoC container.

No comments:

Post a Comment