The NHibernate session architecture

We looked at configuration, sessionFactory, and session among other things in the previous section. These three components encapsulate most of the functionality offered by NH. I do not mean this in literal sense. If you look at NHibernate source code, you will see that a lot of NHibernate features are implemented independently of these components. But these components are gateways into using these features.

Following diagram shows how these three relate to each other:

The NHibernate session architecture

The preceding diagram represents components of a real life application. Layers above Data Access Layer are represented in an abstract way, combining everything into one layer. In Data Access Layer, you have got Configuration as one of the components. Configuration knows about mappings available in Data Access Layer. It also knows about lot of other details that are used either during warming up NHibernate or interacting with Database.

As we have seen earlier, calling the BuildSessionFactory method on the configuration object gives us a sessionFactory. Session factory, as the name suggests is factory of session objects. But more than that, Session Factory is an abstraction of the underlying database of your application. It knows everything about the underlying database and can open new sessions which can be used to perform operations against database. If your application needs to connect to more than one database then you should use more than one instance of session factory, one each for every database. Building a session factory is an involved operation and usually takes time. It is recommended that you create no more than one instance of session factory per database that your application connects to. Session factory once created, has no knowledge of configuration object. If you change any configuration options then the session factory has to be rebuilt, if changed configuration options are to be considered.

Session factory generates sessions on request. Any interaction with the database that you need to do needs to be done against session. Session is your gateway into databases. Session object comes loaded with multitude of APIs that you can use to store and retrieve data from database. Chapter 5, Let's Store Some Data into the Database and Chapter 6, Let's Retrieve Some Data from the Database will primarily deal with these aspects in detail so we will not talk much about them in this chapter. We have seen in the beginning of this chapter that you can call the OpenSession method on session factory to get a new instance of session object. That is just one of the ways that you can get hold of a session. Session factory has few overrides of the OpenSession method along with some other methods that you can use to get hold of session. This makes it possible to open session in a variety of different ways, for a variety of different situations. We would examine some of the important ways of obtaining the session object in this section.

Note

This is not complete NHibernate architecture. The preceding diagram only depicts what we are going to learn in this chapter. As we dig deeper, we will keep on building this diagram to get a more complete picture of NHibernate architecture.

The code we walked through at the beginning of this chapter achieved configuration all through code. This is called programmatic configuration. NHibernate traditionally has offered configuration through XML as well. And if you are using FNH then it offers its own API for configuration of NHibernate fluently. We are not going to use XML configuration in this book but I do want to cover it briefly for the sake of completeness. Next, we will look at XML, loquacious and fluent configuration briefly before we go into details of important configuration options.

..................Content has been hidden....................

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