Click here to Skip to main content
15,897,891 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 322K   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>
    /// Configuration class is used to mantain the configuration required for NHibernate.
    /// Con read the configuration from a connection string class.
    /// Use the TransactionScope class to hide NHibernate details and for better isolation.
    /// Use the OpenSession method to directly open an NHibernate session from the specified configuration.
    /// </summary>
    [Serializable]
    public class Configuration
    {
        public const string DEFAULT_APP = "DefaultApp";

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="settings"></param>
        public Configuration(System.Configuration.ConnectionStringSettings settings)
        {
            System.Data.Common.DbConnectionStringBuilder connectionString = new System.Data.Common.DbConnectionStringBuilder();
            connectionString.ConnectionString = settings.ConnectionString;

            if (connectionString.ContainsKey("DriverClass"))
            {
                Connection_DriverClass = (string)connectionString["DriverClass"];
                connectionString.Remove("DriverClass");
            }
            else
                throw new ConnectionElementNotFoundException("DriverClass");

            if (connectionString.ContainsKey("Dialect"))
            {
                Dialect = (string)connectionString["Dialect"];
                connectionString.Remove("Dialect");
            }
            else
                throw new ConnectionElementNotFoundException("Dialect");

            Connection_ConnectionString = connectionString.ConnectionString;
        }

        private string mConnection_Provider = "NHibernate.Connection.DriverConnectionProvider";
        /// <summary>
        /// Gets or sets the NHibernate provider to use. Default is NHibernate.Connection.DriverConnectionProvider
        /// </summary>
        public virtual string Connection_Provider
        {
            get { return mConnection_Provider; }
            set { mConnection_Provider = value; }
        }
        private string mConnection_DriverClass;
        /// <summary>
        /// Gets or sets the NHibernate driver to use. For example NHibernate.Driver.SQLite20Driver
        /// </summary>
        public virtual string Connection_DriverClass
        {
            get { return mConnection_DriverClass; }
            set { mConnection_DriverClass = value; }
        }

        private string mConnection_ConnectionString;
        /// <summary>
        /// Gets or sets the NHibernate ConnectionString to use.
        /// </summary>
        public virtual string Connection_ConnectionString
        {
            get { return mConnection_ConnectionString; }
            set { mConnection_ConnectionString = value; }
        }

        private string mDialect;
        /// <summary>
        /// Gets or sets the NHibernate dialect to use NHibernate.Dialect.SQLiteDialect.
        /// </summary>
        public virtual string Dialect
        {
            get { return mDialect; }
            set { mDialect = value; }
        }

        private string mShowSql = "false";
        public virtual string ShowSql
        {
            get { return mShowSql; }
            set { mShowSql = value; }
        }

        public NHibernate.Cfg.Configuration CreateConfiguration()
        {
//hibernate.cfg.xml
//<?xml version='1.0' encoding='utf-8'?>
//<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
//  <!-- an ISessionFactory instance -->
//  <session-factory>
//    <!-- properties -->
//    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
//    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
//    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
//    <property name="connection.connection_string">Data Source=data.db3</property>
//    <property name="show_sql">false</property>
//  </session-factory>
//</hibernate-configuration>


            NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();

            string cnString = Connection_ConnectionString;

            configuration.SetProperty("hibernate.connection.connection_string", cnString);

            configuration.SetProperty("hibernate.connection.provider", Connection_Provider);
            configuration.SetProperty("hibernate.connection.driver_class", Connection_DriverClass);
            configuration.SetProperty("hibernate.show_sql", ShowSql);
            configuration.SetProperty("hibernate.dialect", Dialect);

            return configuration;
        }

        #region Member data
        [NonSerialized]
        private NHibernate.ISessionFactory mFactory;

        /// <summary>
        /// Returns an active NHibernate session.
        /// Usually you don't need to call this method directly, I suggest to use the TransactionScope class.
        /// </summary>
        /// <returns></returns>
        public NHibernate.ISession OpenSession()
        {
            if (mFactory == null)
            {
                NHibernate.Cfg.Configuration configuration = CreateConfiguration();
                configuration.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly());

                configuration.SetInterceptor(new EucalyptoInterceptor());

                mFactory = configuration.BuildSessionFactory();
            }

            return mFactory.OpenSession();
        }
        #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