Example of ASP. NET three-layer structure

Source: Internet
Author: User

The following is a conversion:

Typical three-tier structure example source code (data layer, business layer, presentation layer)
Data Layer:
Data Layer:
Using System;
Using System. Collections;
Using System. Data;
Using System. Data. SqlClient;

Namespace YiPu. WebModules. Accounts. Data
{
///

/// Summary of the 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 getpolictivepermissionlist (string userID)
{
ArrayList permissions = new ArrayList ();
SqlParameter [] parameters = {new SqlParameter ("@ UserID", SqlDbType. VarChar, 18 )};
Parameters [0]. Value = userID;

SqlDataReader tmpReader = RunProcedure ("sp_accounts_get1_tivepermissionlist", 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 );
}

}
}

Stupid

Business Layer:
Using System;
Using System. Data;

Namespace YiPu. WebModules. Accounts. Business
{
///

/// Users summary.
///
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;
}
}
}
}

Stupid

Presentation Layer
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 summary.
///
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: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

///

/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
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 ();
}
}
}

Guobinxian

In simple terms:
Data access layer, business rule layer or entity layer, Presentation Layer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.