三层架构的设计方法对我们并不是很陌生,比如说微软的那个petShop,但是这个例子对于初学者来说有点复杂,我就我在学习的过程中做的一个Demo为例说明一下三层架构的搭建过程。虽然很简单,但是我相信对于初学者来说还是有点抛砖引玉的作用吧。
首先说一下三层架构中的“三层”吧:也就是数据访问层---主要负责直接操作数据库的一些类库的集合;业务逻辑层---指的是整个系统中的所有的业务逻辑的集合;表现层---指的是用户直接接触的界面等。这样做的好处是设计界面的开发人员和设计数据操作和业务逻辑的人员同时进行开发,提高了开发的效率,但是也降低了系统的执行效率。
现在开始以图解的方式说明三层架构系统的搭建:
图一:效果图
现在介绍一下每个类库之间的调用:BLL调用的是DALFactory、Model、IDAL;IDAL调用的是Model;DAL调用的是Model、IDAL、Utility
DALFactory里面只有一个类DataAccess。代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace NPOIReport.DALFactory { public class DataAccess { //通过配置文件反射数据访问类 private static readonly string path = System.Configuration.ConfigurationManager.AppSettings["Dal"]; public static NPOIReport.IDAL.IExcelFile CreateExcelFile() { string className = path + ".DALExcelFile"; return (IDAL.IExcelFile)Assembly.Load(path).CreateInstance(className); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NPOIReport.BLL { public class ExcelFileBLL { private static readonly IDAL.IExcelFile Dal = DALFactory.DataAccess.CreateExcelFile(); public bool AddExcelFile(NPOIReport.Model.ExcelFileInfo excelfile) { return Dal.AddExcelFile(excelfile); } public bool DeleteExcelFile(Guid oid) { return Dal.DeleteExcelFile(oid); } public bool UpdateExcelFile(Guid oid) { return Dal.UpdateExcelFile(oid); } public IList<NPOIReport.Model.ExcelFileInfo> GetAllExcelFile() { return Dal.GetAllExcelFile(); } public NPOIReport.Model.ExcelFileInfo GetExcelFileByOid(Guid oid) { return Dal.GetExcelFileByOid(oid); } } }