asp.net mvc中自行搭建实现工厂模式的spring.net+三层架构 (15-4-20)

     在之前的文章中,我们已经搭好了三层框架,通过调用业务层的子接口得到实例化的业务子类,通过业务子类来调用数据访问层子接口,由子接口来实例化数据访问层子类。这里我们需要明确把框架搭这么复杂的原因是遵循了设计模式中的开闭原则:对扩展开放,对修改关闭。

     搭好了框架之后,我们发现这框架并不完善:实例化对象还需要手动实现,并不能实现将对象的实例化托管。所以我们决定引入spring.net框架,它的IOC模块(依赖注入),将帮我们来完成对象的创建,管理。

    1、首先我们需要在项目中引用spring.core.dll、Common.Logging.dll这两个程序集。引用完毕后,我们来对项目框架进行修改。先从最底层的数据访问层出发:为了方便统一管理对象,我们创建一个DBSession工厂类,用来统一对类进行实例化,代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using IDAL;
  6 
  7 namespace DAL
  8 {
  9     public partial class DBSession:IDAL.IDBSession
 10     {
 11         #region 01 数据接口 IsysdiagramsDAL
 12         IsysdiagramsDAL isysdiagramsDAL;
 13         public IsysdiagramsDAL IsysdiagramsDAL
 14         {
 15             get
 16             {
 17                 if(isysdiagramsDAL==null)
 18                     isysdiagramsDAL= new sysdiagramsDAL();
 19                 return isysdiagramsDAL;
 20             }
 21             set
 22             {
 23                 isysdiagramsDAL= value;
 24             }
 25         }
 26         #endregion
 27         
 28         #region 02 数据接口 IT001账号表DAL
 29         IT001账号表DAL iT001账号表DAL;
 30         public IT001账号表DAL IT001账号表DAL
 31         {
 32             get
 33             {
 34                 if(iT001账号表DAL==null)
 35                     iT001账号表DAL= new T001账号表DAL();
 36                 return iT001账号表DAL;
 37             }
 38             set
 39             {
 40                 iT001账号表DAL= value;
 41             }
 42         }
 43         #endregion
 44 
 45         #region 03 数据接口 IT002验证表DAL
 46         IT002验证表DAL iT002验证表DAL;
 47         public IT002验证表DAL IT002验证表DAL
 48         {
 49             get
 50             {
 51                 if(iT002验证表DAL==null)
 52                     iT002验证表DAL= new T002验证表DAL();
 53                 return iT002验证表DAL;
 54             }
 55             set
 56             {
 57                 iT002验证表DAL= value;
 58             }
 59         }
 60         #endregion
 61 
 62         #region 04 数据接口 IT003用户角色表DAL
 63         IT003用户角色表DAL iT003用户角色表DAL;
 64         public IT003用户角色表DAL IT003用户角色表DAL
 65         {
 66             get
 67             {
 68                 if(iT003用户角色表DAL==null)
 69                     iT003用户角色表DAL= new T003用户角色表DAL();
 70                 return iT003用户角色表DAL;
 71             }
 72             set
 73             {
 74                 iT003用户角色表DAL= value;
 75             }
 76         }
 77         #endregion
 78 
 79         #region 05 数据接口 IT004社团信息表DAL
 80         IT004社团信息表DAL iT004社团信息表DAL;
 81         public IT004社团信息表DAL IT004社团信息表DAL
 82         {
 83             get
 84             {
 85                 if(iT004社团信息表DAL==null)
 86                     iT004社团信息表DAL= new T004社团信息表DAL();
 87                 return iT004社团信息表DAL;
 88             }
 89             set
 90             {
 91                 iT004社团信息表DAL= value;
 92             }
 93         }
 94         #endregion
 95 
 96         #region 06 数据接口 IT005票务表DAL
 97         IT005票务表DAL iT005票务表DAL;
 98         public IT005票务表DAL IT005票务表DAL
 99         {
100             get
101             {
102                 if(iT005票务表DAL==null)
103                     iT005票务表DAL= new T005票务表DAL();
104                 return iT005票务表DAL;
105             }
106             set
107             {
108                 iT005票务表DAL= value;
109             }
110         }
111         #endregion
112 
113         #region 07 数据接口 IT006店铺信息表DAL
114         IT006店铺信息表DAL iT006店铺信息表DAL;
115         public IT006店铺信息表DAL IT006店铺信息表DAL
116         {
117             get
118             {
119                 if(iT006店铺信息表DAL==null)
120                     iT006店铺信息表DAL= new T006店铺信息表DAL();
121                 return iT006店铺信息表DAL;
122             }
123             set
124             {
125                 iT006店铺信息表DAL= value;
126             }
127         }
128         #endregion
129 
130         #region 08 数据接口 IT007店铺货物表DAL
131         IT007店铺货物表DAL iT007店铺货物表DAL;
132         public IT007店铺货物表DAL IT007店铺货物表DAL
133         {
134             get
135             {
136                 if(iT007店铺货物表DAL==null)
137                     iT007店铺货物表DAL= new T007店铺货物表DAL();
138                 return iT007店铺货物表DAL;
139             }
140             set
141             {
142                 iT007店铺货物表DAL= value;
143             }
144         }
145         #endregion
146 
147         #region 09 数据接口 IT008海报信息表DAL
148         IT008海报信息表DAL iT008海报信息表DAL;
149         public IT008海报信息表DAL IT008海报信息表DAL
150         {
151             get
152             {
153                 if(iT008海报信息表DAL==null)
154                     iT008海报信息表DAL= new T008海报信息表DAL();
155                 return iT008海报信息表DAL;
156             }
157             set
158             {
159                 iT008海报信息表DAL= value;
160             }
161         }
162         #endregion
163 
164         #region 10 数据接口 IT009社团账号表DAL
165         IT009社团账号表DAL iT009社团账号表DAL;
166         public IT009社团账号表DAL IT009社团账号表DAL
167         {
168             get
169             {
170                 if(iT009社团账号表DAL==null)
171                     iT009社团账号表DAL= new T009社团账号表DAL();
172                 return iT009社团账号表DAL;
173             }
174             set
175             {
176                 iT009社团账号表DAL= value;
177             }
178         }
179         #endregion
180 
181     }
182 
183 }

数据访问层有了统一的对象管理工厂,自然业务逻辑层也需要:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using IBLL;
  6 
  7 namespace BLL
  8 {
  9     public partial class BLLSession:IBLLSession
 10     {
 11         #region 01 业务接口 IsysdiagramsBLL
 12         IsysdiagramsBLL isysdiagramsBLL;
 13         public IsysdiagramsBLL IsysdiagramsBLL
 14         {
 15             get
 16             {
 17                 if(isysdiagramsBLL==null)
 18                     isysdiagramsBLL= new sysdiagramsBLL();
 19                 return isysdiagramsBLL;
 20             }
 21             set
 22             {
 23                 isysdiagramsBLL= value;
 24             }
 25         }
 26         #endregion
 27 
 28         #region 02 业务接口 IT001账号表BLL
 29         IT001账号表BLL iT001账号表BLL;
 30         public IT001账号表BLL IT001账号表BLL
 31         {
 32             get
 33             {
 34                 if(iT001账号表BLL==null)
 35                     iT001账号表BLL= new T001账号表BLL();
 36                 return iT001账号表BLL;
 37             }
 38             set
 39             {
 40                 iT001账号表BLL= value;
 41             }
 42         }
 43         #endregion
 44 
 45         #region 03 业务接口 IT002验证表BLL
 46         IT002验证表BLL iT002验证表BLL;
 47         public IT002验证表BLL IT002验证表BLL
 48         {
 49             get
 50             {
 51                 if(iT002验证表BLL==null)
 52                     iT002验证表BLL= new T002验证表BLL();
 53                 return iT002验证表BLL;
 54             }
 55             set
 56             {
 57                 iT002验证表BLL= value;
 58             }
 59         }
 60         #endregion
 61 
 62         #region 04 业务接口 IT003用户角色表BLL
 63         IT003用户角色表BLL iT003用户角色表BLL;
 64         public IT003用户角色表BLL IT003用户角色表BLL
 65         {
 66             get
 67             {
 68                 if(iT003用户角色表BLL==null)
 69                     iT003用户角色表BLL= new T003用户角色表BLL();
 70                 return iT003用户角色表BLL;
 71             }
 72             set
 73             {
 74                 iT003用户角色表BLL= value;
 75             }
 76         }
 77         #endregion
 78 
 79         #region 05 业务接口 IT004社团信息表BLL
 80         IT004社团信息表BLL iT004社团信息表BLL;
 81         public IT004社团信息表BLL IT004社团信息表BLL
 82         {
 83             get
 84             {
 85                 if(iT004社团信息表BLL==null)
 86                     iT004社团信息表BLL= new T004社团信息表BLL();
 87                 return iT004社团信息表BLL;
 88             }
 89             set
 90             {
 91                 iT004社团信息表BLL= value;
 92             }
 93         }
 94         #endregion
 95 
 96         #region 06 业务接口 IT005票务表BLL
 97         IT005票务表BLL iT005票务表BLL;
 98         public IT005票务表BLL IT005票务表BLL
 99         {
100             get
101             {
102                 if(iT005票务表BLL==null)
103                     iT005票务表BLL= new T005票务表BLL();
104                 return iT005票务表BLL;
105             }
106             set
107             {
108                 iT005票务表BLL= value;
109             }
110         }
111         #endregion
112 
113         #region 07 业务接口 IT006店铺信息表BLL
114         IT006店铺信息表BLL iT006店铺信息表BLL;
115         public IT006店铺信息表BLL IT006店铺信息表BLL
116         {
117             get
118             {
119                 if(iT006店铺信息表BLL==null)
120                     iT006店铺信息表BLL= new T006店铺信息表BLL();
121                 return iT006店铺信息表BLL;
122             }
123             set
124             {
125                 iT006店铺信息表BLL= value;
126             }
127         }
128         #endregion
129 
130         #region 08 业务接口 IT007店铺货物表BLL
131         IT007店铺货物表BLL iT007店铺货物表BLL;
132         public IT007店铺货物表BLL IT007店铺货物表BLL
133         {
134             get
135             {
136                 if(iT007店铺货物表BLL==null)
137                     iT007店铺货物表BLL= new T007店铺货物表BLL();
138                 return iT007店铺货物表BLL;
139             }
140             set
141             {
142                 iT007店铺货物表BLL= value;
143             }
144         }
145         #endregion
146 
147         #region 09 业务接口 IT008海报信息表BLL
148         IT008海报信息表BLL iT008海报信息表BLL;
149         public IT008海报信息表BLL IT008海报信息表BLL
150         {
151             get
152             {
153                 if(iT008海报信息表BLL==null)
154                     iT008海报信息表BLL= new T008海报信息表BLL();
155                 return iT008海报信息表BLL;
156             }
157             set
158             {
159                 iT008海报信息表BLL= value;
160             }
161         }
162         #endregion
163 
164         #region 10 业务接口 IT009社团账号表BLL
165         IT009社团账号表BLL iT009社团账号表BLL;
166         public IT009社团账号表BLL IT009社团账号表BLL
167         {
168             get
169             {
170                 if(iT009社团账号表BLL==null)
171                     iT009社团账号表BLL= new T009社团账号表BLL();
172                 return iT009社团账号表BLL;
173             }
174             set
175             {
176                 iT009社团账号表BLL= value;
177             }
178         }
179         #endregion
180 
181     }
182 
183 }

  有了俩个统一管理的工厂类之后,我们就可以来实现对象实例化的托管了,由于在实际调用时是调用业务逻辑层的子接口,由它返回一个具体的业务逻辑层的对象,我们现在通过spring来实现它(新建一个helper类用来通过配置文件以反射的机制获取一个业务子对象):

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ClassLibrary
 8 {
 9   public  class Helper
10     {
11 
12       public static IBLL.IBLLSession BLLSession = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");
13 
14     }
15 }

相应的springHelper的代码:

 1 using Spring.Context;
 2 using Spring.Context.Support;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 
 8 namespace DI
 9 {
10     public class SpringHelper
11     {
12         #region 1.0 Spring容器上下文 -IApplicationContext SpringContext
13         /// <summary>
14         /// Spring容器上下文
15         /// </summary>
16         private static IApplicationContext SpringContext
17         {
18             get
19             {
20 
21                  return ContextRegistry.GetContext();
22                
23             }
24         }
25         #endregion
26 
27         #region 2.0 获取配置文件 配置的 对象 +T GetObject<T>(string objName) where T : class
28         /// <summary>
29         /// 获取配置文件 配置的 对象
30         /// </summary>
31         /// <typeparam name="T"></typeparam>
32         /// <param name="objName"></param>
33         /// <returns></returns>
34         public static T GetObject<T>(string objName) where T : class
35         {
36             return (T)SpringContext.GetObject(objName);
37         }
38         #endregion
39     }
40 }

这样我们通过反射机制能拿到一个业务对象了,接下来再业务层中调用数据访问层的子接口,我们需要根据上一篇文章做一些修改:
附上修改BaseBLL后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace BLL
{
    public abstract class BaseBLL<T> : IBLL.IBaseBLL<T> where T : class,new()
    {
        public BaseBLL()
        {
            SetDAL();
        }

   

        //定义数据操作层父接口类
        //protected DAL.BaseDAL<T> idal;//= new DAL.BaseDAL<T>();
        protected IDAL.IBaseDAL<T> idal;
        /// <summary>
        /// 由子类实现,为 业务父类 里的 数据接口对象 设置 值!
        /// </summary>
        public abstract void SetDAL();

    
        private IDAL.IDBSession iDbSession;

        #region 数据仓储 属性 + IDBSession DBSession
        /// <summary>
        /// 数据仓储 属性
        /// </summary>
        public IDAL.IDBSession DBSession
        {
            get
            {
                if (iDbSession == null)
                {
                    
                    //根据配置文件内容 使用 DI层里的Spring.Net 创建 DBSessionFactory 工厂对象
                    IDAL.IDBSessionFactory sessionFactory = DI.SpringHelper.GetObject<IDAL.IDBSessionFactory>("DBSessionFactory");

                    //通过 工厂 创建 DBSession对象
                    iDbSession = sessionFactory.GetDBSession();
                }
                return iDbSession;
            }
        }
        #endregion

        
        //2,定义增删改查的方法
        //2.增删改查方法
        #region 1.0 新增 实体 +int Add(T model)
        /// <summary>
        /// 新增 实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(T model)
        {
            return idal.Add(model);
        }
        #endregion

        #region 2。0 根据 用户 id 删除 +int Del(int uId)
        /// <summary>
        /// 根据 用户 id 删除
        /// </summary>
        /// <param name="uId"></param>
        /// <returns></returns>
        public int Del(T model)
        {
            return idal.Del(model);
        }
        #endregion

        #region 3.0 根据条件删除 +int DelBy(Expression<Func<T, bool>> delWhere)
        /// <summary>
        /// 3.0 根据条件删除
        /// </summary>
        /// <param name="delWhere"></param>
        /// <returns></returns>
        public int DelBy(Expression<Func<T, bool>> delWhere)
        {
            return idal.DelBy(delWhere);
        }
        #endregion

        #region 4.0 修改 +int Modify(T model, params string[] proNames)
        /// <summary>
        /// 4.0 修改,如:
        /// </summary>
        /// <param name="model">要修改的实体对象</param>
        /// <param name="proNames">要修改的 属性 名称</param>
        /// <returns></returns>
        public int Modify(T model, params string[] proNames)
        {
            return idal.Modify(model, proNames);
        }
        #endregion

        #region  4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        /// <summary>
        ///  4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        /// </summary>
        /// <param name="model"></param>
        /// <param name="whereLambda"></param>
        /// <param name="modifiedProNames"></param>
        /// <returns></returns>
        public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        {
            return idal.ModifyBy(model, whereLambda, modifiedProNames);
        }
        #endregion

        #region 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)
        /// <summary>
        /// 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public List<T> GetListBy(Expression<Func<T, bool>> whereLambda)
        {
            return idal.GetListBy(whereLambda);
        }
        #endregion

        #region 5.1 根据条件 排序 和查询 + List<T> GetListBy<TKey>
        /// <summary>
        /// 5.1 根据条件 排序 和查询
        /// </summary>
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="whereLambda">查询条件 lambda表达式</param>
        /// <param name="orderLambda">排序条件 lambda表达式</param>
        /// <returns></returns>
        public List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda)
        {
            return idal.GetListBy(whereLambda, orderLambda);
        }
        #endregion

        #region 6.0 分页查询 + List<T> GetPagedList<TKey>
        /// <summary>
        /// 6.0 分页查询 + List<T> GetPagedList<TKey>
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="whereLambda">条件 lambda表达式</param>
        /// <param name="orderBy">排序 lambda表达式</param>
        /// <returns></returns>
        public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy)
        {
            return idal.GetPagedList(pageIndex, pageSize, whereLambda, orderBy);
        }
        #endregion

    }
}

可以看出,主要是在BaseBLL中加了一个数据访问层仓储的属性,并且通过spring.net给它赋值,得到了这个仓储之后,需要更改一下业务子类中的setDAL()方法:

 1  using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Linq.Expressions;
 6 using IBLL;
 7 using IDAL;
 8 namespace BLL
 9 {
10     public partial class sysdiagramsBLL: BaseBLL<Model.sysdiagrams>,IsysdiagramsBLL
11     {
12         public override void SetDAL()
13         {
14             idal = DBSession.IsysdiagramsDAL;
15         }
16     }
17     public partial class T001账号表BLL: BaseBLL<Model.T001账号表>,IT001账号表BLL
18     {
19         public override void SetDAL()
20         {
21             idal = DBSession.IT001账号表DAL;
22         }
23     }
24     public partial class T002验证表BLL: BaseBLL<Model.T002验证表>,IT002验证表BLL
25     {
26         public override void SetDAL()
27         {
28             idal = DBSession.IT002验证表DAL;
29         }
30     }
31     public partial class T003用户角色表BLL: BaseBLL<Model.T003用户角色表>,IT003用户角色表BLL
32     {
33         public override void SetDAL()
34         {
35             idal = DBSession.IT003用户角色表DAL;
36         }
37     }
38     public partial class T004社团信息表BLL: BaseBLL<Model.T004社团信息表>,IT004社团信息表BLL
39     {
40         public override void SetDAL()
41         {
42             idal = DBSession.IT004社团信息表DAL;
43         }
44     }
45     public partial class T005票务表BLL: BaseBLL<Model.T005票务表>,IT005票务表BLL
46     {
47         public override void SetDAL()
48         {
49             idal = DBSession.IT005票务表DAL;
50         }
51     }
52     public partial class T006店铺信息表BLL: BaseBLL<Model.T006店铺信息表>,IT006店铺信息表BLL
53     {
54         public override void SetDAL()
55         {
56             idal = DBSession.IT006店铺信息表DAL;
57         }
58     }
59     public partial class T007店铺货物表BLL: BaseBLL<Model.T007店铺货物表>,IT007店铺货物表BLL
60     {
61         public override void SetDAL()
62         {
63             idal = DBSession.IT007店铺货物表DAL;
64         }
65     }
66     public partial class T008海报信息表BLL: BaseBLL<Model.T008海报信息表>,IT008海报信息表BLL
67     {
68         public override void SetDAL()
69         {
70             idal = DBSession.IT008海报信息表DAL;
71         }
72     }
73     public partial class T009社团账号表BLL: BaseBLL<Model.T009社团账号表>,IT009社团账号表BLL
74     {
75         public override void SetDAL()
76         {
77             idal = DBSession.IT009社团账号表DAL;
78         }
79     }
80 }

最后,别忘了在web.config中注册spring.net:

 1 <configuration>
 2     <configSections>
 3         <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
 4 
 5         <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 6     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 7         <sectionGroup name="spring">
 8             <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
 9             <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
10         </sectionGroup>
11     </configSections>
12     <spring>
13         <context>
14             <!--<resource uri="assembly://SpringLover/SpringLover/Objects.xml"/>-->
15             <!--<resource uri="~/Objects.xml"/>
16       <resource uri="~/Objects02.xml"/>
17       <resource uri="~/objects.xml"/>
18             <resource uri="file://objects.xml"/>-->
19             <resource uri="config://spring/objects" />
20         </context>
21         <objects xmlns="http://www.springframework.net">
22             <object id="BLL" type="BLL.T001账号表BLL" singleton="false"></object>
23             <object id="BLLSession" type="BLL.BLLSession,BLL" singleton="false"></object>
24             <object id="DBSessionFactory" type="DAL.DBSessionFactory,DAL" singleton="false" ></object>
25         </objects>
26     </spring>
27   <connectionStrings>

 

 

 

这样,我们就完成了我们整个项目框架的搭建,spring.net+工厂模式。UI层的调用示例如下:

var t001 = operateContext.BLLSession.IT001账号表BLL.GetListBy(m => m.Email ==“....”)[0];

总结:

     我们是在原来的三层框架上做了一个改进,将类的所有实现交给一个工厂去完成,而为了得到这个工厂的实例,我们使用了spring.net的IOC模块,通过读取配置文件动态获得对象。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值