L3 architecture instance

Source: Internet
Author: User

I have also checked the three-layer concept and read it. Below is a simple example of the three-layer architecture. Let's take a look at how it is implemented. let's take a look at the Entity class-Model essence: the entity class is to complete the function corresponding to the database and the entity class. One class is a table and one attribute is a field! [Csharp] using System; using System. collections. generic; using System. linq; using System. text; namespace model {public class User {public User () {} private string _ name; public string name {set {_ name = value ;} get {return _ name ;}}}let's take a look at it again. This instance applies the configuration file to access the database. Of course, we can use SqlHelper when doing this, you can also directly put the code for connecting to the database in layer D [csharp] <add name = "ConnectionString" connectionString = "Data Source = localhost; Initial Ca Talog = tester; User ID = sa; Password = 123456 "providerName =" System. data. sqlClient "/> the bottom layer of the layer-data access layer (DAL) must reference the entity class and reference the Configuration in essence: adding, deleting, and modifying the content in the database, check [csharp] using System; using System. collections. generic; using System. linq; using System. text; using model; using System. data; using System. configuration; using System. data. sqlClient; namespace DAL {public class UserDB {public bool User_add (model. use R model) {string setting = ConfigurationManager. connectionStrings ["ConnectionString"]. toString (); SqlConnection myconn = new SqlConnection (setting); myconn. open (); SqlCommand cmd = new SqlCommand ("insert into dbo. [user] ([name]) values (@ name) ", myconn); cmd. parameters. addWithValue ("@ name", model. name); if (cmd. executeNonQuery ()> 0) {return true ;}else {return false ;}}} Layer 3 Bridge-business logic layer BLL needs to reference the entity class And the essence of the data access layer: responsible for handling the U layer issues (this example mainly involves operations on the data layer) [csharp] using System; using System. collections. generic; using System. linq; using System. text; using DAL; namespace BLL {public class userBLL {DAL. userDB db = new UserDB (); public bool addUser (model. user model) {return db. user_add (model) ;}} the top-layer-presentation-layer UI Layer in Layer 3 should reference the entity class and business logic layer essence: Solve the specific problem of what to do [csharp] using System; using System. collections. generic; using System. componen TModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using BLL; using model; namespace login {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {model. user thisUser = new User (); thisUser. name = TB_username.Text.ToString (); BLL. userBLL uB = new userBLL (); If (uB. addUser (thisUser) {MessageBox. show ("true");} else {MessageBox. show ("false") ;}}} relationship between three layers: explanation: In the above Code, DAL is mainly used to operate on the content of the database. Here, it is used to add users to the database. BLL mainly calls the operations on the DAL layer and returns the result (true or false) of Adding users to the DAL layer ). In this way, an intermediate layer is added to the client and database to reduce the dependence between the two layers. The UI Layer mainly responds to user needs and calls the adduser method implemented by The BLL layer. The DAL layer is actually doing this operation.

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.