Learn from the story-analyze the layer-3 architecture (an ultra-simple system logon layer-3 architecture instance), and learn from the story

Source: Internet
Author: User

Learn from the story-analyze the layer-3 architecture (an ultra-simple system logon layer-3 architecture instance), and learn from the story

I started to get started with the three-tier architecture almost two months ago. At that time, I found a lot of examples and I didn't quite understand it. Today I am idle, so I just turned out what I learned, it is a review. Since I have not been in touch for a long time, the following comments are incorrect.

First, we need to know what a three-tier architecture is. The three-tier architecture I personally understand is to divide the business into the interface layer (UI Layer) and the business logic layer (BLL Layer) and the data access layer (DAL layer). Each layer performs its duties and transmits information layer by layer.

The advantage is that it can achieve high cohesion, low coupling, and easy to modify. The disadvantage is that it will reduce system performance.

UI Layer: it is a user-oriented layer that Directly Interacts with users.

BLL layer: This layer is used to implement business logic between the UI Layer and the DAL layer.

DAL layer: interacts with the database.

Let's start our project!

Open VS and create a windows form application named ThreeLayers.

Right-click solution ----- add ----- new project ---- class library and name it DAL

In the same way, add two projects named BLL and Model to modify the name of each layer:

Next, add reference, right-click UI ---- add ---- reference,

Select BLL and Model. Similarly, reference the DAL and Model for the BLL layer and reference the Model and using System. Data. SqlClient for the DAL layer. You can search for references in assembly ---- search solution.

 

Next, open Form1 on the UI Layer and edit the interface,

There are two labels, two TextBox, and one button, which can be found in the menu view ---- toolbox. Right-click the user's input box-properties, as shown in, change Name to: ID, and change the Name of the password input box to: Pass.

After the interface is completed, let's take a look at the database. This example uses the SQL Server database. Create a table in the database and save it. Then we can write code.

 

The Code is as follows:

UI Layer:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace ThreeLayers {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {// obtain the input username and password string UserCode = ID. tex T. trim (); string PassWord = Pass. text; BLL. bll bb = new BLL. bll (); int uu = bb. select (UserCode, PassWord); if (uu = 0) {MessageBox. show ("Login successful! ");} Else if (uu = 1) {MessageBox. Show (" Incorrect password! ");} Else if (uu = 2) {MessageBox. Show (" No such user! ");}}}}

BLL layer:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using UI; namespace BLL {public class bll {public int Select (string UserCode, string PassWord) {DAL. dal dd = new DAL. dal (); model mm = dd. select (UserCode, PassWord); // compare the input information with the information found in the database if (mm! = Null & mm. UserCode = UserCode) {if (mm. PassWord = PassWord) {return 0 ;}return 1 ;}return 2 ;}}}

DAL layer:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data. sqlClient; using UI; namespace DAL {public class dal {// database connection command. Server. the local server is equivalent to localhost. ", // After initial catalog is your database name, uid is your database login name, And pwd is the login password. String ConText = "server = .; initial catalog = ljndba; uid = chouningning; pwd = chouningning "; public model Select (string UserCode, string PassWord) {model mm = null; try {SqlConnection con = new SqlConnection (ConText); con. open (); // Ning is the name of the table you just created SqlCommand cmd = new SqlCommand ("select * from Ning where UserCode = '" + UserCode + "'", con); SqlDataReader reader = cmd. executeReader (); while (reader. read () {if (mm = null) {mm = new model ();} mm. userCode = reader. getString (0); mm. passWord = reader. getString (reader. getOrdinal ("PassWord") ;}} catch (Exception ex) {} return mm ;}}}

 

Model layer:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace UI{    public class model    {        public string UserCode        {            get;            set;        }        public string PassWord        {            get;            set;        }    }}

The final running result is as follows:

I wrote a blog for the first time. I am sorry for the shortcomings!

 

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.