Three-tier architecture-system login instance

Source: Internet
Author: User

A three-tier architecture usually divides the entire business application into: presentation layer (UI), business logic layer (BLL), and data access layer (DAL ). The purpose of hierarchy differentiation is the idea of "high cohesion and low coupling.

I. English expansion:

Three-Tier architecture (3-Tier ASrchitecture)

Presentation Layer UI (User Interface)

Business Logic Layer (BLL)

Data Access Layer (DAL)


Ii. Analysis of the functions of each layer:

1. DAL:

1) load data from the data source Select

2) Insert/Update data written to the data source

3) Delete data from the data source

2. UI functions:

1) present specific business data to users.

2) Collect User input information and operations.

3) display specific data to users

Principle: user first, simple and clear interface

3. Role of BLL:

1) obtain data from the DAL for UI display.

2) obtain user commands and data from the UI and execute business logic.

3) obtain user commands and data from the UI and write data to the data source through DAL.

Responsibilities and Mechanisms of BLL:



4. Introduction of data models:

In order to avoid mutual reference between the three layers, a Model is used to transmit data and business data models.


Iii. System Login instance, steps:

1. Create a database

(Name) LoginDemo, which contains two tables:

Create a table Users

The ID is set as the primary key, which is automatically increased. 

Create a table Scores

The ID is set as the primary key, which is automatically increased. 

2. Encoding phase:

Solution name: LoginSolution

Location: LoginDemo

1) DAL data access layer:

New Project name: LoginDAL

Default namespace: Login. DAL

Add class: UserDAO, ScoreDAO, DbUtil

Reference: LoginModel

Namespace Login. DAL {class DbUtil {// sever machine name, Database name, public static string ConnString = @ "Server = 192. 168. **. **; Database = LoginDemo; User ID = sa; Password = 123456 ";}}// add 10 points for each successful login. Public class ScoreDAO {public void UpdateScore (string userName, int value) {using (SqlConnection conn = new SqlConnection (DbUtil. connString) {SqlCommand cmd = conn. createCommand (); cmd. commandText = @ "insert into scores (UserName, Score) Values (@ UserName, @ Score)"; cmd. parameters. add (new SqlParameter ("@ UserName", userName); cmd. parameters. add (new SqlParameter ("@ Score", value); conn. open (); cmd. ExecuteNonQuery () ;}} public class UserDAO {// return a Boolean Value Based on userName and password. Public Login. model. userInfo SelectUser (string userName, string password) {// with using, connection can automatically disable SqlConnection conn = new SqlConnection (DbUtil. connString); {SqlCommand cmd = conn. createCommand (); cmd. commandText = @ "select id, UserName, Password, Email from users where UserName = @ UserName AND Password = @ Password"; cmd. commandType = CommandType. text; cmd. parameters. add (new SqlParameter ("@ User Name ", userName); cmd. parameters. add (new SqlParameter ("@ Password", password); conn. open (); SqlDataReader reader = cmd. executeReader (); // set the default value of user to null Login. model. userInfo user = null; while (reader. read () {if (user = null) {// if the user is null, the loading of user = new Login will be delayed. model. userInfo ();} user. ID = reader. getInt32 (0); user. userName = reader. getString (1); user. password = reader. getString (2); // not suggest Ion // If Email is not null, it can be read. If (! Reader. IsDBNull (3) {user. Email = reader. GetString (3) ;}} return user ;}}}}


2) UI presentation layer:

Add a new project and a Windows form application.

Name: LoginUI, set as startup project

Default namespace: Login. UI

Reference: LoginBLL, LoginModel


 Login: btnLogin

Username: (Name): txtUserName

Password: (Name): txtPassword; PasswordChar :*

Form: Text: system login; MaximizeBox: False; MinimizeBox: False; FormBorderStyle: FixedSingle


Namespace LoginUI {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnLogin_Click (object sender, EventArgs e) {// usually, use DAO to call the database directly. // IDbConnection conn = new SqlConnection ("c .... "); // IDbCommand cmd = conn. createCommand (); // cmd. commandText = "Select UserName From users where ..... "; // cmd. executeReader (); // use a three-tier architecture, You need to reference the string userName = txtUserName of the next layer. text. trim (); string password = txtPassword. text; Login. BLL. loginManager mgr = new Login. BLL. loginManager (); Login. model. userInfo user = mgr. userLogin (userName, password); MessageBox. show ("Login user:" + user. userName );}}}




3) BLL business logic layer:

Add a new project;

Name: LoginBLL

Default namespace: Login. BLL

Add a new class: LoginManager/LoginService

Reference: LoginDAL, LoginModel 

Namespace Login. BLL {public class LoginManager {public Login. model. userInfo UserLogin (string userName, string password) {// throw new NotImplementedException (); // call the data source to obtain the corresponding data Login. DAL. userDAO uDao = new Login. DAL. userDAO (); Login. model. userInfo user = uDao. selectUser (userName, password); if (user! = Null) // login successful {// If the logon is successful, add 10 points. Login. DAL. ScoreDAO sDao = new Login. DAL. ScoreDAO (); sDao. UpdateScore (userName, 10); return user;} else {throw new Exception ("Logon Failed. ");}}}}

4) Modle Data Model:

Add a new project:

Name: LoginModel

Default namespace: Login. Model

Add class: UserInfo

Model Data Model: The data Model is independent of other layers and does not know the information of the other layers. Between UI and BLL, which indicates that the data we want to return is the USER object.


namespace Login.Model{    public  class UserInfo    {        public int ID { get; set; }        public string UserName { get; set; }        public string Password { get; set; }        public string Email { get; set; }    }}


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: 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.