Source

Camino / src / Empi.Camino / Data / DataContextFactory.cs

Full commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Linq.Expressions;
using Empi.Camino.Web.Data;
using Empi.Camino.Configuration;

namespace Empi.Camino.Data
{
    internal class DataContextFactory : IDataContextFactory
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataContextFactory"/> class for the Meta Database.
        /// </summary>
        internal DataContextFactory()
        {
            BuildDataContext("DataContext_MetaDb", () => RetailerSettings.GetRetailerSettings.MetaDataBaseConnectionString);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataContextFactory"/> class.
        /// </summary>
        /// <param name="dbId">The db id.</param>
        /// <param name="dbState">State of the db.</param>
        internal DataContextFactory(string dbId, string dbState)
        {
            BuildDataContext("DataContext_" + dbId+'_'+ dbState, () => Empi.Camino.ConnectionsStrings.GetConnectionString(dbId, dbState));
        }


        private void BuildDataContext(string key, Func<string> connectionString)
        {
            DataContext = DataStoreFactory.GetItemsDataStore.LazyGet<DataContext>(key, () => (DataContext)Activator.CreateInstance(typeof(DataContext), connectionString()));
        }

        #region IDataContextFactory Members

        public DataContext DataContext
        {
            get;
            private set;
        }

        #endregion
    }
}