Wednesday, February 22, 2006
« MySQL Schema Provider 0.9.2 Released | Main | Free ASP.NET 2.0 Video Tutorials »
In case someone else would like to know how to create a singleton DataManager class for WilsonORMapper that uses IsolatedContext and works in ASP.NET...

View DataManager.cs

using System.Configuration;
using System.IO;
using System.Reflection;
using System.Web;
using Wilson.ORMapper;

namespace Portal.Data
{
    /// <summary>
    /// The DataManager class is the singleton instance of the ObjectSpace class
    /// </summary>
    public sealed class DataManager
    {
        private const string MAPPING_FILE = "Mapping.config";
        private static readonly Guid CONTEXT_KEY = Guid.NewGuid();
        
        /// <summary>The application connection string read from app.config</summary>        
        /// <example>
        /// Add the following key to the "appSettings" section of your config:
        /// <code><![CDATA[
        ///     <configuration>
        ///            <appSettings>
        ///                <add key="IMI.Portal.Framework.Data.ConnectionString" value="Data Source=(local);Initial Catalog=DATABASE;User ID=USERNAME;Password=PASSWORD;" />
        ///            </appSettings>
        ///     </configuration>
        /// ]]></code>
        /// </example>
        public static readonly string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        public static readonly string CurrentProvider = ConfigurationSettings.AppSettings["Provider"];

        /// <summary>The singletion instance of an ObjectSpace Class</summary>
        private static ObjectSpace objectSpace; // = GetDefaultInstance();
        private static object syncLock = new object();

        private DataManager()
        { }

        #region public static ObjectSpace ObjectSpace

        /// <summary>The singleton method for retrieving the global ObjectSpace</summary>
        public static ObjectSpace ObjectSpace
        {
            get
            {
                if (objectSpace == null)
                {
                    lock (syncLock)
                    {
                        if (objectSpace == null)
                        {
                            objectSpace = GetDefaultInstance();
                        }
                    }
                }
                
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    ObjectSpace isolatedContext = (ObjectSpace) context.Items[CONTEXT_KEY];
                    if (isolatedContext == null)
                    {
                        // Cache the isolated context in the current request so that it is used
                        // throughout the entire request

                        isolatedContext = objectSpace.IsolatedContext;
                        context.Items[CONTEXT_KEY] = isolatedContext;
                    }
                    return isolatedContext;
                }
                else
                {
                    return objectSpace;
                }
            }
        }

        #endregion

        #region private static ObjectSpace GetDefaultInstance()

        private static ObjectSpace GetDefaultInstance()
        {
            Assembly assembly = Assembly.GetAssembly(typeof(DataManager));
            
            using (Stream mappingStream = assembly.GetManifestResourceStream(
                typeof(DataManager),
                MAPPING_FILE))
            {

                switch(CurrentProvider)
                {
                    case "MySQL":

                        return new ObjectSpace(
                            mappingStream,
                            ConnectionString,
                            GetMySQLProvider(),
                            20, 5);

                    case "PostgreSQL":

                        return new ObjectSpace(
                            mappingStream,
                            ConnectionString,
                            GetPostgreSQLProvider(),
                            20, 5);

                    // TODO: Oracle?

                    default:

                        return new ObjectSpace(
                            mappingStream,
                            ConnectionString,
                            Provider.MsSql,
                            20, 5);

                }
                
            }
        }

        #endregion

        #region private static CustomProvider GetMySQLProvider()

        private static CustomProvider GetMySQLProvider()
        {
            //Provider provider;
            CustomProvider provider;
            provider=new CustomProvider("MySql.Data", "MySql.Data.MySqlClient.MySqlConnection", "MySql.Data.MySqlClient.MySqlDataAdapter");
            provider.StartDelimiter="";
            provider.EndDelimiter="";
            provider.LineTerminator=";";
            provider.IdentityQuery= "SELECT LAST_INSERT_ID()";
            provider.SelectPageQuery="SELECT * LIMIT {0} OFFSET {1}";
            provider.ParameterPrefix = "?";
            provider.GuidDelimiter = "'";

            return provider;
            
        }

        #endregion

        #region private static CustomProvider GetPostgreSQLProvider()

        private static CustomProvider GetPostgreSQLProvider()
        {
            // Npgsql, the .NET provider for PostgreSQL can be found here:
            // http://pgfoundry.org/projects/npgsql

            CustomProvider provider;
            provider=new CustomProvider("Npgsql", "Npgsql.NpgsqlConnection", "Npgsql.NpgsqlDataAdapter");
            provider.StartDelimiter="[";
            provider.EndDelimiter="]";
            provider.LineTerminator=";";
            provider.IdentityQuery= "SELECT currval('{1}_{0}_seq')";
            provider.SelectPageQuery="SELECT * LIMIT {0} OFFSET {1}";
            provider.ParameterPrefix = ":";
            //not sure about this
            provider.GuidDelimiter = "'";

            return provider;
            
        }

        #endregion
    }
}
       
Comments are closed.