zoukankan      html  css  js  c++  java
  • ASP.NET MVC入门到精通——数据库仓储

    业务层调用数据层对象,我不想每次都new一个数据层对象,而是在数据层创建一个仓储,统一管理所有的对象调用。

    1、在IDAL项目中,新建IDBSession.tt模板

     View Code

    Ctrl+S后自动生成IDBSession接口

     View Code

    2、在DAL项目中实现IDBSession接口

    新建DBSession.tt模板

     View Code

    Ctrl+S后自动生成DBSession类

     View Code

    接下来,我们创建DBSession工厂和上下文工厂,目的是为了提高效率,在线程中共用一个对象。

    3、IDAL项目中添加IDBSessionFactory接口

    复制代码
    namespace IDAL
    {
        /// <summary>
        /// 数据仓储工厂
        /// </summary>
       public interface IDBSessionFactory
        {
           IDBSession GetDBSession();
        }
    }
    复制代码

    DAL项目中添加DBSessionFactory类继承IDBSessionFactory接口

    复制代码
    using System.Runtime.Remoting.Messaging;
    using IDAL;
    
    namespace DAL
    {
        public class DBSessionFactory : IDBSessionFactory
        {
            /// <summary>
            /// 此方法的作用: 提高效率,在线程中 共用一个 DBSession 对象!
            /// </summary>
            /// <returns></returns>
            public IDBSession GetDBSession()
            {
                //从当前线程中 获取 DBContext 数据仓储 对象
                IDBSession dbSesion = CallContext.GetData(typeof(DBSessionFactory).Name) as DBSession;
                if (dbSesion == null)
                {
                    dbSesion = new DBSession();
                    CallContext.SetData(typeof(DBSessionFactory).Name, dbSesion);
                }
                return dbSesion;
            }
        }
    }
    复制代码

    4、同样,我们再来创建一个上下文工厂,即便以后有多个数据库上下文,也能够很好的支持。

    IDAL项目中,新建IDBContextFactory.cs接口

    复制代码
    using System.Data.Entity;
    
    namespace IDAL
    {
        /// <summary>
        /// EF数据上下文 工厂
        /// </summary>
       public interface IDBContextFactory
        {
           /// <summary>
            /// 获取 EF 上下文对象
           /// </summary>
           /// <returns></returns>
           DbContext GetDbContext();
        }
    复制代码

    DAL项目中新建DBContextFactory类继承IDBContextFactory接口

    复制代码
    using System.Data.Entity;
    using System.Runtime.Remoting.Messaging;
    using Model;
    
    namespace DAL
    {
        public class DBContextFactory : IDBContextFactory
        {
            #region 创建 EF上下文 对象,在线程中共享 一个 上下文对象 + DbContext GetDbContext()
            /// <summary>
            /// 创建 EF上下文 对象,在线程中共享 一个 上下文对象
            /// </summary>
            /// <returns></returns>
            public DbContext GetDbContext()
            {
                ////从当前线程中 获取 EF上下文对象
                var dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext;
                if (dbContext == null)
                {
                    dbContext = new OAEntities();
                    CallContext.SetData(typeof(DBContextFactory).Name, dbContext);
                }
                return dbContext;
            }
            #endregion
        }
    }
    复制代码

    5、Common项目中,添加ConfigurationHelper.cs来操作配置文件

    复制代码
    using System;
    using System.Configuration;
    
    namespace Common
    {
        public static class ConfigurationHelper
        {
            public static string AppSetting(string key)
            {
                return ConfigurationManager.AppSettings[key];
            }
        }
    }
    复制代码

    Web.config中添加如下配置节点:

        <add key="DBSessionFatory" value="DAL.DBSessionFactory" />
        <add key="DBSessionFatoryDLL" value="E:WorkSpaceStudyWebsMVCOAslnWebinDAL.dll" />

    6、修改BaseBLL类的调用方式,添加如下代码:

    复制代码
            /// <summary>
            /// 2.0 数据仓储接口(相当于数据层工厂,可以创建所有的数据子类对象)
            /// </summary>
            private IDAL.IDBSession iDbSession;
    
            #region 数据仓储 属性 + IDBSession DBSession
            /// <summary>
            /// 数据仓储 属性
            /// </summary>
            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.2根据配置文件内容 使用 DI层里的Spring.Net 创建 DBSessionFactory 工厂对象
    
    
                        //3.通过 工厂 创建 DBSession对象
                        iDbSession = sessionFactory.GetDBSession();
                    }
                    return iDbSession;
                }
            }
            #endregion
    复制代码

     在这里,使用到了工厂来创建对象,后面引入了Spring.net之后,会回过头来优化现有的代码。项目中使用到了许多接口,目的是为了解耦,每一个项目的职责尽量让其单一,业务层只让其调用数据层接口,也是为了依赖于抽象,而不是具体。每一个框架其实都是各种设计模式的一个集合,设计模式是为了解决一类问题,而框架就是为了解决一系列问题了。到现在为止,整个项目的雏形已经出来了,但是后续,我们一步一步来优化,好的框架不是一下子就能设计得完美的,而是能够不断的拥抱修改,可持续扩展,不断改进出来的。

  • 相关阅读:
    401. Binary Watch
    46. Permutations
    61. Rotate List
    142. Linked List Cycle II
    86. Partition List
    234. Palindrome Linked List
    19. Remove Nth Node From End of List
    141. Linked List Cycle
    524. Longest Word in Dictionary through Deleting
    android ListView详解
  • 原文地址:https://www.cnblogs.com/chenliyang/p/6595667.html
Copyright © 2011-2022 走看看