The NHibernate framework uses the abstract factory pattern (see http://en.wikipedia.org/wiki/Abstract_factory_pattern) for creating sessions, and this factory is created from a Configuration
object.
The following line of code builds a Session
Factory
object from our configuration
(cfg
) object that we'll use to create sessions:
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
From now on, when we want to create a session, we just ask the session
factory
to open a session for us as follows:
ISession session = sessionFactory.OpenSession();
In addition to opening the session, we want to wrap our statements in a "transaction" to decrease database overhead. I know what you are thinking, wouldn't creating a transaction for every statement actually INCREASE database overhead? In reality, the database already uses implicit transactions for every call we make, so by explicitly telling it to create a single transaction for all of our operations, we are actually reducing...