Three-tier Architecture brief example

Source: Internet
Author: User

The three-tier architecture is divided into: Presentation layer (UI), Business Logic Layer (BLL), data access Layer (DAL) plus entity class Library (Model)

Reprint please indicate from Zhu Zhu Homeland http://blog.csdn.net/zhgl7688

1, the Entity class Library (Model), mainly the table field in the database.

Operation:

(1) First set up the entity class Library model, open the project, in the solution right--"add-" New Project-"Selected class Library-" renamed model--"determined

(2) Select Model Class Library--"shift+alt+c--" to establish the entity class. UserInfo class

namespace model{   public  class UserInfo    {public        string  UserName {get; set;}        public string  Password {get; set;}}    }


2, the data Access Layer (DAL), mainly to store the data class access, that is, add to the database, delete, modify, update and other basic operations

Operation:

(1) First set up the data Access Layer Class library Dal, open the project, in the solution right--"add-" New Project-"Selected class Library-" renamed dal--"determined

(2) Add a reference to the model in the DAL, select dal--"alt+p+r--" solution-"Project-" Select model--"to determine

(3) Add a reference to the system.configuration in the DAL, check dal--"alt+p+r--" assembly-"frame--" check system.configuration--"to determine

(4) Establish data access class, check dal--"shift+alt+c--" to establish data access class. UserDB class

Using system.configuration;using model;using system.data;using system.data.sqlclient;namespace DAL{    class UserDB    {        private string connstring = Configurationmanager.connectionstrings[connstring]. ToString ();        public int AddUser (UserInfo UserInfo)        {            //Add a user action to database input            string commandtext = INSERT INTO UserInfo (userName , Password) VALUES (@userName, @Password);            Sqlparameter[] paras = new sqlparameter[]            {            new SqlParameter (@userName, Userinfo.username),           new SqlParameter (@Password, Userinfo.password)            };            Return Sqlhelper.executenonquery (connstring, CommandType.Text, CommandText, paras);        }    }
Add additional to database operations}

3, the Business Logic layer (BLL) to transmit the data to determine the logical division, and to transmit the correct value.

(1) First establish the Business Logic Layer Class library BLL, open the project, in the solution right-"add-" New Project-"Selected class Library-" renamed bll--"determined

(2) Add a reference to model, DAL in the BLL, select bll--"alt+p+r--" solution-"project-" Selected model, dal--"to determine

(3) Establish business logic class, check bll--"shift+alt+c--" to establish business logic class. Loginmanager class

Using dal;using model;namespace bll{public    class Loginmanager    {        private UserDB UserDB = new UserDB ();        public bool Add (UserInfo UserInfo, out string messagestr)        {            messagestr =;//return interface layer Add user return information            bool issuccess = FA LSE;            if (UserInfo.UserName.Trim (). Length! = 0)//Determine if the username from the pass is empty            {                if (userdb.isequals (userInfo))//pass to Dall operation to determine if there are duplicate values in the database                {                    Userdb.adduser (userInfo);//Pass the DAL operation to add a new user                    issuccess = true;                }                else                    messagestr = has the same value;            }            else            {                Messagestr = cannot be empty;            }            Return issuccess;//returns whether the interface layer was added successfully}}}    


5, the presentation layer (UI) is the user interface layer

(1) Add a reference to the model, the BLL in the UI, select ui--"alt+p+r--" solution-"project-" Selected model, bll--"determined

(2) Write code to pass data to the BLL layer.

        UserInfo UserInfo;        Loginmanager lm = new Loginmanager ();        private void btnAdd_Click (object sender, EventArgs e)        {            userInfo = new UserInfo ()            {                UserName = TxtUserName.Text.Trim (),                Password = TxtPassword.Text.Trim ()            };            string messagestr =;            if (LM. Add (UserInfo, out  messagestr))            {                MessageBox.Show (added successfully);            }            else            {                MessageBox.Show (MESSAGESTR);                Txtusername.focus ();}}}    

Three-tier Architecture brief example

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.