Understanding the three-layer structure

Source: Internet
Author: User

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192452HJ-0.gif "alt =" j_0001.gif "/> the layer-3 structure here is a logical layer-3 structure, not to say that it is executed separately on different machines. It aims to make the project structure clearer and more complete, and facilitate later modification and maintenance.

Three-tier structure: Presentation Layer USL), business logic layer BLL), data access layer DAL)

Presentation Layer: directly accept user commands, convert commands into acceptable forms of the business logic layer, hand them to the business logic layer for execution, and get the returned results from the business logic layer, it is displayed on the interface. The WEB mode or WinForm mode can be used)

Business logic layer: converts the commands passed by the presentation layer into acceptable forms of the data access layer, and submits them to the data access layer for execution. Then, the data access layer returns the results, submit the result to the presentation layer.

Data access layer: converts the passed business logic layer commands into acceptable data access layer commands and submits them to the database for execution. Then, the data access layer returns the results, submit the result to the business logic layer.

This is probably what they mean.

Below is a simple three-tier structure:

SQL Server DALDAL) contains three classes: ClassInfoService, movie service, and DBHelper. The methods in these two classes are basically the same, but they are only operated on two different tables. Only one class is given below:

The common data catalog class DBHelper can access the specified database through this class.

Public class DBHelper {string connString = @ "server =.; database = schoolDB2; uid = sa; pwd = 514420;"; // data operation language. After connecting to the database, add, delete, and change the data to public int ExcuteNoneQury (string SQL, SqlParameter [] ps) {SqlConnection connection = new SqlConnection (connString); SqlCommand command = connection. createCommand (); command. commandText = SQL; command. parameters. addRange (ps); connection. open (); int x = command. executeNonQuery (); connection. close (); return x;} // query the public DataTable GetTable (string SQL, SqlParameter [] ps) {SqlConnection connection = new SqlConnection (connString) as required ); sqlCommand command = connection. createCommand (); command. commandText = SQL; command. parameters. addRange (ps); SqlDataAdapter sda = new SqlDataAdapter (command); DataTable dt = new DataTable (); sda. fill (dt); return dt ;}}

The following is a specific data category

/// <Summary> /// specific data category. Execute a specific add, delete, modify, and query on the table. /// </summary> public class modify Service {DBHelper db = new DBHelper (); public void AddDepart (Depart depart) {string SQL = "Insert into Depart (partition name) values (@ parameter name)"; SqlParameter [] ps = new SqlParameter [] {new SqlParameter ("@ parameter name", depart. parameter Name)}; db. excuteNoneQury (SQL, ps);} public void UpdateDepart (Depart depart) {string SQL = "Update Depart set Partition name = @ brief name where id = @ id "; sqlParameter [] ps = new SqlParameter [] {new SqlParameter ("@ parameter name", depart. parameter Name), new SqlParameter ("@ id", depart. id)}; db. excuteNoneQury (SQL, ps);} public void DeleteDepart (int id) {string SQL = "Delete from Depart where id = @ id "; sqlParameter [] ps = new SqlParameter [] {new SqlParameter ("@ id", id)}; db. excuteNoneQury (SQL, ps);} // query public Depart GetDepart (int ID) {string SQL = "Select * from Depart where id = @ id "; sqlParameter [] ps = new SqlParameter [] {new SqlParameter ("@ id", id)}; DataTable dt = db. getTable (SQL, ps); if (dt. rows. count = 0) {return null;} DataRow dr = dt. rows [0]; Depart depart = new Depart (); depart. id = (int) dr ["Id"]; depart. commonName = (string) dr ["CommonName"]; return depart;} // unconditionally query the public List <Depart> GetDeparts () {string SQL = "Select * from Depart "; sqlParameter [] ps = new SqlParameter [] {}; DataTable dt = db. getTable (SQL, ps); List <Depart> list = new List <Depart> (); // You can query many rows of foreach (DataRow rows in dt. rows) {Depart depart = new Depart (); depart. id = (int) rows ["Id"]; depart. rule name = (string) rows ["rule name"]; list. add (depart) ;}return list ;}}

The business logic layer mainly processes the data in the data access layer and the commands sent from the presentation layer. Here BLL only contains a simple process of data transmitted from DAL. Call)

public class DepartManager    {       static DepartService ds = new DepartService();       public static void AddDepart(Depart depart)        {            ds.AddDepart(depart);        }       public static void UpdateDepart(Depart depart)        {            ds.UpdateDepart(depart);        }       public static void DeleteDepart(int id)        {            ds.DeleteDepart(id);        }       public static Depart GetDepart(int id)        {            return ds.GetDepart(id);        }       public static List<Depart> GetDeparts()        {            return ds.GetDeparts();        }    }


This article is from the "Ajax girl" blog!

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.