Sessions in NHibernate aren't really too tricky, but they are INCREDIBLY powerful. With an NHibernate session, I can perform all of the CRUD (Create, Retrieve, Update, and Delete) operations with ease. Consider the following example:
ISession session = sessionFactory.OpenSession(); ITransaction tx = session.BeginTransaction(); Contact contact = new Contact("Joe", "Jones", "[email protected]"); session.Save(contact); tx.Commit();
With five short lines of code, we created a new contact and stored it into the database. The first two lines creates a session
from the session factory and start a transaction
, the third line creates the contact
object from the Contact
class that we created, and the last two lines commit it to the database. What if we wanted to add an address before we saved it? We would have to include another three lines of code as follows:
ISession session = sessionFactory.OpenSession(); ITransaction tx = session.BeginTransaction(); Contact contact = new...