Click here to Skip to main content
15,895,656 members
Articles / Web Development / ASP.NET

Eucalypto - ASP.NET CMS Library using NHibernate

Rate me:
Please Sign up or sign in to vote.
4.84/5 (36 votes)
10 Jun 2009MIT24 min read 321.2K   4.6K   260  
An ASP.NET server library for creating CMS website (forums, articles/wiki, news, users/roles, ...), using NHibernate for data access.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Eucalypto
{
    /// <summary>
    /// The TransactionScope class identify the scope of the transaction used by the Eucalypto classes.
    /// Contains a nhibernate transaction and session. (NHibernateTransaction, NHibernateSession).
    /// </summary>
    public class TransactionScope : IDisposable
    {
        private NHibernate.ISession mSession;
        private NHibernate.ITransaction mTransaction;
        private bool mDispose;

        /// <summary>
        /// Create a transaction scope using the specified session and transaction.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="transaction"></param>
        /// <param name="dispose">Set to true to dispose the objects when the transaction scope is disposed</param>
        public TransactionScope(NHibernate.ISession session, 
                        NHibernate.ITransaction transaction,
                        bool dispose)
        {
            mSession = session;
            mTransaction = transaction;
            mDispose = dispose;
        }

        /// <summary>
        /// Create a new transaction scope with a new session and a new transaction.
        /// The session and transaction are created based on the Configuration class.
        /// Both objects are disposed at the end.
        /// </summary>
        /// <param name="configuration"></param>
        public TransactionScope(Configuration configuration)
        {
            mSession = configuration.OpenSession();
            mTransaction = mSession.BeginTransaction();
            mDispose = true;
        }

        public void Rollback()
        {
            mTransaction.Rollback();
        }

        public void Commit()
        {
            mTransaction.Commit();
        }

        public NHibernate.ITransaction NHibernateTransaction
        {
            get { return mTransaction; }
        }
        public NHibernate.ISession NHibernateSession
        {
            get { return mSession; }
        }

        public IDbCommand CreateDbCommand()
        {
            IDbCommand command = NHibernateSession.Connection.CreateCommand();
            NHibernateTransaction.Enlist(command);
            return command;
        }
        public IDbDataParameter CreateDbCommandParameter(IDbCommand command, string parameterName, DbType type, object value)
        {
            IDbDataParameter param = command.CreateParameter();
            param.ParameterName = parameterName;
            param.DbType = type;
            param.Value = value;
            return param;
        }


        #region IDisposable Members
        public void Dispose()
        {
            if (mDispose)
            {
                mTransaction.Dispose();
                mSession.Dispose();
            }

            mTransaction = null;
            mSession = null;
        }
        #endregion
    }
}

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 MIT License


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions