Asp. NET three-tier architecture (DAL,BLL,UI)

Source: Internet
Author: User

The BLL is the business logic layer

The DAL is the data access layer

Asp. NET three-tier architecture (DAL,BLL,UI)

The graph represents a three-layer structure. Where the web is the USL layer

web–> bll–> Dal |          |           | | V | +–> Model <-+

One or three-tier architecture 1. Presentation Layer (USL): The primary means of the web, or it can be represented as a WinForm mode.   If the logical layer is quite powerful and complete, the logic layer can provide services perfectly, regardless of how the presentation layer is defined and changed. 2. Business Logic Layer (BLL): Mainly for the specific problem of operation, can also be understood as the operation of the data layer, the data business logic processing.   If the data layer is a building block, the logical layer is the building blocks. 3. Data access Layer (DAL): Mainly for the original data (database or text files, such as the form of data storage) operations layer, rather than the original data, that is, the operation of the data, rather than the database, specifically the business logic layer or presentation layer

Provide data services.

Second, the specific Distinction 1. Presentation layer: The primary acceptance of the user's request, as well as the return of the data, provides the client with access to the application.   2. Business Logic Layer: mainly responsible for the operation of the data layer, that is, some data layer operations to combine. 3. Data Access layer: The main view of your data layer contains no logical processing, in fact, his various functions mainly to complete the operation of the data files, without the need to control other operations.

Third, the summary three layer structure is a strict layering method, that is, the data Access layer (DAL) can only be accessed by the Business Logic layer (BLL), the business logic layer can only be accessed by the presentation layer (USL), the user through the presentation layer to the business logic layer, the business logic layer to complete the relevant business rules and logic, and access the database through the data access layer to get the data, and then return in reverse order to display the data in the presentation layer. Some three-storey structure also add factory, model and other layers, the actual is on the basis of the three layer of an extension and application.

A simple three-layer structure program typically includes the DAL BLL Web model for several projects, their reciprocal referential relationship as follows 1) Web Reference Bll,model 2) BLL reference Dal,model 3) DAL Reference Model 4) Model no reference

The

  Three-tier architecture is known as the presentation Layer (UI), the Business Logic layer (BLL), and the data Access Layer (DAL), and there are many ways to subdivide each layer. But the specific code how to write, in the end those files on which layer, but is vague. The following is a simple example to lead you to the actual three-tier architecture project, this example has only one function, is the user's simple management.  

     first create a blank solution, add the following projects and files        1, add the ASP. NET WEB Application project, named UI, Create a new Web Form type file user.aspx (including User.aspx.cs)        2, add ClassLibrary project, named BLL, Create a new class type file userbll.cs       3, add the ClassLibrary project, name the DAL, and create a new class type file UserDAL.cs. Add a sqlhelper reference. (This is the Microsoft Data Access class, you can also write all the data access code directly.) I usually use my own data to access the class Dataaccesshelper).        4, add ClassLibrary project, name model, new class type file usermodel.cs        5, add ClassLibrary project, named Idal, new interface type file iuserdal.cs       6, add ClassLibrary project, Named classfactory       I believe you've seen it, this is no different from the petshop example, and it's simpler, because the next is also learning three-tier architecture through PetShop. But some friends may be vague about the level of these projects and the relationship between them, one by one,:       1, user.aspx and user.aspx.cs        these two files (and the project to which the file belongs, as well as the following, are no longer repeated) are part of the presentation layer. User.aspx better understand, because it is the display page. User.aspx.cs Some people feel that they should not be counted, but to be drawn into the business logic layer. Without layering, it's okay to let User.aspx.cs handle business logic and even manipulate the database, but do the layering, then it should not be. In a hierarchical structure, User.aspx.cs should only deal with display-related content, and no other part should be involved.        Example: We implement a list to display the user's function, then the work of extracting information is done by the BLL, The UI (in this case, User.aspx.cs) calls the BLL to get userinfo, and it implements the display of the list by binding the code to the User.aspx data control. In this process, User.aspx.cs does not play a role in the UI, only to pass the data, and because most of the actual coding is so implemented, so that some people feel that User.aspx.cs should not be counted as UI, but should be integrated into the BLL responsible for logical processing. Continuing to look down, a new requirement was asked to add an icon to the front of each user, vividly showing the gender of the user, and a child icon under the age of 18. The realization of this demand, it is User.aspx.cs to do, in this case User.aspx.cs only to have a real purpose.        2, newbll.cs       Add the following method:       Public IList getusers (): Returns all user Information list        public UserInfo GetUser (int UserId): Returns the details of the specified user        public bool AddUser (UserInfo user): New user information        public bool Changeuser (UserInfo user): Update users information        public void removeuser (int UserId): Remove user information        This file belongs to the business logic layer and is designed to handle operations related to business logic. There may be a lot of people who think that the only use of this layer is to forward data from the presentation layer to the data layer. This is a lot, but it only shows that the project is relatively simple, or that the relationship between the project itself and the business is not tightly integrated.(such as the current more popular mis), so the business layer has nothing to do, only played a forwarding role. But this does not mean that the business layer is dispensable, as the project grows, or the business relationship is more, the business layer will reflect its role.        the most likely error here is that the data manipulation code is zoned into the business logic layer, and the database is used as the data access layer.        For example: some friends feel that the BLL layer has little meaning, just forward the data from the Dal to the UI without any processing. Take a look at this example        BLL layer        selectuser (UserInfo UserInfo) Gets the user's details based on the incoming username or email.        isexist (UserInfo UserInfo) determines whether the specified username or email is present.        Then the DAL also provides a method for common BLL calls        Selectuser (UserInfo UserInfo)        isexist (UserInfo UserInfo)        so the BLL really just plays a role.        But if this is done:       BLL. Isexist (Userinfo Userinfo)        {       uerinfo user = DAL. Selectuser (User);       return (userinfo.id! = null);      }      Then the DAL does not have to implement the Isexist () method, and the BLL has logic-processing code.        3, usermodel.cs       entity class, this thing, everyone may feel bad layering. including me before, it is understood that: Ui?àmodel?àbll?àmodel?àdal, so that the model in each layer has played a role of data transmission bridge. But here, we don't want to make things simple, but we want to be complicated. What is        model? It's nothing! It is optional in the three-tier architecture. It's actually the most basic thing in object-oriented programming: classes. A table is a class, a piece of news is also a class, int, string, Doublie, etc. is also a class, it is just a class.        in this way, the position of model in the three-tier architecture is the same as that of variables such as int,string, with no other purpose, only for storing data, except that it stores complex data. So if the objects in your project are very simple, it is possible to pass multiple parameters without model and make a three-tier architecture.        Why do you have model, and what are the benefits of it? Here's what you think about a problem, plug it in here.:       model can play a big role in the transfer of the parameters of each layer?        When passing parameters between layers, you can:       AddUser (Userid,username, UserPassword, ...,        can also:       AddUser (userInfo)        the two methods of the good. At a glance, the second is certainly better.        when to pass parameters between layers using the common variable type (int,string,guid,double), what is the model pass?Here are a few ways to:       selectuser (int UserId)        Selectuserbyname (string username)        selectuserbyname (string username,string password)        selectuserbyemail (string email)        Selectuserbyemail (string email,string password)        can be summarized as:        Selectuser (userId)        selectuser (user)        Here, the model object with the user contains four combinations of the three parameters of the Username,password,email. The UserID can also be incorporated into the user, but the other BLL in the project implements an interface with an ID parameter, so this is also preserved here.        passed in the UserInfo, then how to deal with it, this need to follow the sequence of order, there is a specific code to decide.        here to process        first to see if you have both username and password, Then see if you have email and password at the same time, then see if there is a username, and then see if there is an email. Processed in turn.        So, if you add a new content later, the membership card (number), you do not have to change the interface, as long as the Dal in the code to increase the number of support for the line, Then the front desk to increase membership card a content of the performance and processing.        4, userdal.cs       public IList selectusers (): Returns all user Information list        Public UserInfo selectuser (int UserId): Returns the trust information for the specified user        public bool Insertuser (UserInfo user ): Add user information        public bool UpdateUser (UserInfo user): Update user information        public void DeleteUser (int UserId): Remove user information        The most confusing is the data access layer, which part of the data access layer? Some think that the database is the data access layer, which is not clear about the definition, the DAL is the data access layer rather than the data storage layer, so the database cannot be this layer. Also some of the sqlhelper (or its similar components) as a data access layer, it is a dispensable thing, the role of sqlhelper is to reduce repetitive coding, improve coding efficiency, so if I am accustomed to care about efficiency or use a non-database data source, You can discard SqlHelper, a part that can be disposed of at will, and how can it be a layer in a three-tier architecture.        can be defined as: code related to data source operations should be placed in the data access layer, belonging to the data access layer        5, Iuserdal        data Access layer interface, this is another dispensable thing, because petshop with it and ClassFactory class factory, so some projects regardless of need to support multiple data sources, All these two things did come in, and some did not even build classfactory and only built Idal, and then "iuserdal iuserdal = new Userdal ();", I do not know what the meaning. This is completely a tiger painting is not an anti-class dog.        Many people inHere is a misunderstanding, that is to think that there is such a relationship: Bll?àidal?àdal, think that Idal play a bridge between the BLL and the DAL, the BLL is through the idal to invoke the DAL. But the reality is that even if you encode it this way: "Iuserdal iuserdal = Classfacotry.createuserdal ();", the Userdal instance is actually executed when "iuserdal.selectusers ()" is executed. Instead of Iuserdal instances, the position of Idal in layer three is the relationship to the DAL level.        through the above introduction, basically the hierarchy structure of the three-tier architecture is illustrated. In fact, I have a judge whether the three-tier architecture is a standard method, that is, the three layer of any layer completely replaced, will not affect the other two layers, the structure is basically in line with the three layer standard (although the implementation is more difficult ^_^). For example, if you change an item from B/S to C/s (or vice versa), the BLL and Dal do not have to be changed except the UI, or you can change SQL Server to Oracle, just replace Sqlserverdal to Oracledal, no additional operations, and so on. Originally wanted to add some specific code in the text, but the feeling is not very necessary, if you feel the need, I would like to add.        Summary: Do not think that a layer is useless to you, or that it is very simple to implement, that it is not necessary, or to abandon it, or to use it. As long as the layering, regardless of the layers, each layer should have a clear purpose and functional implementation, and not by the actual process, resulting in the same class of files in different layers of the situation occurs. Also do not appear on the same layer to achieve different functions of the situation occurs.

ASP. NET three-tier architecture (DAL,BLL,UI)

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.