This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17061|Reply: 0

[ASP.NET] Based on. .LINQ to SQL three-layer architecture development

[Copy link]
Posted on 5/8/2015 3:47:22 PM | | | |

Nonsense written in front

Based on. The three-tier architecture of .NET can be divided into the following layers: Data Linq layer, data access layer, business logic layer, and presentation layer. The arrow symbols in Figure 1 below illustrate the interaction between the layers: the data access layer references the Data LINQ layer; The business logic layer references the data access layer and the entity classes in the Data LINQ layer. The presentation layer refers to the business logic layer as well as the entity classes in the Data LINQ layer.

The Data LINQ layer mainly includes entity classes and Data Context classes. Typically, an entity class is for one table in the database. We can create instances of entity classes that are transferred between different layers as data objects.

        Figure 1 LINQ to SQL three-tier architecture

The following is an example of the login window when creating a project to introduce the three-tier architecture development of LINQ to SQL

Preparation

1. First, you need a database and a data table to store the username and password of the logged-in person. Here, a SQL Server 2005 database is used. The database table structure is shown in Figure 2.

Figure 2 Database User table

2. Open VS and create a new project ---> other project types---> Visual Studio solutions---> blank solutions. As shown in Figure 3. (Visual Studio 2008 is used here)


Figure 3 Create a blank solution

3. Add a class library. Right-click on the newly created solution in Solution Explorer ---> add ---> new project ---> class library. As shown in Figure 4. The same method requires adding three libraries, BLL, DAL, and DataLinq, and a Windows Forms application (if it is a web application development, select ASP.NET Web application) and name it UI.


Figure 4 Add a library project

The structure after the addition is shown in Figure 5. At the same time, right-click on the UI and set the UI as the launch item.


Figure 5 Project structure diagram

Add a quote

In Solution Explorer, add references to each tier. BLL layer Select References --->right-click ---> add references. As shown in Figure 6. Add a reference to System.Data.Linq. At the same time, according to the relationship between the layers shown in Figure 1 above, add project references to DAL and DataLinq. In the same way, the DAL layer adds project references to System.Data.Linq and DataLinq, the DataLinq layer adds references to System.Data.Linq, and the UI layer adds references to System.Data.Linq and project references to BLL and DataLinq.


Figure 6 Add project references

So far, a three-tier architecture has been established. The following is to add their corresponding classes to each layer.

1. First, the DataLinq layer. Right-click DataLinq---> add ---> new project---> select the LINQ to SQL class, as shown in Figure 7.


Figure 7 Adding a LINQ to SQL class

2. After the addition is completed, a blank designer (.dbml file) will be automatically created with a link to Server Explorer, as well as the associated dbml.layout file (XML file) and designer.cs file. Open Server Explorer and establish a link to the database.


Figure 8 Open Server Explorer

3. Connect to the database. Select the tool ---> add database. As shown in Figure 9. Select the newly created database and determine the connection database. At this point, the database will appear in Server Explorer.


4. Select the data table User you want to use, drag and drop it onto the designer, as shown in Figure 10 below. Save the file. Now, DataContext classes and entity classes with related properties, methods, and methods have been automatically generated.


Figure 10 User class

Add layers of code

DAL layer

Create a new 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 new 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 code for the login interface is complete. You can run it to see the effect.






Previous:If you already have an open DataReader associated with this Command, you must first close it.
Next:LINQ gets stored procedure return value problem my stored procedure
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com