我们在java或者.NET项目中一直都强调分层,分层可以带来很多好处,比如代码层次清晰,一人划分一层,定好接口,便于协同开发,可扩展性好。实际项目中确实要是尽量这样做的,对于.NET 项目而言,DAL,BLL,UIL三层,分别是数据访问层,业务逻辑层和用户界面层。Java程序员则更习惯用DAO进行数据访问,service或者说manager这层来作为业务逻辑,前台用html,jsp等作为用户界面。
具体项目中是怎样应用上去的呢?这样做有什么好处呢?接下来看一下实际项目中的例子,我相信大多数.NET开发的小项目都是按着这样一下架构的。首先简单解释一下这几层大概是什么个意思
1) DAL:说白了,就是数据库操作,拼SQL语句
2)BLL:业务逻辑层
3)UIL:理解为前台界面吧。
调用关系熟悉就是一条线,UIL只能调用BLL层(就是只能添加这层的引用),BLL层只能调用DAL层。项目组织框架如下,大家只需要关注DAL,BLL, UIL层就是了。
1.1 DAL层
首先看一下DAL层里面都有些什么文件?
1.1.1 Model
Model层其实就是定义的实体类,对应java里面vo或者说教entity。以下是一个实体定义类
1.1.2IDAL
所谓面向接口编程,当然要定义接口了,然后SQLDAL,OracleDAL就去实现对应的接口即可,接口里面定义的操作正是数据库的CRUD相关操作
1.1.3DAL
数据库的CRUD操作,放在这个项目里面,对应于oracle数据库我们编写OracleDAL,SQLSever我们编写SQLDAL,他们都继承上述IDAL层中的接口,为多个数据库之间实现切换做好准备。
值得注意的对于SQL数据库的操作有SQLhelper,这个可以去微软的官网网站上下载,对于oracle数据库当然也有OracleHelper,但是这个类在微软可下载不了,必须啊人又不是微软产品,这个得自己编写,还是以前说的,这些源码也会上传到本人github上,欢迎下载。
1.1.4DALFactory
从名字来看,工厂类呗,工厂类拿来干什么?用于生产各个实体的数据库访问类(具体DAL)。
这里有必要贴点源码了。
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
namespace GatMis.DALFactory
{
///<summary>
/// Factory implementaion for the Organ DAL object
///</summary>
public class Dept
{
public static GatMis.IDAL.IDeptCreate()
{
/// Look up the DAL implementation we should be using
stringpath = System.Configuration.ConfigurationSettings.AppSettings["GatDAL"];
stringclassName = path + ".Dept";
// Usingthe evidence given in the config file load the appropriate assembly and class
return(GatMis.IDAL.IDept)Assembly.Load(path).CreateInstance(className);
}
}
}
可以看到这里我们用了Assembly.Load(path).CreateInstance(className);,反射得到具体的DAL操作类,之前我们看了我们被使应用可以方便在sqlserver和oracle数据库之间进行切换准备SQLDAL和OracleDAL,这两个工程编译完成后都会在生成目录文件夹下生成对应的DLL文件,这里我们只需通过配置文件修改,即可加载对应DAL类,实现数据库之间的切换,用到的当然是反射技术了。但是对于外界我们暴漏的只是接口,返回的是IDAL工程下的接口类GatMis.IDAL.IDept。
1.2BLL层
这里我们在看一下业务逻辑层,贴一个实体类的操作源码。如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using GatMis.Model;
using GatMis.IDAL;
using GatMis.DALFactory;
namespace GatMis.BLL
{
///<summary>
/// A business Component used to manage data string
/// The GatMis.Model.DataString is used in most methods
/// and is used to store serializable information about a datastring
///</summary>
public class Operation
{
///<summary>
/// A method to insert a new Data DataString
///</summary>
///<paramname="data">A data string entitywith information about the new data string</param>
public void Insert(OperationInfooperation)
{
//Validate input
if(operation.DBID.Trim() == string.Empty)
return;
if(operation.Name.Trim() == string.Empty)
return;
// Get aninstance of the data batch DAL using the DALFactory
IOperationdal = GatMis.DALFactory.Operation.Create();
// Callthe DAL to insert the string
dal.Insert(operation);
}
///<summary>
/// A method to get all existing operations
///</summary>
///<returns>A operations list</returns>
public OperationInfo GetOperation(string dbid)
{
// Get aninstance of the operation DAL using the DALFactory
IOperationdal = GatMis.DALFactory.Operation.Create();
// getoperation information list to the DAL
returndal.GetOperation(dbid);
}
///<summary>
/// A method to get all existing operations
///</summary>
///<returns>A operations list</returns>
public IList GetOperations()
{
// Get aninstance of the operation DAL using the DALFactory
IOperationdal = GatMis.DALFactory.Operation.Create();
// getoperation information list to the DAL
returndal.GetOperations();
}
}
}
这里我们可以看到对应类的操作,通过工厂方法即可得到。
总结:
UIL层以后再说吧,具体就是一些控件进行相应的布局了。这是一个商业项目中一个典型的.NET开发整体架构,桌面端程序和web程序的区别就在最后一层UIL上,桌面端的用DOTNETBAR,devexpress等进行布局,web程序的就用js,css,html等前台技术即可。基本都是一个套路。现在流行SOA,当然如果需要的话可以在BLL层上再加一个服务层,对外提供web服务,用于系统之间的集成。
-----------Predator_Zhang