基于LINQ to SQL的WEB开发三层架构(2)
初始的Northwind.dbml文件代码如下:
#pragma warning disable 1591 //------------------------------------------------------------------------------ // // 此代码由工具生成。 // 运行时版本:2.0.50727.3053 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // //------------------------------------------------------------------------------ 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
从中可以看到,这里本质上就是以前的数据模型层,将数据整个对象化了,为了层之间的访 问方便,我们将改类的命名空间改为Auto.DAL,并将NorthwindDataContent类的第一个构造函 数代码修改如下,这样修改主要是为了统一数据库连接字符串的位置,因为dbml文件在生成后 会附带生成一个app.config文件,用来存放连接字符串,而我们要把连接字符串统一放到 web.config中。(要访问web.config文件,需要为DAL项目添加对System.Configuration的应用 )
public NorthwindDataContext() : base (ConfigurationManager.ConnectionStrings ["NorthwindConnectionString"].ConnectionString, mappingSource) { OnCreated (); } |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Auto.DAL { public class DALEmployees { /// /// VS自动生成的数据模型 /// NorthwindDataContext db = new NorthwindDataContext(); /// /// 根据条件获取数据列表方法 /// /// 城 市地址 /// 获取的Employees数据列表 public IQueryable GetList(string strCity) { //根据城市地质,通过LINQ返 回数据 return from p in db.Employees where p.City == strCity select p; } } }
这个数据访问层中只建立了一个用来根据城市地址获取Employee数据的方法,它内部执行一 段LINQ,返回一个IQueryable型的结果,由于LINQ后期编译的特性,这个结果只有在程序运行 后才会返回数据集。
接着,建立业务逻辑层,在BLL项目中,添加一个类文件,取名为BLLEmployees.cs,其代码 如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Auto.DAL; namespace Auto.BLL { public class BLLEmployees { /// /// 通过城市名获得Employee数据 /// /// 城市名 /// 数据 结果 public IQueryable GetList(string strCity) { DALEmployees de=new DALEmployees(); //调用数据访问层的方法 return de.GetList(strCity); } } }
这段代码完成了业务逻辑层的定义,建立了一个和数据访问层的同名方法GetList,用来传 送城市名称参数。最后,在WEB Application项目中,添加对BLL层的应用,并在Default.aspx 页面中添加一个GridView控件用来显示数据,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"); }} /// /// 根据城市获取数据 /// /// 城市名称 private void GetList(string strCity) { //执行业务逻辑层的方法 BLLEmployees bl = new BLLEmployees(); //绑 定到GridView1控件 GridView1.DataSource = bl.GetList (strCity); GridView1.DataBind(); }}} |
完成后,执行程序,便可得到相关数据,整个程序的结构如下:
总结:实际上,这个基于LINQ的架构是将数据模型层和数据访问层整合到一个项目中,它的 特点就是开发快速,效率高,可以很方便的帮你完成数据模型的搭建,并且也便于后期修改, 当数据表发生变动时,只选要修改dbml文件就可以了。但这个架构里有些地方还是不大完善, 例如城市数据作为参数,应该也是以数据模型的方式来传送,但这里仅是按字符串传递,这样 不利于数据的封装和安全。因此还需要在此处改进。总体来说,比以往的三层架构结构更加清 晰了,如果再结合ASP.NET MVC架构来使用,就更加完美了。
基于LINQ to SQL的WEB开发三层架构(2)相关推荐
- 基于LINQ to SQL的WEB开发三层架构(1)
LINQ语言集成查询是微软用于抗衡竞争对手,改进现有开发模式下面向数据库操作问题的一个种全新体系架构.它的出现,解决了面向对象开发中由数据库操作引起的先天性的瓶颈,使得面向对象和数据库完美结合. 程序 ...
- WEB开发三层架构概述
SSH的三层结构 DAO 对数据库的操作的接口和实现 SERVICE 业务逻辑的实现,也可以使用dao提供的数据库操作的接口实现 ACTION 实现调用业务逻辑方法来处理用户的请求 三层结构 3-t ...
- JDBC事务--软件开发三层架构--ThreadLocal
JDBC事务--软件开发三层架构--ThreadLocal 一.JDBC事务 1.概述: 事务是指逻辑上的一组操作!这一组操作,通常认为是一个整体,不可拆分! 特点:同生共死;事务内的这一组操作要么全 ...
- 【Web开发的架构】
[Web开发的架构] Web开发分为:B/S架构.C/S架构 B/S 架构 B/S架构的全称为Browser/Server,即浏览器/服务器结构.Browser指的是Web浏览器,极少数事务逻辑在前端 ...
- 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构zhen项目
原项目链接:https://github.com/shuzheng/zheng 目前看过做完整的项目,大家一起学习一下,详情请点击原项目链接的地址 前言 zheng项目创建于2016年10月4日,正在 ...
- 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构---权限管理系统
原文链接: https://github.com/shuzheng/zheng 安利一个GitHub上的项目,见原文链接. 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构, ...
- 基于Stripes框架进行Java Web开发
Mark Eagle是美国乔治亚州亚特兰大市MATRIX Resources有限公司的一位资深软件工程师,拥有Sun公司的SCP和SCWCD认证.Mark本人非常喜欢使用开源软件进行软件开发,并且多次 ...
- 基于SQLServer ,使用Java Web开发的学生信息管理系统,在校大学生和新手可参考学习
目的 ①了解使用数据库创建和表的创建. ②了解使用数据的添加和增删改查等SQL语句. ③理解数据库的表之间的关系处理. ④构建良好安全的后台数据管理系统. 二 设备及材料 一台装有SQL Server ...
- 关于J2EE开发三层架构
现在开发web大多都是基于MVC的三层架构,三层架构优势什么呢? 三层架构(3-tier application) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL). ...
最新文章
- 如何让插件代码同时兼容ats 5.x和ats 6.x的方法
- 用日志记录LINQ中的所有增删改的SQL语句的方法
- java和C中基本数据类型的所占字节数对比
- Unknown: Failed to get convolution algorithm. This is probably because cuDNN
- 从羊毛党看一个成功的裂变活动
- OpenVINO+OpenCV 文本检测与识别
- C#接口的作用(经典)
- Android中ICS4.0源码Launcher启动流程分析【android源码Launcher系列一】
- mysql用reader读取空数据时报错的处理
- 如何获取 Instagram 视频以及图片
- CMOS门电路(OD门、传输门、双向模拟开关、三态门)
- 联想 ThinkBook 15 LLI 蓝屏恢复
- 2021江苏大学生编程大赛I题(省赛试水)
- activity串行多实例审批
- 基于springboot的课堂考勤签到打卡小程序
- Caas社区作为一种服务
- mysql rownum写法_MySQL 的 RowNum 实现
- springboot整合多线程ThreadPoolTaskExecutor
- 乌镇论剑:张朝阳的四张牌=两横两纵
- ntp计算机系统时间总是跳,电脑时间老是自动跳的解决方法 电脑时间每小时自动校准方法的图文教程...