📜 ⬆️ ⬇️

Basics of NHibernate. Part 1

Not so long ago, fell under the hands of a new project. Until now, basically, I had to finish the old ones. The project was supposed to use the database. Googling decided a little to abandon the old methods of working with data in favor of ORM. Yes, there are many code generators (for example, CodeSmith), which in a few seconds will create a level of access to data, but such solutions are not very flexible, and with further development they threaten to turn into a nightmare. Although ORM also has its drawbacks. But first things first. Now I want to share with you my experience in mastering one of the representatives of the world of ORM - NHibernate. Why from all possible ORMs I chose to study NHibernate? Firstly, because it was necessary to choose one thing. Secondly, the history of NHibernate is deeply rooted in the Hibernate ORM framework for Java and is a fairly mature solution. More so far, it seems, and there are no arguments, but I think they will appear later on closer acquaintance with NHibernate.

The main task of ORM (Object-Relational Mapping) is to be the connecting link that will transparently do all the work for us to save the data of our business objects and fill them with data from the database if necessary. In addition, ORM eliminates the need for a large number of the same type of primitive code that performs only CRUD operations (Create, Read, Update, Delete). This is of course the simplest explanation of the ORM meaning, but I think the idea is clear. More can be read here .

Before we start working we need the library itself. While we are talking about version 1.2.1, unless otherwise indicated. Most recently, the second version of the framework in which promised many goodies, but again, we will not rush things;). Archive with all necessary can be downloaded here . Having unpacked the archive, you will see a lot of files, but at first, not all of them will be needed. The most important and necessary file for us will be NHibernate.dll. This is still the only library, the reference for which you want to add to the project. Iesi.Collections.dll, Castle.DynamicProxy.dll and log4net.dll will also be needed, but there is no need to add them, NHibernate.dll will load them
if necessary.

To understand the basics of NHibernate, we will create a simple class, configure our application and try to create an instance of the class, save it in the database and get it back.
')
First, create a simple Book class:

public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  1. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  2. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  3. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  4. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  5. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  6. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  7. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  8. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  9. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  10. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  11. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  12. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  13. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  14. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  15. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  16. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  17. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  18. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  19. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  20. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  21. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  22. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  23. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  24. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  25. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  26. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  27. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  28. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
  29. public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .
public class Book { private long id; private string isbn; private string title; public virtual long ID { get { return id; } protected set { id = value ; } } public virtual string ISBN { get { return isbn; } set { isbn = value ; } } public virtual string Title { get { return title; } set { title = value ; } } public Book() { } public Book( string bookIsbn, string bookTitle) { isbn = bookIsbn; title = bookTitle; } } * This source code was highlighted with Source Code Highlighter .


Immediately, I note a feature of NHibernate: it does not require any additional efforts to implement a specific interface or inherit from the base class. Those. describing the objects of business logic, you can not think about the implementation of any mechanisms for interacting with the database. NHibernate will do all the dirty work. But whatever omnipotent he is, he still needs to get information from where about classes, their fields and properties. There are two ways to provide this information. The first is to describe all the necessary information in the class attributes, its fields and properties, the second is to set all the necessary correspondences between the class, its data and the table in the database in the mapping file. I think the second method is more convenient and intuitive (separating flies from cutlets;)).

o_O I know that this is a subjective opinion, but it's easier for me. In addition, there are tools to facilitate and automate it.
About them will be discussed in the continuation of the series.

When writing or editing the mapping manually, I don’t want to refer to the reference book every time to recall the necessary attribute or element. VS and xsd-schemes for configuration and mapping of files will help us in this. They are in the previously downloaded archive. These are nhibernate-configuration-2.0.xsd, nhibernate-mapping-2.0.xsd, and nhibernate-generic.xsd. Copy them to the [Path To Microsoft Visual Studio Folder] \ Xml \ Schemas folder and restart the studio. Everything, you can use Intellisense when creating mapping-files.


A mapping file might look something like this:
<? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  1. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  2. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  3. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  4. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  5. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  6. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  7. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  8. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  9. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  10. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
  11. <? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? > < hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate_1" assembly ="NHibernate_1" > < class name ="Book" table ="Books" > < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id > < property name ="ISBN" /> < property name ="Title" /> </ class > </ hibernate-mapping > * This source code was highlighted with Source Code Highlighter .


  1. < class name ="Book" table ="Books" >

Here we describe a class which mapim and a table in the database in which data will be stored.

  1. < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id >
  2. < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id >
  3. < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id >
  4. < id name ="ID" unsaved-value ="0" > < column name ="ID" not-null ="true" /> < generator class ="identity" /> </ id >

Here is the identifier. As an identifier generator, the Identity class is used, which is in SQL Serrver. You can use some other generators, and also write the class itself that implements the NHibernate.Id.IIdentifierGenerator interface.

  1. < property name = "ISBN" />
  2. < property name = "Title" />

Well, here we describe all the other properties of our class.

To begin with, I think that will be enough. More information about mapping files will be discussed later. By tradition, the name of the file is given the name of the class_name.hbm.xml, but this is not a requirement. Next, do not forget to specify in the properties of this xml file Build Action = Embedded Resource so that the file is included in the final assembly.

The next step is to connect to the database, where we will store the data. We need to create a configuration file for NHibernate. In our first example, it will be extremely simple, but still sufficient to enable it to fully work with the database.

It looks like this:
<? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  1. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  2. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  3. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  4. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  5. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  6. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  7. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  8. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  9. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
  10. <? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .
<? xml version ='1.0' encoding ='utf-8' ? > < hibernate-configuration > < session-factory xmlns ="urn:nhibernate-configuration-2.2" > < property name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property > < property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property > < property name ="dialect" > NHibernate.Dialect.MsSql2005Dialect </ property > < property name ="connection.connection_string" > Server=DevDB;Initial Catalog=NHibTut;Trusted_Connection=yes; </ property > < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" /> </ session-factory > </ hibernate-configuration > * This source code was highlighted with Source Code Highlighter .


  1. < session-factory xmlns ="urn:nhibernate-configuration-2.2" >

Required item. Here are all the settings for the object of the ISessionFactory class.

connection.provider - Provider for connecting to the database. Must implement IConnectionProvider interface. In our case, the native provider is used.

connection.driver_class - A class that implements IDriver. In our case, the built-in driver is used to connect to MS SQL Server.

dialect - The implementation of a dialect for a specific database server. In our case, we use NHibernate.Dialect.MsSql2005Dialect, since we use MS SQL Server 2005 Express Edition.

connection.connection_string - The connection string to the database.

Here I want to make a small clarification. Since you often have to work on different computers, in order not to constantly change the connection string to the database, you can create an alias for your database. Thus, you no longer need to worry about the connection string from wherever you work.


  1. < mapping resource ="NHibernate_1.Book.hbm.xml" assembly ="NHibernate_1" />

Here we specify the mapping file of our class and the assembly where it can be found.

The next step is to configure NHibernate itself. The Configuration class is responsible for this.
  1. ISessionFactory sessionFactory = new Configuration().Configure( "Nhibernate.cfg.xml" ).BuildSessionFactory();


It is possible and not to set the name of the configuration file "Nhibernate.cfg.xml", then it should be called "hibernate.cfg.xml", because it is the file with this name that searches in the root directory of NHibernate. In the example above, we configured NHibernate and, based on this configuration, created an ISessionFactory session factory.

ISessionFactory - the interface that serves as a factory for creating an ISession to work directly with the database. Creating an ISessionFactory is a resource-intensive process and it is intended to use a single instance from any part of the application, since it is thread-safe. If there is a need to connect to several databases from your application, then for each of them you need to create a separate ISessionFactory. ISessionFactory also caches SQL database requests, can store cached data that was requested from the database.

To perform any action associated with the database, whether it is a request, save, edit an object, we cannot do without ISession. This is a lightweight interface and its creation requires very few resources. Developers NHibernate advised to create it every time when accessing the database. For example, in an ASP.NET application, you can create an ISession instance for each HttpRequest. ISession is not thread safe, so you need to use it in one thread. ISession is an implementation by NHibernate of the Unit of Work pattern.

Unit of Work contains a list of objects, tracks their changes, and then writes changes to the database.

UnitofWork tracks all changes, as it is the only way to access the database.
Objects that are not placed in UnitOfWork will not be written to the database, i.e. When using this template, you can not access the database directly.

The algorithm of working with the Unit of Work in general is as follows:

1. The object is read from the database and is registered in the Unit of Work list.
Immediately after reading from the database, the object is marked as “clean”, i.e. Dirty = false;
2. When modified or created, the object is marked as Dirty = true
3. When deleting as Deleted
4. To record changes to the database, Commit is called.


To simplify working with ISessionFactory and ISession, we will create the Domain class. Here I will not give all the text of the class,
but only the method of getting the session:
private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  1. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  2. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  3. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  4. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  5. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  6. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  7. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  8. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  9. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  10. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  11. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  12. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  13. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  14. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  15. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  16. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  17. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  18. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  19. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  20. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  21. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  22. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  23. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
  24. private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .
private static ISession GetSession( bool getNewIfNotExists) { ISession currentSession; if ( HttpContext .Current != null ) { HttpContext context = HttpContext .Current; currentSession = context.Items[sessionKey] as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); context.Items[sessionKey] = currentSession; } } else { currentSession = CallContext.GetData(sessionKey) as ISession; if (currentSession == null && getNewIfNotExists) { currentSession = sessionFactory.OpenSession(); CallContext.SetData(sessionKey, currentSession); } } return currentSession; } * This source code was highlighted with Source Code Highlighter .


Depending on whether we are working with a database from a web or win application, the session is saved / retrieved to / from
HttpContext.Current.Items and CallContext respectively. Of course, the Domain class itself is far from ideal and I don’t like everything there, but so far I haven’t come up with anything better. I will be glad to advice. :)

So, we finished the simple and minimally needed infrastructure for our not less simple example of demonstrating the work of NHibernate:
class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  1. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  2. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  3. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  4. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  5. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  6. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  7. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  8. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  9. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  10. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  11. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  12. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  13. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  14. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  15. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  16. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  17. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  18. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  19. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  20. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  21. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  22. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  23. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  24. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  25. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  26. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  27. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  28. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  29. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  30. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  31. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  32. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
  33. class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .
class Program { static void Main() { // Domain.Init(); // Book long bookId; // ISession session = Domain.CurrentSession; // ITransaction tr = session.BeginTransaction(); // Book Book book = new Book( "NW8523IDISDN" , "How to learn not to do stupid things v.1" ); // session.Save(book); // bookId = book.ID; // . // , tr.Commit(); session.Flush(); // , , , session.Clear(); // Book book1 = session.Get<Book>(bookId); // , if (book == book1) { Console .WriteLine( "Yep!!!" ); } // . Domain.Close(); Console .ReadKey(); } } * This source code was highlighted with Source Code Highlighter .


I hope the code with comments is very clear. I deliberately did not give more complicated examples here. I tried to make this first article as easy as possible in order to create a basis for the subsequent ones, in which (I really hope;)) there will be a deeper content of the material.

Project sources can be downloaded here .

UPD: I completely forgot: - [, I want to express a special thanks to XaocCPS for help in editing the article.

Source: https://habr.com/ru/post/37984/


All Articles