My view of the three-tier architecture--different from the three-tier architecture you've seen.

Source: Internet
Author: User
I started the programming work from 02, began to contact some simple website, the second half wrote a small self-service construction program (ASP and asp.net), relatively simple not use. 03 began to make official website (ASP).           In the second half of 03 began to revision Miyoshi online (www.sanhaoonline.com asp.net), the end of 03, the beginning of 04 to know the three-storey structure of the related Dongdong, a very early start is not understand, so it is not ignored, still follow my own ideas to write the site. Later I began to look at the three level of the stuff, but unfortunately I still do not understand PetShop and Duwish code (perhaps I did not spend too much time to see it). The overall feeling of three layers is two words--trouble, four words--waste code. Feel your own stuff is very concise.           Because I have always been a person to write the site, no one to help me share the other layers of writing; and I am lazy, one person write three layers of code-too tired. As time went on, I gradually validated my own ideas. Can not be said to be absolutely good, but also to achieve the convenience, simplicity, scalability, good degree of stability. Always wanted to write something out, but for a variety of reasons obstructed, has not fulfilled the desire. (previously in CSDN also sent a number of posts, are nothing.)           In the last two days, three floors have been mentioned again, it seems I should write something. Before continuing, narrow down the scope (the programmer's ideas are broad and easily associated with other aspects): at present, I want to discuss only asp.net, and just a site, and then a little smaller is that kind of very simple website. (Other circumstances will not be taken into consideration, of course, it would be explained later.) Please don't be impatient. Of course, you should not think that I have not considered the complicated situation at all. Below is a brief talk about my thinking. In fact, it is very simple, is two words-- General。 1, the data access layer is universal. (The concept of the data access layer in the three layer is different, please don't be serious.) 2, the entity class is also generic. 3, commonly used functions (such as paging, linkage drop-down list, etc.) are written in the control, nature is universal. Let's talk about the data access layer first. My impression: The three-tier architecture of the data access layer is not universal, in fact, I now do not fully understand the three-tier architecture of the data access layer exactly what to write, the feeling is repeated written SqlConnection cn = New SqlConnection (...);
SqlCommand cm = new SqlCommand
SqlDataAdapter da = new SqlDataAdapter (cm);
Da.   Fill (...); The code.           I don't know if my understanding is correct.         Perhaps you will say: You do not know sqlhelp. I know sqlhelp, but it's too late to know. Before I knew him, I was already using my own "Data Access Layer" (which was not as powerful as it is now, so I added quotes). And I feel like I'm writing a lot more useful than sqlhelp. Of course, I also refer to some sqlhelp and other similar programs, in order to absorb the essence, to continuously improve their data access layer. The capabilities of my data access layer:In short, it is a further encapsulation of ado.net-simplifying the functionality of the package.             Ado.net is generic, so my data access layer is naturally generic. Think about what we need. Execute SQL statements (such as Add, modify, delete), and return recordsets (stored in objects such as datasets).   My data access layer is also built around these two features. Using ado.net directly, you write several lines of code to get a dataset.   (like the example I wrote above).   The use of my data access layer is not so much code, basically a line can be.   Parameters to pass in: SQL statement, name of stored procedure (parameters of stored procedure). According to the actual situation to determine what the specific parameters, as well as the use of those functions. (These are the things that call the data Access layer.)   return type: Returns void, DataSet, DataTable, DataRow, string[], string, and recently adds a function to return an array of structures, which is an "entity class." Simply put, my data access layer is these interfaces. Here are examples of how my data access layer is used:For example, I would like to call the last five messages added on the homepage of the website and then bind to the Repeater control. I can write jyk like this. Dataaccesslayer dal = new Jyk.           Dataaccesslayer (); Instantiating the data access layer
Rpt_news.datasource = dal.         Runsqldatatable ("Select Top 5 field from table name where conditional sort, etc."); Get a Recordset
Rpt_news.databind ();

Dal. Dispose ();           Releases resources. Just a few lines to achieve the function, in addition to the front desk repeater need to write point code, the rest of the place no longer write any code.   I feel that my method is quite brief, how do you feel. (Of course, the home page will not only show information on the finished, the other is similar to the wording.) You can also use the DataSet ds = dal. Runsqldataset (SQL statement)
Or
DataSet ds = dal. Runstoredataset (stored procedure name) returns more than one recordset. Then use the DS. Table[0], ds. TABLE[1] ... To bind the control. ) data Access layer first of all, let 's talk about entity classes           At first I didn't use entity classes, because it would be too much trouble to have a table corresponding to an entity class. Instead of using the DataTable directly, it is also easy to use with the data access layer.           later found <%# databinder.eval (Container, "DataItem.txt")%> The binding method is really inefficient, discard it, and change it to <%# ((DataRowView) container.dataitem) ["Url"]%>. But it always feels bad. It suddenly occurred to me that there used to be an expert. A method of customizing a data source-bound control. Memory has been very vague, vaguely feel is the use of the structure of the stuff. After a period of exploration and experimentation, it was determined that one of its "entity classes"--more precisely, the structure (struct) array.  //Sussu         Let's take a look at the "elements" in the Web page first. Think about the example above that shows the information, what we need to get in the page (UI layer). Links to Web sites, information headlines, published time, popularity, information pictures of the Web site, information on a brief introduction. (Is there anything else?)           According to my experience in writing websites, these are enough. That is, the structure (struct) that defines these attributes is sufficient for general pages (first and list pages) to be used. In practice, of course, I added an id attribute. Sussu end           Common entity classes, that is, whether it is information or file downloads or whatever, save the recordset with an array of structures with these attributes. Then add a function to the data access layer to return the array of structures (like a function that returns a DataTable). The foreground call is also handy, not only to bind to the control, but also to display the data directly using a For loop (as if it were back to the age of ASP J). Because for more flexible, is the control can not match. And can be very easy to deal with the art of a bad cycle of the page.   Binding control <% #  (BaseTitle) Container.DataItem). URL%>
<% # ((basetitle) Container.DataItem). Title%>
<% # ((basetitle) Container.DataItem). ImagePath%>
<% # ((basetitle) Container.DataItem). Hits%> ... for. (The background defines myData and populates the data with my data access layer) <%= Mydata[i]. URL%>
<%= Mydata[i]. Title%>
<%= Mydata[i]. ImagePath%>
<%= Mydata[i]. Hits%> The same writing, do not consider the specific field name, where the use of copying to where it can be, is not very convenient. here is the last part. After saying the first page, the following is the list page.           The main is paging and query.     It may not be easy for you to page out (including the query) (and it may be very easy for you, of course), but it's quite easy for me-because I have my own paging control. private void Page_Load (object sender, System.EventArgs e)
... {
Place user code here to initialize page
Response.Cache.SetNoStore ();

Page1.pubshowdataobject = DL; Give a control that displays data

if (! Page.IsPostBack)
... {
Setpageinfo ();
}
}

Set paging controls #region set paging controls
private void Setpageinfo ()
... {
Page1.sqltablenames = "TableName"; Table name
Page1.sqlcolumns = "*"; Fields that are displayed
Page1.sqlordercolumn = "ID"; Sort fields
Page1.sqlordercolumnkind = "int"; Type of sort field
Page1.isorderdesc = true; Reverse Display

Page1.sqlpagesize = 10; Number of records on a page
Page1.sqlquery = ""; Query criteria

Page1.createquery (); To generate a query statement
Page1.bindfirstpage (); Bind first Page

}

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.