using System;
using System.Reflection;
using System.Configuration;
using FOUPOS.IDAL;
namespace FOUPOS.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)  
    /// DataCache类在导出代码的文件夹里
    /// <appSettings>  
    /// <add key="DAL" value="FOUPOS.OleDbDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }
        /// <summary>
        /// 创建FAccount数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IFAccount CreateFAccount()
        {

            string ClassNamespace = AssemblyPath + ".FAccount";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (FOUPOS.IDAL.IFAccount)objType;
        }

        /// <summary>
        /// 创建FClass数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IFClass CreateFClass()
        {

            string ClassNamespace = AssemblyPath + ".FClass";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (FOUPOS.IDAL.IFClass)objType;
        }


        /// <summary>
        /// 创建FDetail数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IFDetail CreateFDetail()
        {

            string ClassNamespace = AssemblyPath + ".FDetail";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (FOUPOS.IDAL.IFDetail)objType;
        }


        /// <summary>
        /// 创建FGoods数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IFGoods CreateFGoods()
        {

            string ClassNamespace = AssemblyPath + ".FGoods";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (FOUPOS.IDAL.IFGoods)objType;
        }


        /// <summary>
        /// 创建FUser数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IFUser CreateFUser()
        {

            string ClassNamespace = AssemblyPath + ".FUser";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (FOUPOS.IDAL.IFUser)objType;
        }
        /// <summary>
        /// 创建Membership数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IMembership CreateMembership()
        {

            string CacheKey = AssemblyPath + ".Membership";
            object objType = CreateObject(AssemblyPath, CacheKey);
            return (FOUPOS.IDAL.IMembership)objType;
        }
        /// <summary>
        /// 创建AdminUser数据层接口
        /// </summary>
        public static FOUPOS.IDAL.IAdminUser CreateAdminUser()
        {

            string CacheKey = AssemblyPath + ".AdminUser";
            object objType = CreateObject(AssemblyPath, CacheKey);
            return (FOUPOS.IDAL.IAdminUser)objType;
        }
        /// <summary>
        /// 创建Card数据层接口
        /// </summary>
        public static FOUPOS.IDAL.ICard CreateCard()
        {

            string CacheKey = AssemblyPath + ".Card";
            object objType = CreateObject(AssemblyPath, CacheKey);
            return (FOUPOS.IDAL.ICard)objType;
        }

    }
}