(实践篇)剖析最近项目使用的一个框架
本篇用到的纯理论知识在此,大抵是引用一些网友的博文:
http://www.cnblogs.com/jangwewe/archive/2013/03/22/2975012.html,理论过后,来看实战
前段时间完成了一个项目,照例过段时间应该总结一下,这里补上,
主要是关于系统架构的总结,这里记下作为以后参考:
1 先来看下系统的总体架构,如图
--------------------------------------
2 各层简要介绍:
bll:逻辑层
commonUtil 通用方法层(如处理字符串,excel导入到处,序列化等方法)
dal 数据层
dbUtil层(主要是封装了访问数据库的一些操作)
dll 包含项目中引用的各种dll
entity 实体层,对应数据库中的每一张表
iterface 接口层,又分为ibll逻辑层接口,和idal数据层接口
web 展示层
-----------------------------------------
3 各层之间的引用关系:
web层,引用所有层
interface层,引用commonUtil,dbUtil,entity层
bll层,引用commonUtil,dbUtil,entity,dal,interface层
dal层,引用commonUtil,dbUtil,entity,interface层
------------------------------------------
4 以一个小例子来看各层展开之后,如图:
页面调用如下:
5 看完这个调用,估计你已经想到了,这里使用的是IOC依赖注入的方式来实现调用,
那么它又是怎么实现的呢,如下:
(1)首先在web.config里新增配置节:
<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=4c5a798e9f2ad114"/> </configSections> <unity configSource="Setting\Unity.config" />
Unity.config是一个标准的xml文件,内容如下图:
(2)commonUitl里新增容器处理类:
public class UnityContainerAccessor { private static IUnityContainer logicUnityContainer = null; /// <summary> /// 获取逻辑层IoC容器 /// </summary> public static IUnityContainer LogicContainer { get { if (logicUnityContainer == null) { IUnityContainer container = new UnityContainer(); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers["LogicContainer"].Configure(container); logicUnityContainer = container; } return logicUnityContainer; } } private static IUnityContainer daoUnityContainer = null; /// <summary> /// 获取DAO层IoC容器 /// </summary> public static IUnityContainer DaoContainer { get { if (daoUnityContainer == null) { IUnityContainer container = new UnityContainer(); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers["DaoContainer"].Configure(container); daoUnityContainer = container; } return daoUnityContainer; } } }
(3)bll层里实现如下,实例如下:
public class ExampleMethodBLL:MyStructOne.INTERFACE.BLL.IExampleBLL { private MyStructOne.INTERFACE.DAL.IExampleDAL _DAL = null; private Database _dataBase; public Database dataBase { get { return _dataBase; } set { _dataBase = value; _DAL.dataBase = value; } } public ExampleMethodBLL() { _DAL = UnityContainerAccessor.DaoContainer.Resolve<MyStructOne.INTERFACE.DAL.IExampleDAL>(); dataBase = DatabaseFactory.CreateDatabase(System.Configuration.ConfigurationManager.ConnectionStrings["ExampleConnectionKey"].ConnectionString); } public DataTable ExampleMethod() { return _DAL.ExampleMethod(); } }
(4)dal层里则是具体的数据操作和实现:
class ExampleMethodDAL:MyStructOne.INTERFACE.DAL.IExampleDAL { private Database _dataBase; public Database dataBase { get { return _dataBase; } set { _dataBase = value; _dataBase = value; } } public ExampleMethodDAL() { } public DataTable ExampleMethod() { string strSql = @"select top(10) * from UM_MENU"; return dataBase.ExecuteDataTable(strSql); } }
以上是抽取项目中的架构做的一个示例,留作参考之用!