您的位置:首页 > 数据库 > SQL

基于LINQ to SQL的WEB三层架构(上)

2009-05-05 17:51 211 查看
  LINQ语言集成查询是微软用于抗衡竞争对手,改进现有开发模式下面向数据库操作问题的一个种全新体系架构。它的出现,解决了面向对象开发中由数据库操作引起的先天性的瓶颈,使得面向对象和数据库完美结合,程序员不再受限于复杂的SQL脚本,而可以一种近乎完美的方式来搭建自己的面向对象型软件系统,这种方式就是将数据完全对象化,将SQL语句封装到底层,由framework来完成,程序员只需面向数据库对象来编程,从另一种意义上来说,是把数据也程序化了。

  LINQ的这种开发模式也改变了系统架构的搭建方式,在以往的系统中,数据访问层DAL要访问数据字段,业务逻辑层BLL要得到数据都需要通过数据模型层Model来处理,而LINQ和VS2008为我们带来了一种全新的自动化方式生成数据模型层,这就是dbml(Database Mark Language。数据库描述语言,是一种xml格式的文档,用来描述数据库),有了它我们就不需要去找那些第三方的代码生成工具,只需要把数据表拖拽到设计器中,如下图所示,DONET便为我们做好了一切。

Northwind.dbml

#pragma warning disable 1591

//------------------------------------------------------------------------------

// <auto-generated>

// 此代码由工具生成。

// 运行时版本:2.0.50727.3053

//

// 对此文件的更改可能会导致不正确的行为,并且如果

// 重新生成代码,这些更改将会丢失。

// </auto-generated>

//------------------------------------------------------------------------------

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> Employees

{

get

{

return this.GetTable<Employees>();

}

}

}

[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<System.DateTime> _BirthDate;

private System.Nullable<System.DateTime> _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<int> _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<System.DateTime> value);

partial void OnBirthDateChanged();

partial void OnHireDateChanging(System.Nullable<System.DateTime> 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<int> 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<System.DateTime> 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<System.DateTime> 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<int> 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

从中可以看到,这里本质上就是以前的数据模型层,将数据整个对象化了,为了层之间的访问方便,我们将改类的命名空间改为Auto.DAL,并将NorthwindDataContent类的第一个构造函数代码修改如下,这样修改主要是为了统一数据库连接字符串的位置,因为dbml文件在生成后会附带生成一个app.config文件,用来存放连接字符串,而我们要把连接字符串统一放到web.config中。(要访问web.config文件,需要为DAL项目添加对System.Configuration的应用)

public NorthwindDataContext() :

base(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString, mappingSource)

{

OnCreated();

}

基于LINQ to SQL的WEB三层架构(下)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: