My Opinion on the three-tier architecture is different from the general three-tier architecture.

Source: Internet
Author: User
Http://community.csdn.net/Expert/TopicView3.asp? Id = 4949724

The three layers seem to be quite popular recently, but they are different from the previous ones. Many people are confused and puzzled.

There seems to be a lot of questions. Based on my experience, I wrote something that may be helpful to you.

(Why can't I post a blog? Are you upgrading again ?)

I started programming in and started to access some simple websites. In the second half of the year, I wrote a small self-help website creation program (asp and asp.net), which was relatively simple and useless. The website (asp) was officially started in ). In the second half of, sanhaoonline.com asp.net (www.sanhaoonline.com) was revised. At the end of 03 and early 04, it was only known about the three-tier architecture. At the beginning, it was hard to understand, I still write my website based on my own ideas.

Later, I started to look at the three layers of stuff, but unfortunately so far I still haven't understood the petshop and duwish code (maybe I didn't spend much time to see it ). The three layers give me the general feeling that there are two words -- trouble; four words -- a waste of code. I feel that my stuff is very concise. Because I have been writing websites by myself, and no one has helped me share the writing of other layers. I am very lazy, and I am too tired to write three layers of code by myself.

Over time, I gradually verified my own ideas. It cannot be said that it is absolutely good, but it can also be convenient, concise, scalable, and stable. I always wanted to write something out, but for various reasons, I never realized my wish. (In the past, I posted some posts in csdn, but they are all gone .)

In the last two days, the three layers have been raised again. It seems that I should write something too.

Before proceeding, narrow down the scope (programmers have a wide range of ideas and can easily think of other aspects ):

At present, I only want to discuss asp.net, and it's just a website. to narrow it down, it's a very simple website. (I will not consider other cases for the time being. Of course, I will explain it later! Please do not worry. Of course, you do not think that I have not considered the complicated situation at all .)

The following is a brief introduction of my ideas. In fact, it is very simple, that is, two words-General!

1. The data access layer is common. (The concept of the data access layer is different from that of the data access layer on the layer 3. Please do not be more accurate .)
2. Entity classes are also common.
3. Common functions (such as paging and linked drop-down lists) are written as controls, which are also common.

Let's talk about the data access layer first.

My impression: the data access layer in the three-tier architecture is not universal. In fact, I have no idea what the data access layer should write in the three-tier architecture, I feel like I'm reading it again.

SqlConnection cn = new SqlConnection (...);
SqlCommand cm = new SqlCommand
SqlDataAdapter da = new SqlDataAdapter (cm );
Da. Fill (...);

These codes. I don't know if my understanding is correct.

Maybe you would say: Do you know SqlHelp?
I know SqlHelp, but it's too late to know. Before I knew him, I was using my own "data access layer" (at that time it was not as powerful as it was now, so I added quotation marks ). In addition, I feel that I write more easily than SqlHelp. Of course, I also referred to some SqlHelp and other similar programs to learn the essence and constantly improve my data access layer.

Features of my data access layer:

Simply put, it is further encapsulation of ado.net-encapsulation of simplified functions. Ado.net is common, so my data access layer is also common.

Think about what we need? Execute SQL statements (such as adding, modifying, and deleting) and return records (stored in DataSet and other objects ). My data access layer is centered around these two features.

If you directly use ado.net, you need to write several lines of code to get a DataSet. (Like the example I wrote above ).

Using my data access layer, there will be less code, and a line is enough.

Parameters to be passed in: SQL statement and stored procedure name (stored procedure parameters ).

Determine the specific input parameters and use the functions based on the actual situation. (These are the places where the data access layer is called .)

Return type: Return void, DataSet, able, DataRow, string [], and string. Recently, the return structure array function is added, which is also called "entity class ".

In short, my data access layer is these interfaces.

The following example shows how to use the data access layer:

For example, I want to call the last five pieces of information on the homepage of the website and then bind it to the Repeater control.

I can write it like this

JYK. DataAccessLayer dal = new JYK. DataAccessLayer (); // instantiate the data access layer
Rpt_News.DataSource = dal. RunSqlDataTable ("select top 5 fields from table name where conditional sorting, etc."); // obtain the record set
Rpt_News.DataBind ();

Dal. Dispose (); // release resources.

In just a few lines, the function is implemented. In addition to the front-end Repeater, you need to write some code. In other places, you do not need to write any code. I think my method is quite brief. How do you feel?

(Of course, the home page won't only show the information to complete, and others are similar. You can also use
DataSet ds = dal. RunSqlDataSet (SQL statement)
Or
DataSet ds = dal. RunStoreDataSet (stored procedure name)

Returns multiple record sets. Then use ds. Table [0], ds. Table [1]… To bind controls .)

Let's talk about the object class at the data access layer.

At first, I didn't need entity classes, because I felt that a table was needed to correspond to an object class. If so, it would be too troublesome. Instead of using DataTable directly, it is very easy to use with the data access layer.

Later we found that <% # DataBinder. the binding method of Eval (Container, "DataItem.txt") %> is too inefficient. It is discarded and changed to <% # (DataRowView) Container. dataItem) ["Url"] %>. But I always feel uncomfortable. It suddenly occurred to me that someone previously mentioned how to bind controls to a custom data source. The memory is already vague, and it seems like the structure is used. After a period of exploration and experiment, I have determined a kind of "entity class"-more accurately, it is a structure (struct) array.

// Flashback
Let's take a look at the "elements" in the webpage ". Think about the example of information shown above. What do we need to obtain on the page (UI Layer? Brief Introduction of URL, news title, posting time, popularity, news image URL, and information. (Is there anything else ?)

Based on my website writing experience, this is enough. That is to say, defining these attributes in the structure (struct) is enough for general pages (home pages and list pages) to use. Of course, I actually added an ID attribute.
// End the plug-in

Generic object classes, that is, whether it is information, file download, or other, use a structure array with these properties to save the record set. Add a function in the data access layer to return the structure array (just like the function that returns the DataTable ). Front-end calls are also very convenient, not only can be bound to the control, but also can be directly used for loop to display data (as if back to the asp era J ). Because for is more flexible, it is incomparable to controls! In addition, it can easily cope with the bad cycle pages provided by the artist.

Binding controls
<% # (BaseTitle) Container. DataItem). URL %>
<% # (BaseTitle) Container. DataItem). Title %>
<% # (BaseTitle) Container. DataItem). ImagePath %>
<% # (BaseTitle) Container. DataItem). Hits %>
......

For. (Define myData in the background and use my data access layer to fill in data)
<% = MyData [I]. URL %>
<% = MyData [I] .. Title %>
<% = MyData [I] .. ImagePath %>
<% = MyData [I] .. Hits %>

For the same writing method, you do not need to consider the specific field name. It is convenient to copy the field where it is used.

The following is the last part.

After talking about the home page, the following is the list page. It mainly refers to paging and query.

Maybe paging is not too easy for you (including the query function) (of course it may be very easy for you ), but it is quite easy for me-because I have my own paging controls.

Private void Page_Load (object sender, System. EventArgs e)
...{
// Place user code here to initialize the page
Response. Cache. SetNoStore ();

Page1.PubShowDataObject = DL; // Control for displaying data

If (! Page. IsPostBack)
...{
SetPageInfo ();
}
}

Set paging control # region set paging Control
Private void SetPageInfo ()
...{
Page1.SqlTableNames = "TableName"; // table name
Page1.SqlColumns = "*"; // displayed Field
Page1.SqlOrderColumn = "ID"; // sorting Field
Page1.SqlOrderColumnKind = "int"; // type of the sorting Field
Page1.IsOrderDesc = true; // display in reverse order

Page1.SqlPageSize = 10; // number of records on one page
Page1.SqlQuery = ""; // query Condition

Page1.CreateQuery (); // generate a query statement
Page1.BindFirstPage (); // bind the first page

}
# Endregion

I only need to write the above lines of code to implement the data display function by PAGE (extract the data part and display the data must be processed separately ). Query is also very simple. Combine the SQL statement (after where), assign the value to the Page1.SqlQuery attribute, and then execute
Page1.CreateQuery (); // generate a query statement
Page1.BindFirstPage (); // bind the first page

You can. Click the next page and other processing events are included in the control. You do not need to add the event yourself unless otherwise specified.

These are the benefits of controls, that is, general. It is easy to use in any project (including background management!

The following is a summary:

UI Layer: aspx page

The logic layer is divided into two situations:
The function appears only once in the project and is directly written on the aspx. cs page;
Functions that appear multiple times in the project are written in the. cs file. (For example, login, user login information verification, etc)
A large number of custom controls are used to simplify encoding.

Data access layer: Call dll files without repeated code writing.

Of course, this seems to have become a layer of code, because the code is written in aspx and aspx. cs.

I am using a layered approach, not a layered form!

I don't know if I have any instructions.

My presentation skills are poor. Please forgive me a lot.

Write this information first, and then write it slowly.

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.