Establish a. Net-based architecture for developing a three-tier architecture of LINQ to SQL

Source: Internet
Author: User

Nonsense written in front

The. Net-based three-tier architecture of LINQ to SQL can be divided into the following layers: Data LINQ layer, data access layer, business logic layer, and presentation layer. The arrow symbol in Section 1 illustrates the interaction between layers: the data access layer references the data LINQ layer; the business logic layer references the data access layer and the entity class in the Data LINQ layer; the presentation layer references the entity classes in the business logic layer and data LINQ layer.

The data LINQ layer mainly includes object classes and data context classes. Generally, an object class is used for a table in a database. We can create an entity-class instance to transmit data between different layers as data objects.

 

Figure 1 three-tier architecture of LINQ to SQL

The following uses the logon window when a project is created as an example to describe the development of the three-tier architecture of LINQ to SQL.

Preparations

1. First, a database and a data table are required to store the username and password of the logon user. Here, the SQL Server 2005 database is used. The database table structure 2 is shown in.

Figure 2 Database User table

2. Open Vs and create a project ---> other project type ---> Visual Studio solution ---> blank solution. 3. (Visual Studio 2008 is used here)

Figure 3 new blank solution

3. Add a class library. In Solution Explorer, right-click solution> Add> Create Project> class library. 4. In the same way, you need to add Bll, Dal, datalinq three class libraries and a Windows form application (if it is web application development, select ASP. NET web application) named UI.

Figure 4 Add a class library project

The added structure is shown in structure 5. At the same time, right-click the UI and set the UI as the startup Item.

Figure 5 Project Structure

Add reference

In Solution Explorer, add references for each layer. Select reference for BLL layer ---> right-click ---> Add reference. 6. Add a reference to system. Data. LINQ. Add project references to Dal and datalinq based on the relationship between the layers shown in. In the same way, the Dal layer is added to the system. data. project references of LINQ and datalinq; added to system at datalinq layer. data. the reference of LINQ; the UI Layer is added to the system. data. references of LINQ and projects of BLL and datalinq.

Figure 6 Add a project reference

So far, the three-tier architecture has been established. The following describes how to add corresponding classes to each layer.

1. The first is the datalinq layer. Right-click datalinq ---> Add ---> Create Project ---> select the LINQ to SQL class, as shown in figure 7.

Figure 7 Add a LINQ to SQL class

2. A blank designer (. dbml file), with a link to the server resource manager, and also create the relevant dbml. layout file (XML file) and designer. CS file. Open the server resource manager and create a connection to the database.

Figure 8 open the server Resource Manager

3. Connect to the database. Select tool ---> Add database. 9. Select the database you just created to connect to the database. In this case, the database will appear in the server resource manager.

4. Select the user data table to be used and drag it to the designer, for example, 10. Save the file. Now, the datacontext class and object class containing related attributes and methods are automatically generated.

Figure 10 user class

Add layers of code

Dal Layer

Create a userdal class and add the following code:

// Userdal. CS

Using datalinq;
Using system. Data. LINQ;

Namespace dal
{
Public class userdal
{
Private datalinq. dblinqdatacontext objdatacontext = new datalinq. dblinqdatacontext ();

Public user selectrecordbyid (string userid)
{
Try
{
Return (from u in objdatacontext. User where u. ID = userid select U). Single ();
}
Catch (exception ex)
{
Throw ex;
}
}
}
}

Bll Layer

Create a userbll class and add the following code:

// Userbll. CS

Using system. Data. LINQ;
Using datalinq;

Namespace BLL
{
Public class userbll
{
Private Dal. userdal objuserdal = New DAL. userdal ();

Public user selectrecordbyid (string userid)
{
Return objuserdal. selectrecordbyid (userid );
}
}
}

UI-Layer Code

// Loginform. CS

Private BLL. userbll objuserbll = new BLL. userbll ();

Private void btnsubmit_click (Object sender, eventargs E)
{
String id = txtid. Text. Trim ();
String PSD = txtpsd. Text. Trim ();

User localdatatable = objuserbll. selectrecordbyid (ID );

If (localdatatable! = NULL & localdatatable. PSD = PSD)
{
MessageBox. Show ("success ");
}
Else
{
MessageBox. Show ("false ");
}
}

At this point, the logon interface code has been completed. You can run it to check the effect.

Next article: General Code for database operations based on. Net-based LINQ to SQL three-tier architecture development

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.