Click here to Skip to main content
15,886,837 members
Articles / Programming Languages / C#

Test Driving NHibernate 3.0, LINQ and the Entity Framework CTP 5 with the Abstract Factory Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.89/5 (18 votes)
2 Apr 2011CPOL12 min read 229.2K   1.7K   53  
Developing an N-Tier application with C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using ORMDataServices.DataFactories.NHibernate;

namespace ORMDataServices.DataFactories.NHibernate
{
    class NHibernateDataService : IDataService, IDisposable
    {

        public ISession Session 
        {
            get { return _session; }         
        }

        public Boolean SessionIsOpen
        {
            get { return _sessionIsOpen; }
        }

        ISession _session;     
        Configuration _nHibernateConfiguration;
        ISessionFactory _nHibernateFactory;
       
        ITransaction _dbTransaction;

        private Boolean _sessionIsOpen = false;

        /// <summary>
        /// Constructor
        /// </summary>
        public NHibernateDataService()
        {
           
            _nHibernateConfiguration = new Configuration();
            _nHibernateConfiguration.AddAssembly("ORMNhibernateMaps");
            _nHibernateFactory = _nHibernateConfiguration.BuildSessionFactory();      
        }

        /// <summary>
        /// Begin Transaction
        /// </summary>
        public void BeginTransaction()
        {
            if (_sessionIsOpen == false) OpenSession();

            _dbTransaction = _session.BeginTransaction();

            _sessionIsOpen = true;

        }

        /// <summary>
        /// Insert Entity
        /// </summary>
        /// <param name="entity"></param>
        public void Insert(Object entity)
        {
            _session.Save(entity);
        }

        /// <summary>
        /// Update Entity
        /// </summary>
        /// <param name="entity"></param>
        public void Update(Object entity)
        {
            _session.Update(entity);
        }    

        /// <summary>
        /// Commit Transaction
        /// </summary>
        /// <param name="closeSession"></param>
        public void CommitTransaction(Boolean closeSession)
        {
            _dbTransaction.Commit();

            if (closeSession == true)
                CloseSession();
        }

        /// <summary>
        /// Open Session
        /// </summary>
        public void OpenSession()
        {
            if (_sessionIsOpen == false)
            {
                _session = _nHibernateFactory.OpenSession();
                _sessionIsOpen = true;
            }

        }

        /// <summary>
        /// Close Session
        /// </summary>
        public void CloseSession()
        {
            _session.Close();
            _sessionIsOpen = false;
            _session.Dispose();
        }

        /// <summary>
        /// Dispose 
        /// </summary>
        public void Dispose()
        {
            if (_sessionIsOpen == true)
                CloseSession();

            if (_dbTransaction != null)
                _dbTransaction.Dispose();

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Joey Software Solutions
United States United States
Mark Caplin has specialized in Information Technology solutions for the past 30 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.

For the past fifteen years, Mark has specialized in the Microsoft .NET framework using C# as his tool of choice. For the past four years Mark has been implementing Single Page Applications using the Angular platform.

When not coding, Mark enjoys playing tennis, listening to U2 music, watching Miami Dolphins football and watching movies in Blu-Ray technology.

In between all this, his wife of over 25 years, feeds him well with some great home cooked meals.

You can contact Mark at mark.caplin@gmail.com

...

Comments and Discussions