关于数据工厂的创建“DBSession”

第一:在数据接口层创建IDBSession 存放的全是字段属性        再创建IDBSessionFactory创建获取的方法

public partial interface IDBSession
    {
     IHKSJ_ClientsDAL IHKSJ_ClientsDAL{get;set;}

     IHKSJ_EmployeesDAL IHKSJ_EmployeesDAL{get;set;}

     IHKSJ_FirstDAL IHKSJ_FirstDAL{get;set;}

     IHKSJ_MainDAL IHKSJ_MainDAL{get;set;}

     IHKSJ_RelationshipDAL IHKSJ_RelationshipDAL{get;set;}

     IHKSJ_USERSDAL IHKSJ_USERSDAL{get;set;}

    }
public interface IDBSessionFactory
    {
      IDAL.IDBSession GetDBSession();
    }

第二:数据层实现IDBSession和IDBSessionFactory     

public partial class DBSession:IDAL.IDBSession
    {

    #region 01 数据接口 + IHKSJ_ClientsDAL
     IHKSJ_ClientsDAL iHKSJ_ClientsDAL;
     public IHKSJ_ClientsDAL IHKSJ_ClientsDAL
     {
        get
        {
            if(iHKSJ_ClientsDAL==null)
                iHKSJ_ClientsDAL=new HKSJ_ClientsDAL();
            return iHKSJ_ClientsDAL;
        }
        set
        {
            iHKSJ_ClientsDAL=value;
        }
     }
    #endregion
  以下还有很多代码省略
 public class DBSessionFactory : IDAL.IDBSessionFactory
    {
        /// <summary>
        /// 此方法的作用: 提高效率,在线程中 共用一个 DBSession 对象!
        /// </summary>
        /// <returns></returns>
        public IDAL.IDBSession GetDBSession()
        {
            //从当前线程中 获取 DBSession 数据仓储 对象
            IDAL.IDBSession iDBSession = CallContext.GetData(typeof(DBSessionFactory).Name) as IDAL.IDBSession;
            if (iDBSession==null)
            {
                //创建数据工厂对象实例
                iDBSession = new DBSession();
                CallContext.SetData(typeof(DBSessionFactory).Name, iDBSession);
            }
            return iDBSession;
        }
    }

第三:在业务层通过反射方式获取到DBSession对象

        public IDAL.IDBSession iDbSession;

        #region 数据仓储 属性 + IDBSession DBSession
        /// <summary>
        /// 数据仓储 属性
        /// </summary>
        //让子类调用父类的属性  构造子类对象赋值给idal
        public IDAL.IDBSession DBSession
        {
            get
            {
                if (iDbSession == null)
                {
                    //1.读取配置文件
                    string strFactoryDLL = Common.ConfigurationHelper.AppSetting("DBSessionFatoryDLL");
                    string strFactoryType = Common.ConfigurationHelper.AppSetting("DBSessionFatory");
                    //2.1通过反射创建 DBSessionFactory 工厂对象
                    Assembly dalDLL = Assembly.LoadFrom(strFactoryDLL);
                    Type typeDBSessionFatory = dalDLL.GetType(strFactoryType);
                    IDAL.IDBSessionFactory sessionFactory = Activator.CreateInstance(typeDBSessionFatory) as IDAL.IDBSessionFactory;
                    //2.根据配置文件内容 使用 DI层里的Spring.Net 创建 DBSessionFactory 工厂对象


                    //3.通过 工厂 创建 DBSession对象 就是存储的都是DAL的实例
                    iDbSession = sessionFactory.GetDBSession();
                }
                return iDbSession;
            }

        }
通过反射获取DBSession对象

第四:通过业务子类获取数据子类对象实列  传送给业务父类使用

public partial class HKSJ_ClientsBLL : BaseBLL<EFModel.HKSJ_Clients>,IHKSJ_ClientsBLL
    {
        public override void SetDAL()
        {
           idal=DBSession.IHKSJ_ClientsDAL;
        }
    }

猜你喜欢

转载自www.cnblogs.com/xiaoyangshu/p/12311292.html