三层构架的理解
来源:互联网 发布:帕科网络 编辑:程序博客网 时间:2024/05/04 02:58
三层构架在.NET数据库开发中应用很广泛.主要用于讲数据访问层,业务逻辑层,显示层分离开.数据访问层主要用于数据库的访问,数据库增删查改以及数据返回.业务逻辑层是产品的核心包含所有的业务.显示层主要用于解决UI界面怎么显示的问题.
我在平常对.NET数据库的项目开发的时候在借鉴这个思想的基础上细分了这三个层次.分别分成UI层,数据层,业务逻辑层和数据库.先看这张图片
下面我用C#代码具体实现这几个层次
UI层:
设计了一个简单的GUI交互界面,如图:
主要用于实现一个简单的人员信息添加功能.
数据层:
using System;
using System.Collections.Generic;
using System.Text;
namespace PersonInfo
{
class Person
{
private string _name;
private string _sex;
private int _age;
private string _college;
public string name
{
set
{
_name = value;
}
get
{
return _name;
}
}
public string sex
{
set
{
_sex = value;
}
get
{
return _sex;
}
}
public int age
{
set
{
_age = value;
}
get
{
return _age;
}
}
public string college
{
set
{
_college = value;
}
get
{
return _college;
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace PersonInfo
{
class Person
{
private string _name;
private string _sex;
private int _age;
private string _college;
public string name
{
set
{
_name = value;
}
get
{
return _name;
}
}
public string sex
{
set
{
_sex = value;
}
get
{
return _sex;
}
}
public int age
{
set
{
_age = value;
}
get
{
return _age;
}
}
public string college
{
set
{
_college = value;
}
get
{
return _college;
}
}
}
}
主要实现了对人员信息数据结构的封装
业务逻辑层:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace PersonInfo
{
class PersonComman
{
private string _linkStr = "Data Source = localhost;Initial Catalog = 数据库名;Integrated Security = SSPI";
private SqlConnection _myCon;
private SqlCommand _myCom;
/// <summary>
/// 调用存储过程插入人员信息,参数person,存储过程名
/// <returns>成功返回true</returns>
/// </summary>
public bool InsertPersonInfo(ref Person myPerson)
{
_myCon = new SqlConnection();
_myCon.ConnectionString = _linkStr;
try
{
_myCon.Open();
_myCom = new SqlCommand("InsertPerson", _myCon);
_myCom.CommandType = CommandType.StoredProcedure;
SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar, 20);
name.Value = myPerson.name;
_myCom.Parameters.Add(name);
SqlParameter sex = new SqlParameter("@sex", SqlDbType.VarChar, 2);
sex.Value = myPerson.sex;
_myCom.Parameters.Add(sex);
SqlParameter age = new SqlParameter("@age", SqlDbType.Int);
age.Value = myPerson.age;
_myCom.Parameters.Add(age);
SqlParameter college = new SqlParameter("@college", SqlDbType.VarChar, 20);
college.Value = myPerson.college;
_myCom.Parameters.Add(college);
_myCom.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
if (_myCon.State == ConnectionState.Open)
{
_myCon.Close();
_myCom.Dispose();
_myCom = null;
}
_myCon.Dispose();
_myCon = null;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace PersonInfo
{
class PersonComman
{
private string _linkStr = "Data Source = localhost;Initial Catalog = 数据库名;Integrated Security = SSPI";
private SqlConnection _myCon;
private SqlCommand _myCom;
/// <summary>
/// 调用存储过程插入人员信息,参数person,存储过程名
/// <returns>成功返回true</returns>
/// </summary>
public bool InsertPersonInfo(ref Person myPerson)
{
_myCon = new SqlConnection();
_myCon.ConnectionString = _linkStr;
try
{
_myCon.Open();
_myCom = new SqlCommand("InsertPerson", _myCon);
_myCom.CommandType = CommandType.StoredProcedure;
SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar, 20);
name.Value = myPerson.name;
_myCom.Parameters.Add(name);
SqlParameter sex = new SqlParameter("@sex", SqlDbType.VarChar, 2);
sex.Value = myPerson.sex;
_myCom.Parameters.Add(sex);
SqlParameter age = new SqlParameter("@age", SqlDbType.Int);
age.Value = myPerson.age;
_myCom.Parameters.Add(age);
SqlParameter college = new SqlParameter("@college", SqlDbType.VarChar, 20);
college.Value = myPerson.college;
_myCom.Parameters.Add(college);
_myCom.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
if (_myCon.State == ConnectionState.Open)
{
_myCon.Close();
_myCom.Dispose();
_myCom = null;
}
_myCon.Dispose();
_myCon = null;
}
}
}
}
实现了一个基本的数据插入功能,在这个地方我把基本的Insert写成了存储过程.我觉得这样写有这些好处:
(1):存储过程是已经编译好的sql语句,运行于服务器端.所以相对来说提高了程序的效率
(2):存储过程可以防止Sql注入(当然这个例子没有体现这个特点)
(3):程序调试带来了极大的方便.小弟最近写了一个统计签到程序,核心算法写成存储过程,业务逻辑层只要定义好接口并编译好,以后更改变化的可能性很小,在数据库提供的查询分析器上对存储过程算法进行调试分析,不用管业务逻辑层,业务逻辑层的代码在存储过程的改变的时候改动不大也不用重新编译.
数据库层:
只要定义如此存储过程就行了:
Create Procedure InsertPerson
(
@name varchar(20),
@sex varchar(2),
@age int,
@college varchar(20)
)
as
insert into person --假设已经定义这个表
(
name,
sex,
age,
college
)
values
(
@name,
@sex,
@age,
@college
)
go
(
@name varchar(20),
@sex varchar(2),
@age int,
@college varchar(20)
)
as
insert into person --假设已经定义这个表
(
name,
sex,
age,
college
)
values
(
@name,
@sex,
@age,
@college
)
go
那么在UI层只需如此调用就行:
private void addInfor_Click(object sender, EventArgs e)
{
Person myPerson = new Person();
myPerson.name = this.name.Text;
myPerson.sex = this.sex.Text;
myPerson.age = Int32.Parse(this.age.Text);
myPerson.college = this.college.Text;
PersonComman myComman = new PersonComman();
if (myComman.InsertPersonInfo(ref myPerson))
{
MessageBox.Show("添加信息成功!", "信息提示");
}
else
{
MessageBox.Show("添加信息失败!", "错误");
}
myPerson = null;
myComman = null;
}
{
Person myPerson = new Person();
myPerson.name = this.name.Text;
myPerson.sex = this.sex.Text;
myPerson.age = Int32.Parse(this.age.Text);
myPerson.college = this.college.Text;
PersonComman myComman = new PersonComman();
if (myComman.InsertPersonInfo(ref myPerson))
{
MessageBox.Show("添加信息成功!", "信息提示");
}
else
{
MessageBox.Show("添加信息失败!", "错误");
}
myPerson = null;
myComman = null;
}
- 三层构架的理解
- 三层构架的理解
- 三层构架
- 三层构架
- 三层构架
- 三层构架
- asp.net 简单的三层构架例子
- 三层构架与反射工厂的笔记
- 三层构架下的DBHelper.cs类
- 三层构架下的DBHelper.cs类
- 对软件构架的理解
- WFC开始的构架理解
- 三层的理解
- 三层结构的理解
- 三层架构的理解
- 三层架构的理解
- 什么是三层构架
- 三层构架组成
- 编程
- 在C#中如何实现回车
- 自己写了一段assert的实现
- Fork——分道扬镳的开始
- 关于alarm函数
- 三层构架的理解
- C#中的封装
- 设计模式之四--Prototype(原型)
- 二路合并排序算法
- 转贴:《史记 • 王石列传 》
- PB成功导入数据格式
- C#中的抽象
- 什么是IDE
- 物业公司保安工作的管理认识和学习体会