Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the three-tier architecture of WEB development based on LINQ to SQL

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize the three-tier architecture of WEB development based on LINQ to SQL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to implement the three-tier architecture of WEB development based on LINQ to SQL".

Programmers are no longer limited to complex SQL scripts, but can build their own object-oriented software systems in an almost * * way, which is to completely objectify the data, encapsulate the SQL statements to the bottom, and be completed by framework. Programmers only need to program for database objects, in another sense, the data is also programmed.

This development mode of LINQ also changes the way of building system architecture. In previous systems, data access layer DAL wants to access data fields, business logic layer BLL needs to deal with data through data model layer Model, and LINQ and VS2008 bring us a new automatic way to generate data model layer, which is dbml (Database Mark Language). Database description language, is a xml format document, used to describe the database), with it we do not need to find those third-party code generation tools, we just need to drag the data table to the designer, as shown in the following figure, DONET has done everything for us.

After the drag-and-drop operation is completed, VS automatically generates a dbml file and related class files for the data model layer. In this way, we do not need to build the data model layer, and the architecture of the system is different. Here is a simple example to talk about the architecture model.

To complete this architecture, we first create a WEB APPLICATION project, select "ASP.NET WEB Application" in the new project window, give it a name, and determine.

Next, add another class library project to solution Explorer, named DAL, as shown below:

Then use the same method to add a class library project in solution Explorer, named BLL, so that our basic architecture is complete, and our solution Explorer should have the following structure.

At this point, we start with the DAL project, in the DAL project, add a LINQ TO SQL class named Northwind (for convenience, this project uses the Northwind sample database in SQL SERVER2005), double-click the newly created Northwind.dbml file, then open Server Explorer, establish a connection with the data, and drag the Employees table from the Northwind database to the visual designer of the Northwind.dbml file.

The initial Northwind.dbml file code is as follows:

# pragma warning disable 1591Compact / / this code is generated by the tool. / / Runtime version: 2.0.50727.3053 / changes to this file may cause incorrect behavior and will be lost if / / the code is regenerated. /-namespace DAL {using System.Data.Linq;using System.Data.Linq.Mapping;using System.Data;using System.Collections.Generic;using System.Reflection;using System.Linq;using System.Linq.Expressions Using System.ComponentModel;using System; [System.Data.Linq.Mapping.DatabaseAttribute (Name= "Northwind")] public partial class NorthwindDataContext: System.Data.Linq.DataContext {private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource (); # region Extensibility Method Definitionspartial void OnCreated (); partial void InsertEmployees (Employees instance); partial void UpdateEmployees (Employees instance); partial void DeleteEmployees (Employees instance); # endregionpublic NorthwindDataContext (): base (global::DAL.Properties.Settings.Default.NorthwindConnectionString, mappingSource) {OnCreated () } public NorthwindDataContext (string connection): base (connection, mappingSource) {OnCreated ();} public NorthwindDataContext (System.Data.IDbConnection connection): base (connection, mappingSource) {OnCreated ();} public NorthwindDataContext (string connection, System.Data.Linq.Mapping.MappingSource mappingSource): base (connection, mappingSource) {OnCreated ();} public NorthwindDataContext (System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource): base (connection, mappingSource) {OnCreated ();} public System.Data.Linq.Table

As you can see, this is essentially the previous data model layer, which objectifies the whole data. In order to facilitate the query between layers, we change the namespace of the changed class to Auto.DAL, and modify the * construction function code of the NorthwindDataContent class as follows. This modification is mainly to unify the location of the database connection string, because the dbml file will be accompanied by an app.config file after it is generated. It is used to store the connection string, and we want to put the connection string into web.config. (to access the web.config file, you need to add an application to System.Configuration for the DAL project)

Public NorthwindDataContext (): base (ConfigurationManager.ConnectionStrings ["NorthwindConnectionString"] .ConnectionString, mappingSource) {OnCreated ();}

After completing the previous operation, start to establish the class file of the data access layer. At this time, add a class file under the DAL project, named DALEmployees.cs, whose code is as follows:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Auto.DAL {public class DALEmployees {/ NorthwindDataContext db = new NorthwindDataContext (); / [object Object] City address / public IQueryable

This data access layer only establishes a method to obtain Employee data according to the city address, which executes a piece of LINQ internally and returns an IQuery result, which will not return the dataset until the program is run due to the late compilation of LINQ.

Next, set up the business logic layer, and in the BLL project, add a class file named BLLEmployees.cs, whose code is as follows:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using Auto.DAL;namespace Auto.BLL {public class BLLEmployees {/ [object Object] city name / public IQueryable

This code completes the definition of the business logic layer and establishes a method GetList with the same name as the data access layer, which is used to pass the city name parameters. *. In the WEB Application project, add an application to the BLL layer and add a GridView control to the Default.aspx page to display the data. The code of Default.aspx.cs is as follows:

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Auto.BLL;namespace WebApplication2 {public partial class _ Default: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {GetList ("London");}} / [object Object] City name private void GetList (string strCity) {/ / method BLLEmployees bl = new BLLEmployees () to execute the business logic layer / / bind to GridView1 control GridView1.DataSource = bl.GetList (strCity); GridView1.DataBind ();}

After the completion, execute the program, you can get the relevant data, the structure of the whole program is as follows:

At this point, I believe you have a deeper understanding of "how to realize the three-tier architecture of LINQ to SQL-based WEB development". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report