自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于LINQ to SQL的WEB開發(fā)三層架構(gòu)

開發(fā) 后端
LINQ語言集成查詢是微軟用于抗衡競(jìng)爭(zhēng)對(duì)手,改進(jìn)現(xiàn)有開發(fā)模式下面向數(shù)據(jù)庫操作問題的一個(gè)種全新體系架構(gòu)。它的出現(xiàn),解決了面向?qū)ο箝_發(fā)中由數(shù)據(jù)庫操作引起的先天性的瓶頸,使得面向?qū)ο蠛蛿?shù)據(jù)庫完美結(jié)合。

程序員不再受限于復(fù)雜的SQL腳本,而可以一種近乎***的方式來搭建自己的面向?qū)ο笮蛙浖?系統(tǒng),這種方式就是將數(shù)據(jù)完全對(duì)象化,將SQL語句封裝到底層,由framework來完成,程序員 只需面向數(shù)據(jù)庫對(duì)象來編程,從另一種意義上來說,是把數(shù)據(jù)也程序化了。

LINQ的這種開發(fā)模式也改變了系統(tǒng)架構(gòu)的搭建方式,在以往的系統(tǒng)中,數(shù)據(jù)訪問層DAL 要訪問數(shù)據(jù)字段,業(yè)務(wù)邏輯層BLL要得到數(shù)據(jù)都需要通過數(shù)據(jù)模型層Model來處理,而LINQ和 VS2008為我們帶來了一種全新的自動(dòng)化方式生成數(shù)據(jù)模型層,這就是dbml(Database Mark Language。數(shù)據(jù)庫描述語言,是一種xml格式的文檔,用來描述數(shù)據(jù)庫),有了它我們就不需要 去找那些第三方的代碼生成工具,只需要把數(shù)據(jù)表拖拽到設(shè)計(jì)器中,如下圖所示,DONET便為我 們做好了一切。

完成拖拽操作后,VS會(huì)自動(dòng)生成一個(gè)數(shù)據(jù)模型層的dbml文件和相關(guān)的類文件。這樣我們省去 了數(shù)據(jù)模型層的搭建,系統(tǒng)的架構(gòu)也就有所不同,以下用一個(gè)例子簡(jiǎn)單的講一下該架構(gòu)模型。

為了完成這個(gè)架構(gòu),我們首先要?jiǎng)?chuàng)建一個(gè)WEB APPLICATION項(xiàng)目,在新建項(xiàng)目窗口選擇 “ASP.NET WEB應(yīng)用程序”,為它取一個(gè)名字,并確定。

接下來,在解決方案資源管理器中再添加一個(gè)類庫項(xiàng)目,取名為DAL,如下圖:

再使用同樣的方法在解決方案資源管理器中添加一個(gè)類庫項(xiàng)目,取名為BLL,這樣我們的基 礎(chǔ)架構(gòu)搭建完成,此時(shí)我們的解決方案資源管理器應(yīng)該是如下結(jié)構(gòu)。

此時(shí),我們先從DAL項(xiàng)目入手,在DAL項(xiàng)目中,添加一個(gè)LINQ TO SQL類,取名為Northwind( 為了方便起見,此項(xiàng)目使用SQL SERVER2005中的Northwind示例數(shù)據(jù)庫),雙擊新建立的 Northwind.dbml文件,然后打開“服務(wù)器資源管理器”,建立與數(shù)據(jù)的連接,并從Northwind數(shù) 據(jù)庫中,將Employees表拖拽到Northwind.dbml文件的可視化設(shè)計(jì)器中。

#p#

初始的Northwind.dbml文件代碼如下:

#pragma warning disable 1591
//-------------------------------------- ----------------------------------------
//
// 此代碼由 工具生成。
// 運(yùn)行時(shí)版本:2.0.50727.3053
//
// 對(duì)此文件的更改可能會(huì) 導(dǎo)致不正確的行為,并且如果
// 重新生成代碼,這些更改將會(huì)丟失。
//
//----------------------------------------------------------------- -------------

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 Definitions
partial void OnCreated();
partial void InsertEmployees(Employees instance);
partial void UpdateEmployees(Employees instance);
partial void DeleteEmployees(Employees instance);
#endregion

public 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 Employees
{
get
{
return this.GetTable();
}
}
}

[Table (Name="dbo.Employees")]
public partial class Employees : INotifyPropertyChanging, INotifyPropertyChanged
{

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs (String.Empty);

private int _EmployeeID;

private string _LastName;

private string _FirstName;

private string _Title;

private string _TitleOfCourtesy;

private System.Nullable _BirthDate;

private System.Nullable _HireDate;

private string _Address;

private string _City;

private string _Region;

private string _PostalCode;

private string _Country;

private string _HomePhone;

private string _Extension;

private System.Data.Linq.Binary _Photo;

private string _Notes;

private System.Nullable _ReportsTo;

private string _PhotoPath;

#region Extensibility Method Definitions
partial void OnLoaded ();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnEmployeeIDChanging(int value);
partial void OnEmployeeIDChanged();
partial void OnLastNameChanging(string value);
partial void OnLastNameChanged();
partial void OnFirstNameChanging(string value);
partial void OnFirstNameChanged();
partial void OnTitleChanging(string value);
partial void OnTitleChanged ();
partial void OnTitleOfCourtesyChanging(string value);
partial void OnTitleOfCourtesyChanged();
partial void OnBirthDateChanging (System.Nullable value);
partial void OnBirthDateChanged ();
partial void OnHireDateChanging(System.Nullable value);
partial void OnHireDateChanged();
partial void OnAddressChanging(string value);
partial void OnAddressChanged();
partial void OnCityChanging(string value);
partial void OnCityChanged();
partial void OnRegionChanging(string value);
partial void OnRegionChanged();
partial void OnPostalCodeChanging(string value);
partial void OnPostalCodeChanged();
partial void OnCountryChanging(string value);
partial void OnCountryChanged();
partial void OnHomePhoneChanging(string value);
partial void OnHomePhoneChanged();
partial void OnExtensionChanging(string value);
partial void OnExtensionChanged();
partial void OnPhotoChanging(System.Data.Linq.Binary value);
partial void OnPhotoChanged();
partial void OnNotesChanging (string value);
partial void OnNotesChanged();
partial void OnReportsToChanging(System.Nullable value);
partial void OnReportsToChanged();
partial void OnPhotoPathChanging(string value);
partial void OnPhotoPathChanged();
#endregion

public Employees()
{
OnCreated();
}

[Column(Storage="_EmployeeID", DbType="Int NOT NULL", IsPrimaryKey=true)]
public int EmployeeID
{
get
{
return this._EmployeeID;
}
set
{
if ((this._EmployeeID != value))
{
this.OnEmployeeIDChanging(value);
this.SendPropertyChanging();
this._EmployeeID = value;
this.SendPropertyChanged("EmployeeID");
this.OnEmployeeIDChanged();
}
}
}

[Column(Storage="_LastName", DbType="NVarChar(20) NOT NULL", CanBeNull=false)]
public string LastName
{
get
{
return this._LastName;
}
set
{
if ((this._LastName != value))
{
this.OnLastNameChanging(value);
this.SendPropertyChanging();
this._LastName = value;
this.SendPropertyChanged("LastName");
this.OnLastNameChanged();
}
}
}

[Column (Storage="_FirstName", DbType="NVarChar(10) NOT NULL", CanBeNull=false)]
public string FirstName
{
get
{
return this._FirstName;
}
set
{
if ((this._FirstName != value))
{
this.OnFirstNameChanging(value);
this.SendPropertyChanging();
this._FirstName = value;
this.SendPropertyChanged("FirstName");
this.OnFirstNameChanged();
}
}
}

[Column (Storage="_Title", DbType="NVarChar(30)")]
public string Title
{
get
{
return this._Title;
}
set
{
if ((this._Title != value))
{
this.OnTitleChanging(value);
this.SendPropertyChanging();
this._Title = value;
this.SendPropertyChanged("Title");
this.OnTitleChanged();
}
}
}

[Column(Storage="_TitleOfCourtesy", DbType="NVarChar(25)")]
public string TitleOfCourtesy
{
get
{
return this._TitleOfCourtesy;
}
set
{
if ((this._TitleOfCourtesy != value))
{
this.OnTitleOfCourtesyChanging(value);
this.SendPropertyChanging();
this._TitleOfCourtesy = value;
this.SendPropertyChanged("TitleOfCourtesy");
this.OnTitleOfCourtesyChanged ();
}
}
}

[Column(Storage="_BirthDate", DbType="DateTime")]
public System.Nullable BirthDate
{
get
{
return this._BirthDate;
}
set
{
if ((this._BirthDate != value))
{
this.OnBirthDateChanging(value);
this.SendPropertyChanging();
this._BirthDate = value;
this.SendPropertyChanged("BirthDate");
this.OnBirthDateChanged();
}
}
}

[Column(Storage="_HireDate", DbType="DateTime")]
public System.Nullable HireDate
{
get
{
return this._HireDate;
}
set
{
if ((this._HireDate != value))
{
this.OnHireDateChanging(value);
this.SendPropertyChanging();
this._HireDate = value;
this.SendPropertyChanged("HireDate");
this.OnHireDateChanged();
}
}
}

[Column (Storage="_Address", DbType="NVarChar(60)")]
public string Address
{
get
{
return this._Address;
}
set
{
if ((this._Address != value))
{
this.OnAddressChanging(value);
this.SendPropertyChanging();
this._Address = value;
this.SendPropertyChanged("Address");
this.OnAddressChanged();
}
}
}

[Column(Storage="_City", DbType="NVarChar(15)")]
public string City
{
get
{
return this._City;
}
set
{
if ((this._City != value))
{
this.OnCityChanging(value);
this.SendPropertyChanging();
this._City = value;
this.SendPropertyChanged("City");
this.OnCityChanged();
}
}
}

[Column(Storage="_Region", DbType="NVarChar(15)")]
public string Region
{
get
{
return this._Region;
}
set
{
if ((this._Region != value))
{
this.OnRegionChanging(value);
this.SendPropertyChanging();
this._Region = value;
this.SendPropertyChanged("Region");
this.OnRegionChanged();
}
}
}

[Column(Storage="_PostalCode", DbType="NVarChar(10)")]
public string PostalCode
{
get
{
return this._PostalCode;
}
set
{
if ((this._PostalCode != value))
{
this.OnPostalCodeChanging(value);
this.SendPropertyChanging();
this._PostalCode = value;
this.SendPropertyChanged("PostalCode");
this.OnPostalCodeChanged();
}
}
}

[Column (Storage="_Country", DbType="NVarChar(15)")]
public string Country
{
get
{
return this._Country;
}
set
{
if ((this._Country != value))
{
this.OnCountryChanging(value);
this.SendPropertyChanging();
this._Country = value;
this.SendPropertyChanged("Country");
this.OnCountryChanged();
}
}
}

[Column(Storage="_HomePhone", DbType="NVarChar(24)")]
public string HomePhone
{
get
{
return this._HomePhone;
}
set
{
if ((this._HomePhone != value))
{
this.OnHomePhoneChanging(value);
this.SendPropertyChanging();
this._HomePhone = value;
this.SendPropertyChanged("HomePhone");
this.OnHomePhoneChanged();
}
}
}

[Column (Storage="_Extension", DbType="NVarChar(4)")]
public string Extension
{
get
{
return this._Extension;
}
set
{
if ((this._Extension != value))
{
this.OnExtensionChanging(value);
this.SendPropertyChanging();
this._Extension = value;
this.SendPropertyChanged("Extension");
this.OnExtensionChanged();
}
}
}

[Column(Storage="_Photo", DbType="Image", UpdateCheck=UpdateCheck.Never)]
public System.Data.Linq.Binary Photo
{
get
{
return this._Photo;
}
set
{
if ((this._Photo != value))
{
this.OnPhotoChanging(value);
this.SendPropertyChanging();
this._Photo = value;
this.SendPropertyChanged("Photo");
this.OnPhotoChanged();
}
}
}

[Column(Storage="_Notes", DbType="NText", UpdateCheck=UpdateCheck.Never)]
public string Notes
{
get
{
return this._Notes;
}
set
{
if ((this._Notes != value))
{
this.OnNotesChanging(value);
this.SendPropertyChanging();
this._Notes = value;
this.SendPropertyChanged("Notes");
this.OnNotesChanged();
}
}
}

[Column (Storage="_ReportsTo", DbType="Int")]
public System.Nullable ReportsTo
{
get
{
return this._ReportsTo;
}
set
{
if ((this._ReportsTo != value))
{
this.OnReportsToChanging(value);
this.SendPropertyChanging();
this._ReportsTo = value;
this.SendPropertyChanged("ReportsTo");
this.OnReportsToChanged();
}
}
}

[Column(Storage="_PhotoPath", DbType="NVarChar(255)")]
public string PhotoPath
{
get
{
return this._PhotoPath;
}
set
{
if ((this._PhotoPath != value))
{
this.OnPhotoPathChanging(value);
this.SendPropertyChanging();
this._PhotoPath = value;
this.SendPropertyChanged("PhotoPath");
this.OnPhotoPathChanged();
}
}
}

public event PropertyChangingEventHandler PropertyChanging;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}

protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
#pragma warning restore 1591

從中可以看到,這里本質(zhì)上就是以前的數(shù)據(jù)模型層,將數(shù)據(jù)整個(gè)對(duì)象化了,為了層之間的訪 問方便,我們將改類的命名空間改為Auto.DAL,并將NorthwindDataContent類的***個(gè)構(gòu)造函 數(shù)代碼修改如下,這樣修改主要是為了統(tǒng)一數(shù)據(jù)庫連接字符串的位置,因?yàn)閐bml文件在生成后 會(huì)附帶生成一個(gè)app.config文件,用來存放連接字符串,而我們要把連接字符串統(tǒng)一放到 web.config中。(要訪問web.config文件,需要為DAL項(xiàng)目添加對(duì)System.Configuration的應(yīng)用 )

public NorthwindDataContext() :
base (ConfigurationManager.ConnectionStrings ["NorthwindConnectionString"].ConnectionString, mappingSource)
{
OnCreated ();
}
 
完成前面的操作以后,開始建立數(shù)據(jù)訪問層的類文件,這時(shí),先在 DAL項(xiàng)目下添加一個(gè)類文件,取名為DALEmployees.cs,其代碼如下:
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Auto.DAL
{
public class DALEmployees
{
///
/// VS自動(dòng)生成的數(shù)據(jù)模型
///

NorthwindDataContext db = new NorthwindDataContext();

///
/// 根據(jù)條件獲取數(shù)據(jù)列表方法
///

/// 城 市地址
/// 獲取的Employees數(shù)據(jù)列表
public IQueryable GetList(string strCity)
{
//根據(jù)城市地質(zhì),通過LINQ返 回?cái)?shù)據(jù)
return from p in db.Employees
where p.City == strCity
select p;
}
}
}

這個(gè)數(shù)據(jù)訪問層中只建立了一個(gè)用來根據(jù)城市地址獲取Employee數(shù)據(jù)的方法,它內(nèi)部執(zhí)行一 段LINQ,返回一個(gè)IQueryable型的結(jié)果,由于LINQ后期編譯的特性,這個(gè)結(jié)果只有在程序運(yùn)行 后才會(huì)返回?cái)?shù)據(jù)集。

接著,建立業(yè)務(wù)邏輯層,在BLL項(xiàng)目中,添加一個(gè)類文件,取名為BLLEmployees.cs,其代碼 如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Auto.DAL;

namespace Auto.BLL
{
public class BLLEmployees
{
///
/// 通過城市名獲得Employee數(shù)據(jù)
///

/// 城市名
/// 數(shù)據(jù) 結(jié)果
public IQueryable GetList(string strCity)
{
DALEmployees de=new DALEmployees();
//調(diào)用數(shù)據(jù)訪問層的方法
return de.GetList(strCity);
}
}
}

這段代碼完成了業(yè)務(wù)邏輯層的定義,建立了一個(gè)和數(shù)據(jù)訪問層的同名方法GetList,用來傳 送城市名稱參數(shù)。***,在WEB Application項(xiàng)目中,添加對(duì)BLL層的應(yīng)用,并在Default.aspx 頁面中添加一個(gè)GridView控件用來顯示數(shù)據(jù),Default.aspx.cs的代碼如下:

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");
}}

///
/// 根據(jù)城市獲取數(shù)據(jù)
///

/// 城市名稱
private void GetList(string strCity)
{
//執(zhí)行業(yè)務(wù)邏輯層的方法
BLLEmployees bl = new BLLEmployees();
//綁 定到GridView1控件
GridView1.DataSource = bl.GetList (strCity);
GridView1.DataBind();
}}}

 

完成后,執(zhí)行程序,便可得到相關(guān)數(shù)據(jù),整個(gè)程序的結(jié)構(gòu)如下:

總結(jié):實(shí)際上,這個(gè)基于LINQ的架構(gòu)是將數(shù)據(jù)模型層和數(shù)據(jù)訪問層整合到一個(gè)項(xiàng)目中,它的 特點(diǎn)就是開發(fā)快速,效率高,可以很方便的幫你完成數(shù)據(jù)模型的搭建,并且也便于后期修改, 當(dāng)數(shù)據(jù)表發(fā)生變動(dòng)時(shí),只選要修改dbml文件就可以了。但這個(gè)架構(gòu)里有些地方還是不大完善, 例如城市數(shù)據(jù)作為參數(shù),應(yīng)該也是以數(shù)據(jù)模型的方式來傳送,但這里僅是按字符串傳遞,這樣 不利于數(shù)據(jù)的封裝和安全。因此還需要在此處改進(jìn)??傮w來說,比以往的三層架構(gòu)結(jié)構(gòu)更加清 晰了,如果再結(jié)合ASP.NET MVC架構(gòu)來使用,就更加***了。

【編輯推薦】

  1. 使用LINQ查詢非泛型類型
  2. 詳解在ASP.NET中用LINQ實(shí)現(xiàn)數(shù)據(jù)處理
  3. 手把手教你用好LINQ to SQL
責(zé)任編輯:彭凡 來源: cnblogs
相關(guān)推薦

2013-01-09 11:00:20

架構(gòu)開發(fā)三層架構(gòu).NET架構(gòu)

2011-04-19 13:53:41

三層架構(gòu)

2015-07-02 10:57:11

General框架架構(gòu)開發(fā)

2009-08-26 18:20:42

三層架構(gòu)

2009-07-28 17:25:14

ASP.NET三層結(jié)構(gòu)

2019-07-26 08:39:29

JavaWebMVC

2012-09-04 09:41:00

三層架構(gòu)架構(gòu)EF

2018-03-08 15:30:31

超融合架構(gòu)傳統(tǒng)三層架構(gòu)

2011-08-08 14:14:03

架構(gòu)

2012-04-16 10:45:17

三層架構(gòu)

2012-02-03 09:44:33

.NET

2009-04-30 15:56:50

三層架構(gòu)MVCMVP

2009-07-28 15:08:50

MVC三層架構(gòu)實(shí)例

2018-10-31 14:32:53

數(shù)據(jù)中心網(wǎng)絡(luò)架構(gòu)

2011-05-12 14:24:14

三層架構(gòu)

2009-07-28 17:18:33

2009-07-30 13:30:56

ASP.NET開發(fā)模式

2012-07-16 10:19:02

MongoDB

2012-02-07 10:40:13

MVCJava

2022-08-30 09:57:28

項(xiàng)目傳統(tǒng)對(duì)象訪問
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)