黑马程序员----三层架构(没有另建一个BLL业务层--类库)
2013-02-21 11:57
344 查看
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流!
----------------------
分为UI(表现)层、DAL(数据访问)层(BLL层包含在其中)、Model,在添加项目时;如分别添加Winfrom(WPF、控制台、ASP.NET等应用程序,生成的是.exe文件),而DAL和Model添加的是类库,他们生成后都是程序集(.DLL文件),这两个类库都是用来放类的,DAL层放的是有关SQL语句的类,用来访问数据库的,如SQLHelp;而Model是用来放一般类的;原则上"UI层不能包含SQL语句",当然特殊情况下还是可以在UI层写SQL语句的,如多条件的搜索(拼接SQL的字符串如where
@name=name and @age=age.....)
例如.....
代码:
DAL层的SQLHelp类和Common类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace HRMSYSDAL
{
class SqlHelp
{
//获取连接字符串
private static string connectionStr = ConfigurationManager.ConnectionStrings["dbTable"].ConnectionString;
//sql是SQL语句,及插入可变长度参数数组parameter[] 注意类型要一致
public static int ExecuteNonQuery(string sql,params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
con.Open();
using (SqlCommand command = new SqlCommand(sql, con))
{
//将可变参数数组变量添加到集合里
foreach (SqlParameter p in param)
{
command.Parameters.Add(p);
}
int num = command.ExecuteNonQuery();
return num;
}
}
}
//返回一个要查询的表
public static DataTable GetQuery(string sql,params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
using (SqlCommand command = new SqlCommand(sql, con))
{
command.Parameters.AddRange(param); //和上面foreach遍历一样,添加参数到集合里
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
return table;
}
}
}
//返回第一行的第一列数据
public static object ExecuteScalar(string sql, params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
con.Open();
using (SqlCommand command = new SqlCommand(sql, con))
{
foreach (SqlParameter p in param)
{
command.Parameters.Add(p);
}
return command.ExecuteScalar();
}
}
}
}
}
Common类: (应该算是BLL业务层)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSYSModel; //注意DAL层要引用Model层的类,因为我写在DAL层,所以不需要引用DAL层
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Collections;
namespace HRMSYSDAL
{
public class Common
{
//注册管理员信息,向数据库提交信息
public static int PostInfo(Operater p)
{
int num = SqlHelp.ExecuteNonQuery(@"insert into T_Operater(operater,password,isDelete)
values(@operater,@password,@isDelete)", new SqlParameter("@operater", p.OperaterName),
new SqlParameter("@password", p.Password),new SqlParameter("@isDelete",p.IsDelete)); //默认插入false,代表默认"软删除"没删除管理员
return num;
}
}
Model类库的一个Operater类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HRMSYSModel
{
public class Operater
{
public string OperaterName { set; get; }
public string Password { set; get; }
public bool IsDelete { set; get; }
public Guid ID { set; get; }
}
}
UI层(这里用的是winfrom应用程序)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSYSDAL; //注意UI层要引用DAL层和Model层
using HRMSYSModel;
using HRMSYSUI.WinFromSurface;
namespace HRMSYSUI
{
public partial class LogIn : Form
{
public LogIn()
{
InitializeComponent();
}
MainShow MS = new MainShow();
int errorTime = 0;
private void btnLogIn_Click(object sender, EventArgs e)
{
string name = txtLogInUserName.Text.Trim();
string password = txtLogInPassword.Text.Trim();
//验证文本框是否为空
if (Common.CheckTextBox(txtLogInUserName, txtLogInPassword))
{
return;
}
try
{
Common.GetOperater(name); //要是获取不到数据库中用户名,则抛出异常
}
catch
{
MessageBox.Show("用户名不存在!");
return;
}
if(errorTime>=3)
{
MessageBox.Show("错误次数过多,要锁定输入的文本框!");
txtLogInUserName.Enabled = false;
txtLogInPassword.Enabled = false;
return;
}
if (Common.GetPassword(name) == MD5.GetMD5((password + MD5.GetSalt()))&&errorTime<3)
{
MessageBox.Show("登陆成功!");
errorTime = 0;
this.Hide();
MS.Show();
}
else
{
MessageBox.Show("用户名或密码有误!");
errorTime++;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
//加载表数据
public void DGShowInfo()
{
MS.dgShow.DataSource = Common.GetPersonInfo();
}
}
}
而我写的应该也是三层,Common类不就是引用DAL层的SQLHelp类和Model类库嘛,其实它就是BLL业务层!
原则上:三层是-----DAL数据层引用Model类库 BLL业务层引用DAL层和Model类库 UI表现层引用BLL层和Model类库(三层我是这么理解的!望纠正!!)
(注意:以上的代码只是为了举例复制了一部分,不是很完整)
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流!
----------------------
----------------------
分为UI(表现)层、DAL(数据访问)层(BLL层包含在其中)、Model,在添加项目时;如分别添加Winfrom(WPF、控制台、ASP.NET等应用程序,生成的是.exe文件),而DAL和Model添加的是类库,他们生成后都是程序集(.DLL文件),这两个类库都是用来放类的,DAL层放的是有关SQL语句的类,用来访问数据库的,如SQLHelp;而Model是用来放一般类的;原则上"UI层不能包含SQL语句",当然特殊情况下还是可以在UI层写SQL语句的,如多条件的搜索(拼接SQL的字符串如where
@name=name and @age=age.....)
例如.....
代码:
DAL层的SQLHelp类和Common类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace HRMSYSDAL
{
class SqlHelp
{
//获取连接字符串
private static string connectionStr = ConfigurationManager.ConnectionStrings["dbTable"].ConnectionString;
//sql是SQL语句,及插入可变长度参数数组parameter[] 注意类型要一致
public static int ExecuteNonQuery(string sql,params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
con.Open();
using (SqlCommand command = new SqlCommand(sql, con))
{
//将可变参数数组变量添加到集合里
foreach (SqlParameter p in param)
{
command.Parameters.Add(p);
}
int num = command.ExecuteNonQuery();
return num;
}
}
}
//返回一个要查询的表
public static DataTable GetQuery(string sql,params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
using (SqlCommand command = new SqlCommand(sql, con))
{
command.Parameters.AddRange(param); //和上面foreach遍历一样,添加参数到集合里
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
return table;
}
}
}
//返回第一行的第一列数据
public static object ExecuteScalar(string sql, params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
con.Open();
using (SqlCommand command = new SqlCommand(sql, con))
{
foreach (SqlParameter p in param)
{
command.Parameters.Add(p);
}
return command.ExecuteScalar();
}
}
}
}
}
Common类: (应该算是BLL业务层)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSYSModel; //注意DAL层要引用Model层的类,因为我写在DAL层,所以不需要引用DAL层
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Collections;
namespace HRMSYSDAL
{
public class Common
{
//注册管理员信息,向数据库提交信息
public static int PostInfo(Operater p)
{
int num = SqlHelp.ExecuteNonQuery(@"insert into T_Operater(operater,password,isDelete)
values(@operater,@password,@isDelete)", new SqlParameter("@operater", p.OperaterName),
new SqlParameter("@password", p.Password),new SqlParameter("@isDelete",p.IsDelete)); //默认插入false,代表默认"软删除"没删除管理员
return num;
}
}
Model类库的一个Operater类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HRMSYSModel
{
public class Operater
{
public string OperaterName { set; get; }
public string Password { set; get; }
public bool IsDelete { set; get; }
public Guid ID { set; get; }
}
}
UI层(这里用的是winfrom应用程序)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSYSDAL; //注意UI层要引用DAL层和Model层
using HRMSYSModel;
using HRMSYSUI.WinFromSurface;
namespace HRMSYSUI
{
public partial class LogIn : Form
{
public LogIn()
{
InitializeComponent();
}
MainShow MS = new MainShow();
int errorTime = 0;
private void btnLogIn_Click(object sender, EventArgs e)
{
string name = txtLogInUserName.Text.Trim();
string password = txtLogInPassword.Text.Trim();
//验证文本框是否为空
if (Common.CheckTextBox(txtLogInUserName, txtLogInPassword))
{
return;
}
try
{
Common.GetOperater(name); //要是获取不到数据库中用户名,则抛出异常
}
catch
{
MessageBox.Show("用户名不存在!");
return;
}
if(errorTime>=3)
{
MessageBox.Show("错误次数过多,要锁定输入的文本框!");
txtLogInUserName.Enabled = false;
txtLogInPassword.Enabled = false;
return;
}
if (Common.GetPassword(name) == MD5.GetMD5((password + MD5.GetSalt()))&&errorTime<3)
{
MessageBox.Show("登陆成功!");
errorTime = 0;
this.Hide();
MS.Show();
}
else
{
MessageBox.Show("用户名或密码有误!");
errorTime++;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
//加载表数据
public void DGShowInfo()
{
MS.dgShow.DataSource = Common.GetPersonInfo();
}
}
}
而我写的应该也是三层,Common类不就是引用DAL层的SQLHelp类和Model类库嘛,其实它就是BLL业务层!
原则上:三层是-----DAL数据层引用Model类库 BLL业务层引用DAL层和Model类库 UI表现层引用BLL层和Model类库(三层我是这么理解的!望纠正!!)
(注意:以上的代码只是为了举例复制了一部分,不是很完整)
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流!
----------------------
相关文章推荐
- 实战剖析三层架构3:不要说BLL没有用
- Entity Framework 三层架构--业务层使用封装之实现类ServiceTemplate
- 三层架构通用ORM类库 ThreeTierLib v2.0
- K8 系统中省市县数据表的设计可以反映出什么? 通过一个基础业务表的设计品味软件系统的整体架构
- 怎么判断一个ASP.NET的程序是否三层架构
- 怎么判断一个ASP.NET的程序是否三层架构
- 了解c#中的三层架构(DAL,BLL,UI)
- 了解c#中的三层架构(DAL,BLL,UI)
- 黑马程序员_浅谈三层架构
- 两天完成一个小型工程报价系统(三层架构)
- Delphi scktsrvr 三层架构程序,解决“远程主机强迫关闭了一个现有的连接”(2)
- JAVA三层架构,持久层,业务层,表现层的理解
- ASP_NET的三层架构(DAL,BLL,UI)
- MFC原创:三层架构03(人事管理系统)BLL以及总体说明
- 如何构建一个较为通用的业务技术架构
- 架构业务wait和sleep区别狭义jiavaBean规范,三层架构模式
- ASP.NET的三层架构(DAL,BLL,UI)
- 也谈TDD,以及三层架构、设计模式、ORM……没有免费的午餐,选择了,必付出代价
- 黑马程序员---理解三层架构
- 两天完成一个小型工程报价系统(三层架构)