Posted on 2007-01-18 12:45
东人EP 阅读(1788)
评论(1) 编辑 收藏 引用 所属分类:
.NET
三层的划分:
目的:高内聚,低耦合,为更好的实现分布式开发和维护。
用户界面层: 页面,用户控件,图片,样式表
业务逻辑层:业务接口( BusinessInterface ), 业务实体( BusinessEntity),业务逻辑(BusinessLogic)
数据访问层: 数据实现(DataAccessSQLServer), 数据工具(DataAccessTool),数据工厂( DataAccessFactory)
数据访问
数据访问项目执行从数据库(或其他数据服务)获取数据或向数据库发送数据的功能。在分布式应用程序结构中,相应功能使用 ADO.NET 数据适配器和 SQL 服务器存储过程来完成。
"数据访问"层:
(1)从"业务规则"层接收请求,从"数据服务"获取数据或向其发送数据。
(2)使用存储过程获取数据,并可选用 ADO.NET 向数据库发送数据。
(3)将数据库查询结果返回到"业务规则"层,作为ADO.NET 数据集。
业务规则
业务规则项目包含业务对象本身以及应用于它们的规则。这也是主要业务对象所在的位置。它们实现业务实体或系统对象。系统的业务规则将在这些对象中编码,尽管部分业务规则可能实际上已在数据库的存储过程和触发器中进行了编码。
"业务规则"层:
(1) 从“用户界面”层接受请求。
(2) 根据编码的业务规则处理请求。
(3) 从“数据访问”层获取数据或将数据发送到“数据访问”层。
(4)将处理结果传递回“用户界面”层。
用户界面
用户界面项目是指在应用程序中实现的客户端。在分布式应用程序结构中,用户服务可以是 Web 客户端或 Windows 客户端,这具体取决于特定的应用程序。例如,在开发Web 应用程序时,可能需要提供具有标准 Windows 用户界面或 Web 用户界面。通常,这种一般类型的应用程序包含以下功能:
"用户界面"层:
(1)管理 Web 页或 Windows 界面的呈现和行为
(2)显示数据
(3)捕获数据
(4)数据验证检查
(5)为用户提供任务指南
(6)向“业务规则”发送用户输入
(7)从“业务规则”接收结果
(8)向用户显示错误
下面是我用三层做的一个例子: 实现简单的用户注册:
首先在WebConfig里面添加以下代码:
<
appSettings
>
<
add key
=
"
SqlServer
"
value
=
"
server=.;database=kabaadmin;uid=sa;pwd=
"
></
add
>
</
appSettings
>
搭建三层的文件结构,如下图所示:

1.业务实体层(BusinessEntity)

using
System;
namespace
BusinessEntity

{

/**/
///
<summary>
///
Cuser 的摘要说明。
///
</summary>
public
class
CUser

{
private
string
username;
//
用户帐号
private
string
userpwd;
//
用户密码
public
string
UserName

{

get
{
return
username;}
set
{username
=
value;}
}
public
string
UserPwd

{

get
{
return
userpwd;}
set
{userpwd
=
value;}
}
}
}
2.接口层(BusinessInterface), 添加业务实体层的引用

using System;

namespace BusinessInterface


{

/**//// <summary>
/// ICuser 的摘要说明。
/// </summary>
public interface ICUser

{

/**//// <summary>
/// 向用户表中插入用户信息
/// </summary>
/// <param name="be">用户信息</param>
/// <returns>执行的行数</returns>
int InsertUser(BusinessEntity.CUser be);
}
}

3.数据实现层(DataAccessSQLServer), 添加业务逻辑层,接口层,数据工具层的引用 
using System;
using System.Data;//添加数据操作命名空间
using System.Data.SqlClient;//添加SQL数据操作命名空间
using DataAccessTool;//添加数据工具命名空间

namespace DataAccessSQLServer


{

/**//// <summary>
/// CUser 的摘要说明。
/// </summary>
public class CUser :BusinessInterface.ICUser//实现接口(按Tab键自动添加接口定义的方法)

{
private const string INSERT_USER="insert into users(username,userpwd) values(@username,@userpwd)";//定义SQL语句

ICUser 成员#region ICUser 成员
public int InsertUser(BusinessEntity.CUser be)//实现接口方法

{
SqlParameter [] pas=new SqlParameter[2];//定义参数
pas[0]=new SqlParameter("@username",SqlDbType.VarChar);
pas[0].Value=be.UserName;
pas[1]=new SqlParameter("@userpwd",SqlDbType.VarChar);
pas[1].Value=be.UserPwd;
return SqlHelper.ExecuteNonQuery(SqlHelper.CONNSTR,CommandType.Text,INSERT_USER,pas);//执行
}
#endregion
}
} 4.数据工厂(DataAccessFactory),添加接口层,数据实现层的引用

using System;

namespace DataAccessFactory


{

/**//// <summary>
/// Cuser 的摘要说明。
/// </summary>
public class CUser

{
public static BusinessInterface.ICUser CreateFace()

{
System.Reflection.Assembly getface=System.Reflection.Assembly.Load("DataAccessSQLServer");//反射
BusinessInterface.ICUser face;//定义接口
face=getface.CreateInstance("DataAccessSQLServer.CUser")as BusinessInterface.ICUser;//找到实现接口的类,并转为接口类型
return face;//返回实现了接口方法的类
}
}
} 5.业务逻辑层(BusinessLogic).添加业务实体层,接口层,数据工厂层的引用 
using System;

namespace BusinessLogic


{

/**//// <summary>
/// Cuser 的摘要说明。
/// </summary>
public class CUser

{
public int InsertUser(BusinessEntity.CUser be)

{
BusinessInterface.ICUser face;//定义接口
face=DataAccessFactory.CUser.CreateFace();//接口=数据工厂中实现了该接口的方法
return face.InsertUser(be);//返回影响的行数
}
}
} 6.数据工具层(DataAccessTool),其中SQLHelper.cs类里封装了对数据库的操作,详见ADO.NET分类的SQLHelper.cs类的实现
7.界面层调用
点击注册的代码为:
private void btnServer_Click(object sender, System.EventArgs e)

{
BusinessEntity.CUser be=new BusinessEntity.CUser();//业务实体层
be.UserName=this.txtUserName.Text;//界面层赋值给实体层
be.UserPwd=this.txtPWD.Text;
BusinessLogic.CUser bl=new BusinessLogic.CUser();//业务逻辑层
bl.InsertUser(be);//调用InsertUser方法实现注册功能.
}
