以下是转的:
典型的三层结构范例源码(数据层、商务层、表示层)
数据层:
数据层:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace YiPu.WebModules.Accounts.Data
{
///
/// User 的摘要说明。
///
public class User : YiPu.WebModules.Data.DbObject
{
public User(string newConnectionString): base(newConnectionString)
{ }
public bool Create(
string userID,
byte[] password,
string userName,
string company,
string department,
string office,
string duties,
bool sex,
DateTime birth,
string emailAddress,
string mobile,
DateTime registerTime,
DateTime invalidTime,
string question,
string answer,
string country,
string province,
string city,
string code,
string address,
bool isRejected)
{
int rowsAffected;
SqlParameter[]
parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@Password", SqlDbType.Binary, 12),
new SqlParameter("@UserName", SqlDbType.VarChar, 8),
new SqlParameter("@Company", SqlDbType.VarChar, 50),
new SqlParameter("@Department", SqlDbType.VarChar, 10),
new SqlParameter("@Office", SqlDbType.VarChar, 10),
new SqlParameter("@Duties", SqlDbType.VarChar, 10),
new SqlParameter("@Sex", SqlDbType.Bit),
new SqlParameter("@Birth", SqlDbType.DateTime),
new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50),
new SqlParameter("@Mobile", SqlDbType.VarChar, 10),
new SqlParameter("@RegisterTime", SqlDbType.DateTime),
new SqlParameter("@InvalidTime", SqlDbType.DateTime),
new SqlParameter("@Question", SqlDbType.VarChar, 50),
new SqlParameter("@Answer", SqlDbType.VarChar, 50),
new SqlParameter("@Country", SqlDbType.VarChar, 10),
new SqlParameter("@Province", SqlDbType.VarChar, 10),
new SqlParameter("@City", SqlDbType.VarChar, 10),
new SqlParameter("@Code", SqlDbType.VarChar, 6),
new SqlParameter("@Address", SqlDbType.VarChar, 50),
new SqlParameter("@IsRejected", SqlDbType.Bit)
};
parameters[0].Value = userID;
parameters[1].Value = password;
parameters[2].Value = userName;
parameters[3].Value = company;
parameters[4].Value = department;
parameters[5].Value = office;
parameters[6].Value = duties;
parameters[7].Value = sex;
parameters[8].Value = birth;
parameters[9].Value = emailAddress;
parameters[10].Value = mobile;
parameters[11].Value = registerTime;
parameters[12].Value = invalidTime;
parameters[13].Value = question;
parameters[14].Value = answer;
parameters[15].Value = country;
parameters[16].Value = province;
parameters[17].Value = city;
parameters[18].Value = code;
parameters[19].Value = address;
parameters[20].Value = isRejected;
RunProcedure("sp_Accounts_CreateUser", parameters, out rowsAffected);
return (rowsAffected == 1);
}
public DataRow Retrieve(string userID)
{
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;
using (DataSet users = RunProcedure( "sp_Accounts_GetUserDetails", parameters, "Users" ))
{
return users.Tables[0].Rows[0];
}
}
public bool Update(
string userID,
string userName,
byte[] password,
string company,
string department,
string office,
string duties,
bool sex,
DateTime birth,
string emailAddress,
string mobile,
DateTime registerTime,
DateTime invalidTime,
string question,
string answer,
string country,
string province,
string city,
string code,
string address,
bool isRejected)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@Password", SqlDbType.Binary, 20),
new SqlParameter("@UserName", SqlDbType.VarChar, 8),
new SqlParameter("@Company", SqlDbType.VarChar, 50),
new SqlParameter("@Department", SqlDbType.VarChar, 10),
new SqlParameter("@Office", SqlDbType.VarChar, 10),
new SqlParameter("@Duties", SqlDbType.VarChar, 10),
new SqlParameter("@Sex", SqlDbType.Bit),
new SqlParameter("@Birth", SqlDbType.DateTime),
new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50),
new SqlParameter("@Mobile", SqlDbType.VarChar, 10),
new SqlParameter("@RegisterTime", SqlDbType.DateTime),
new SqlParameter("@InvalidTime", SqlDbType.DateTime),
new SqlParameter("@Question", SqlDbType.VarChar, 50),
new SqlParameter("@Answer", SqlDbType.VarChar, 50),
new SqlParameter("@Country", SqlDbType.VarChar, 10),
new SqlParameter("@Province", SqlDbType.VarChar, 10),
new SqlParameter("@City", SqlDbType.VarChar, 10),
new SqlParameter("@Code", SqlDbType.VarChar, 6),
new SqlParameter("@Address", SqlDbType.VarChar, 50),
new SqlParameter("@IsRejected", SqlDbType.Bit),
};
parameters[0].Value = userID;
parameters[1].Value = userName;
parameters[2].Value = password;
parameters[3].Value = company;
parameters[4].Value = department;
parameters[5].Value = office;
parameters[6].Value = duties;
parameters[7].Value = sex;
parameters[8].Value = birth;
parameters[9].Value = emailAddress;
parameters[11].Value = mobile;
parameters[12].Value = registerTime;
parameters[13].Value = invalidTime;
parameters[14].Value = question;
parameters[15].Value = answer;
parameters[16].Value = country;
parameters[17].Value = province;
parameters[18].Value = city;
parameters[19].Value = code;
parameters[20].Value = address;
parameters[21].Value = isRejected;
RunProcedure("sp_Accounts_UpdateUser", parameters, out rowsAffected);
return (rowsAffected == 1);
}
public bool Delete(string userID)
{
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
int rowsAffected;
parameters[0].Value = userID;
RunProcedure( "sp_Accounts_DeleteUser", parameters, out rowsAffected );
return (rowsAffected == 1);
}
public int ValidateLogin(string userID, byte[] encPassword)
{
int rowsAffected;
SqlParameter[] parameters =
{
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@EncryptedPassword", SqlDbType.Binary, 20)
};
parameters[0].Value = userID;
parameters[1].Value = encPassword;
return RunProcedure("sp_Accounts_ValidateLogin", parameters, out rowsAffected);
}
public int TestPassword(string userID, byte[] encPassword)
{
int rowsAffected;
SqlParameter[] parameters =
{
new SqlParameter("@UserID", SqlDbType.VarChar,18),
new SqlParameter("@EncryptedPassword", SqlDbType.Binary, 20)
};
parameters[0].Value = userID;
parameters[1].Value = encPassword;
return RunProcedure("sp_Accounts_TestPassword", parameters, out rowsAffected);
}
public ArrayList GetUserRoles( string userID )
{
ArrayList roles = new ArrayList();
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;
SqlDataReader tmpReader = RunProcedure("sp_Accounts_GetUserRoles", parameters);
while (tmpReader.Read())
{
roles.Add( tmpReader.GetString(1) );
}
Connection.Close();
return roles;
}
public ArrayList GetEffectivePermissionList( string userID )
{
ArrayList permissions = new ArrayList();
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;
SqlDataReader tmpReader = RunProcedure("sp_Accounts_GetEffectivePermissionList", parameters);
while (tmpReader.Read())
{
permissions.Add( tmpReader.GetInt32(0) );
}
Connection.Close();
return permissions;
}
public DataSet GetUserList()
{
return RunProcedure("sp_Accounts_GetUsers", new IDataParameter[]{}, "Users");
}
public bool AddRole(string userID, int roleID)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@RoleID", SqlDbType.Int, 4)
};
parameters[0].Value = userID;
parameters[1].Value = roleID;
RunProcedure("sp_Accounts_AddUserToRole", parameters, out rowsAffected);
return (rowsAffected == 1);
}
public bool RemoveRole(String userID, int roleID)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar,18),
new SqlParameter("@RoleID", SqlDbType.Int,4 )
};
parameters[0].Value = userID;
parameters[1].Value = roleID;
RunProcedure("sp_Accounts_RemoveUserFromRole", parameters, out rowsAffected);
return (rowsAffected == 1);
}
}
}
商务层:
using System;
using System.Data;
namespace YiPu.WebModules.Accounts.Business
{
///
/// Users 的摘要说明。
///
public class User : YiPu.WebModules.Business.BizObject
{
private Configuration.ModuleSettings moduleSettings;
private string userID;
private byte[] password;
private string userName;
private string company;
private string department;
private string office;
private string duties;
private bool sex;
private DateTime birth;
private string emailAddress;
private string mobile;
private DateTime registerTime;
private DateTime invalidTime;
private string question;
private string answer;
private string country;
private string province;
private string city;
private string code;
private string address;
private bool isRejected;
public User()
{
moduleSettings = Configuration.ModuleConfig.GetSettings();
}
private void LoadFromID()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
DataRow userRow = dataUser.Retrieve( userID );
password=(byte[])userRow["Password"];
userName=userRow["UserName"].ToString();
company=userRow["Compandy"].ToString();
department=userRow["Department"].ToString();
office=userRow["Office"].ToString();
duties=userRow["Duties"].ToString();
sex=bool.Parse(userRow["Sex"].ToString());
birth=DateTime.Parse(userRow["Birth"].ToString());
emailAddress=userRow["emailAddress"].ToString();
mobile=userRow["Mobile"].ToString();
registerTime=DateTime.Parse(userRow["RegisterTime"].ToString());
invalidTime=DateTime.Parse(userRow["InvalidTime"].ToString());
question=userRow["Question"].ToString();
answer=userRow["Answer"].ToString();
country=userRow["Country"].ToString();
province=userRow["Province"].ToString();
city=userRow["City"].ToString();
code=userRow["Code"].ToString();
address=userRow["Address"].ToString();
isRejected=bool.Parse(userRow["IsRejected"].ToString());
}
public User( string existingUserID )
{
moduleSettings = Configuration.ModuleConfig.GetSettings();
userID = existingUserID;
LoadFromID();
}
public User( IESPrincipal existingPrincipal )
{
moduleSettings = Configuration.ModuleConfig.GetSettings();
userID = ((IESIdentity)existingPrincipal.Identity).UserID;
}
public bool Create()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
return dataUser.Create(userID,password,userName,company,department,
office,duties,sex,birth,emailAddress,mobile,registerTime,invalidTime,
question,answer,country,province,city,code,address,isRejected);
}
public bool Update()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
return dataUser.Update(userID,userName,password,company,department,
office,duties,sex,birth,emailAddress,mobile,registerTime,invalidTime,
question,answer,country,province,city,code,address,isRejected);
}
public bool Delete()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
return dataUser.Delete(userID);
}
public bool AddToRole(int RoleID)
{
Data.User dataUser = new Data.User( moduleSettings.ConnectionString );
return dataUser.AddRole(UserID, RoleID);
}
public bool RemoveRole(int RoleID)
{
Data.User dataUser = new Data.User( moduleSettings.ConnectionString );
return dataUser.RemoveRole( UserID, RoleID );
}
public string UserID
{
get
{
return userID;
}
set
{
userID=value;
}
}
public byte[] Password
{
get
{
return password;
}
set
{
password=value;
}
}
public string UserName
{
get
{
return userName;
}
set
{
userName=value;
}
}
public string Company
{
get
{
return company;
}
set
{
company=value;
}
}
public string Department
{
get
{
return department;
}
set
{
department=value;
}
}
public string Office
{
get
{
return office;
}
set
{
office=value;
}
}
public string Duties
{
get
{
return duties;
}
set
{
duties=value;
}
}
public bool Sex
{
get
{
return sex;
}
set
{
sex=value;
}
}
public DateTime Birth
{
get
{
return birth;
}
set
{
birth=value;
}
}
public string EmailAddress
{
get
{
return emailAddress;
}
set
{
emailAddress=value;
}
}
public string Mobile
{
get
{
return mobile;
}
set
{
mobile=value;
}
}
public DateTime RegisterTime
{
get
{
return registerTime;
}
set
{
registerTime=value;
}
}
public DateTime InvalidTime
{
get
{
return invalidTime;
}
set
{
invalidTime=value;
}
}
public string Question
{
get
{
return question;
}
set
{
question=value;
}
}
public string Answer
{
get
{
return answer;
}
set
{
answer=value;
}
}
public string Country
{
get
{
return country;
}
set
{
country=value;
}
}
public string Province
{
get
{
return province;
}
set
{
province=value;
}
}
public string City
{
get
{
return city;
}
set
{
city=value;
}
}
public string Code
{
get
{
return code;
}
set
{
code=value;
}
}
public string Address
{
get
{
return address;
}
set
{
address=value;
}
}
public bool IsRejected
{
get
{
return isRejected;
}
set
{
isRejected=value;
}
}
}
}
表示层
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using YiPu.WebModules.Accounts.Business;
namespace YiPu.Modules.Users
{
///
/// Users 的摘要说明。
///
public class Users : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button DeleteButton;
protected System.Web.UI.WebControls.DataGrid UsersGrid;
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
BindUsersGrid();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.DeleteButton.Click = new System.EventHandler(this.DeleteButton_Click);
this.UsersGrid.PageIndexChanged = new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.UsersGrid_PageIndexChanged);
this.Load = new System.EventHandler(this.Page_Load);
}
#endregion
private void BindUsersGrid()
{
DataView dv=AccountsTool.GetUserList().Tables["Users"].DefaultView;
UsersGrid.DataSource=dv;
UsersGrid.DataBind();
}
private void UsersGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
UsersGrid.CurrentPageIndex=e.NewPageIndex;
BindUsersGrid();
}
private void DeleteButton_Click(object sender, System.EventArgs e)
{
for(int i=0;i {
HtmlInputCheckBox checkBox=(HtmlInputCheckBox)UsersGrid.Items[i].FindControl("CheckBox");
if(checkBox.Checked==true)
{
YiPu.WebModules.Accounts.Business.User deleteUser=new YiPu.WebModules.Accounts.Business.User();
deleteUser.UserID=checkBox.Value;
deleteUser.Delete();
}
}
BindUsersGrid();
}
}
}
简单的来说就是:
数据访问层,商业规则层或是实体层,表示层