Using a three-tier architecture in LINQ (2) Creating an lts layer and querying data in the presentation layer

Source: Internet
Author: User
To enable my first friend who came into contact with LINQ to discuss it together, I provided a step-by-step approach and started with the most basic operations.
I will not talk much about the advantages of the three-tier architecture. When we get started, we will start our work directly.

Step by step

1. Open vs2008, create a new site, and directly copy the provided northwind database to app_data.

Right-click the root directory, add new item, select "LINQ to SQL classes", select "C #", and rename it "northwind. dbml.

2. Click the Server Explorer tab, open the northwind. MDF node, and drag the table categories and table products in tables into northwind. dbml.


3. Click the blank area in the window and switch to the Properties tab. Note that the datacontext name is northwinddatacontext. Then we will use this object to create an instance. Click on the products table and notice that its name is products and the source attribute is DBO. Products.

4. Note that the northwind. dbml file in Solution Explorer has a CS file. Let's take a look at its content and understand how it works, but we will not discuss it in depth.


5. We will not add a logical access layer that contains the LINQ syntax, but directly use the table data through the presentation layer and some LINQ statements. So we create a new web form, add a gridview, double-click the blank space to enter the pageload event, let's add the following code:

Northwinddatacontext DB = new northwinddatacontext ();
VaR Product = from P in db. Products
Select P;
Gridview1.datasource = product;
Gridview1.databind ();

 

6. briefly explain the Code:

1) We have created a datacontext instance DB

2) We use a new var keyword to tell the compiler to determine the value type. (However, since the compiler has to deduce the type, it must be assigned a value when declared, and cannot be a null value. Note: this can only be used for local variables. It is not allowed for fields)

3) We chose products in the database. In fact, this is the result of the objectization of the table products (I will not talk about the LINQ syntax here)

4) In fact, the final product type should be iqueryable, which is located in system. LINQ. This type can be used directly as datasource.

5) use product as the data source of gridview1. then bind it.

7. Run the page to view all records in the product table.


 

How does it work?

We do very little work. We just created a LINQ to SQL class, and dragged tables in the database into it (you can also add views and stored procedures ). After that, vs2008 automatically generates a series of code, which we will not introduce in detail, but we know that the LTS layer converts the data into an object, and we can operate on the data as an operation object.

However, my practice does not write the methods for operating these objects in the LTS layer (in fact, this can also be done). Therefore, in this chapter, we use these methods in the presentation layer, it is called the LINQ syntax and looks like the SQL query syntax. During compilation, LINQ translates them into SQL syntax and then operates the database based on the corresponding object.

Summary:

Some people may think that I have not traditionally put the operation method at the bottom layer, which is not in line with the three-tier architecture. However, I personally think that even completing these operations on the presentation layer does not violate the "statements that do not have database operations in the presentation layer ". In fact, we didn't operate on data entities, but operate on objects. Then, LINQ will help us apply these operations to Data.

However, even so, I think it is inappropriate to implement the object Operation Method in the presentation layer, but this chapter focuses on the establishment of lts, In order to quickly get our results, this method is adopted. In the next chapter, we will learn how to use the logical access layer to write these methods. In the presentation layer, we can directly call these methods.

You can refer to some articles in Li yongjing's blog for more details on the use of LINQ statements. I will focus on the design of layers here, so I will not discuss it in detail. It seems like SQL, but it is more object-oriented.

Here are a few different queries. You can replace the LINQ statement in the previous example.

View disabled records

VaR Product = from P in db. Products
Where p. discontinued = true
Select P;

View the Supplier name produce record

VaR Product = from P in db. Products
Where p. Category. categoryname = "produce"
Select P;

Since the relationship between tables is also objectized by LINQ, we can directly use P. Category. xxxx to represent the Field Values in the category table.

Well, the content in Chapter 1 should be easier to digest.

Happy programming!

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.