[To] Asp.net 2.0 three-tier architecture creation steps add reference steps

Source: Internet
Author: User

Add a project, but add -- new project"

 

The three-layer architecture is the database access layer, business logic layer, and user interface layer (User Interface Layer). The three layers are Dal, BLL, and UIL.

The data access layer provides some common functions for database operations. The business logic layer calls these functions to complete some functions, and the user performance Layer calls the functions at the business logic layer to use these functions.

It is relatively simple to say, but how is it implemented? Don't worry, just give you a small example to understand.

1,Open vs2005 and create a blank solution. Be sure to create a blank solution instead of a project or website. Click Create Project in the menu bar. Select another project type-> Visual Studio solution. Select a blank solution for the template on the right, enter a name, select a location, and click OK.

2,Open Solution Explorer, right-click the solution name to create a project, choose class library, select C # in the language, and name it Dal. use the same method to create a new project named Bll, and then create an ASP.. Net website, which is named UIL.

3,Add a project BLL reference to the project Dal. And the reference of the website UIL to the project BLL. In resource manager, right-click the project name and add a reference. In the displayed dialog box, select the project tab, select an appropriate project, and click OK.

4,Add a new class file named dataaccess to the project Dal. CS. then, write common methods for accessing database or XML files in such files for BLL layer calls. I usually use a Microsoft Enterprise Library component that calls the method for operating the database.

Let's take a simple example. Assume that the following function returns a DataSet object based on the passed query statement.

/// <Summary>

/// Execute the query and return the dataset

/// </Summary>

/// <Param name = "connectionname"> connection string name </param>

/// <Param name = "commandtype"> command type (stored procedure or T-SQL query statement) </param>

/// <Param name = "command"> stored procedure name or T-SQL query statement </param>

/// <Returns> dataset </returns>

Public static dataset executedataset (querytype commandtype, string command)

{

Sqlconnection mysqlconnection = new sqlconnection (myconnectionstring );

......

Mysqlconnection. Close ();

Return mydataset;

}

5,Assume that we need to implement a function in the user performance layer, that is, to obtain detailed information of a user based on a user ID. Design this function at the business logic layer.

Add a new class theuser To The bll project.

Some of the Code is:

Public class theuser

{

...

/// <Summary>

/// Return the user details based on the user ID

/// </Summary>

/// <Param name = "userid"> User ID </param>

/// <Returns> dataset </returns>

 

Public static dataset getuserinfobyid (string userid) // The function is defined as a static function and can be directly called without declaring a new class.

{

String strsql = "select * From userinfo where userid =" + userid;

Return Dal. dataaccess. executedataset (commandtype. Text, strsql); // call the data access layer function and the dal is its namespace.

}

 

...

}

 

6,Call this function at the user interface layer.

Create a page on the UIL website and add the following code to the function to be called in its. CS file:

...

String userid = "001 ";

Dataset dsuserinfo = BLL. theuser. getueserinfobyid (userid); // call the business logic layer function. Bll is the namespace

Datatabel dtuserinfo = dsuserinfo. Tables [0];

...

This is the general structure of the three layers. The advantage is that the structure is clear, the function module is clear, and exceptions can be quickly located and eliminated. The code developed in this mode is very elegant and concise, which is especially convenient for others to read.

In addition, I would like to tell you a tips for using vs2005. You can try it if you do not know it yet. When you write a function to add comments, name three in the above line of the function name/(C # language is three/, VB is three '). Then you will find that the program automatically has a clear hierarchy of comments. You only need to add your comment in the place where the comment should be added. This annotation looks pretty, and you will be able to see its dynamic prompt when calling it later.

 

 

Data access layer: it is also known as the persistent layer. Its function is mainly to access the database. Simply put, select, insert, update, and delete operations on data tables are implemented. If you want to add an ORM element, it will include mapping between the object and the data table, and object Object persistence. In the data access layer of petshop, Orm is not used, which leads to an increase in the amount of code. It can be seen as a major failure in the entire design implementation. Business logic layer: it is the core of the entire system and is related to the business (domain) of the system. Taking petshop as an example, the related design of the business logic layer is related to the unique logic of pet shops on the Internet, such as querying pets, placing orders, adding pets to the shopping cart, and so on. If database access is involved, the data access layer is called. Presentation Layer: it is the UI part of the system and is responsible for user interaction with the entire system. In this layer, the ideal state should not include the business logic of the system. The logic code in the presentation layer is only related to the interface elements. In petshop, ASP. NET is used for design. Therefore, it contains many Web controls and related logic. Note: here is the dotted line =============================================== = Benbo comments: the first half is still capable of learning the node spectrum, and the second half is a bit messy. I don't think it's three layers, but it's just two layers. In addition, if I use dataaccess for a tutorial, it's better to use sqlhelper for example, the entry-level users can leave the door to their work. What kind of professional data access library do they not understand? In addition, they did not highlight the role of the Dal layer. They directly access dataaccess through BLL, keep in mind the Party's underground work discipline and keep in touch with each other! It's dangerous not to go beyond the level! In general, the second half will make beginners confused about the relationship between the three layers. I don't understand it. I can only understand it after I get it from other places two years later. If I didn't understand it elsewhere, it cannot be understood for such a long time, it's hard for me to save what I shouldn't have understood for so long and read it elsewhere. Then I came back to see how I shouldn't have read it. Tang Miao, you're naughty again: if you want me to understand it, you can make it clear. It is impossible for you to make it clear. I can understand it clearly. Do you understand it!
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.