Introduction to Basic Interfaces
Interfaces called by applications to perform basic CRUD and querying operations (that is: Create, Retrieve, Update and Delete). These interfaces are the main point of dependency of application business/control logic on NHibernate.
Five basic Interfaces used as shown in the figure 1.1; where IQuery and ICriteria performs the same operation.

Figure 1.1 Five Basic Interfaces in NHibernate
ISession interface
1. The ISession interface is the primary interface used by NHibernate applications, it exposes NHibernates methods for finding, saving, updating and deleting objects.
2. An instance of ISession is lightweight and is inexpensive to create and destroy. This is important because your application will need to create and destroy sessions all the time, perhaps on every ASP.NET page request. NHibernate sessions are not thread safe and should by design be used by only one thread at a time. The NHibernate notion of a session is something between connection and transaction.
3. It may be easier to think of a session as a cache or collection of loaded objects relating to a single unit of work. NHibernate can detect changes to the objects in this unit of work.
4. We sometimes call the ISession a persistence manager because it's also the interface for persistence-related operations such as storing and retrieving objects. Note that a NHibernate session has nothing to do with an ASP.NET session. When we use the word session in this book, we mean the NHibernate session.
ISessionFactory interface
1. The application obtains ISession instances from an ISessionFactory. Compared to the ISession interface, this object is much less exciting.
2. The ISessionFactory is certainly not lightweight! It's intended to be shared among many application threads. There is typically a single instance of ISessionFactory for the whole application-created during application initialization, for example. However, if your application accesses multiple databases using NHibernate, you'll need a SessionFactory for each database.
3. The SessionFactory caches generated SQL statements and other mapping metadata that NHibernate uses at runtime.
4. It can also hold cached data that has been read in one unit of work, and which may be reused in a future unit of work or session. This is possible if you configure class and collection mappings to use the second-level cache.
ITransaction interface
1. The ITransaction interface, next to the ISession interface. The ITransaction interface is an optional API. NHibernate applications may choose not to use this interface, instead managing transactions in their own infrastructure code.
2. A NHibernate ITransaction abstracts application code from the underlying transaction implementation-which might be an ADO.NET transaction or any kind of manual transaction-allowing the application to control transaction boundaries via a consistent API. This helps to keep NHibernate applications portable between different kinds of execution environments and containers.
IQuery and ICriteria interfaces
1. The IQuery interface gives you powerful ways of performing queries against the database, whilst also controlling how the query is executed.
2. It is the basic interface used for fetching data using NHibernate. Queries are written in HQL or in the native SQL dialect of your database. An IQuery instance is lightweight and can't be used outside the ISession that created it. It is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.
A. The ICriteria interface is very similar; it allows you to create and execute object-oriented criteria queries.
Interfaces Used for Storing Values
Suppose that our sellers can attach images to Items. An image is accessible only via the containing item; it doesn't need to support associations to any other entity in our system. In this case, it's reasonable to model the image as a value type. Item would have a collection of images that NHibernate would consider to be part of the Item, and therefore without their own persistence lifecycle.


Join the conversation! Your thoughts help the community grow.