微型项目实践(8):数据访问的实现
上一篇文章,我们分析了数据访问是如何定义,以及如何与其它(特别是Business)模块耦合的,今 天我们来看一下数据访问(DataAccess)是如何实现的。 从系统结构图和上一篇“数据访问的定义”中可以知道,数据访问层的任务就是实现 IEntityDataAccess和IDatabase接口。由于Linq的出现,我们只需要一个Adapter,将Linq2Sql的类适配 到IEntityDataAccess就可以了,其类设计图和文件结构如下如下: 貫夕嶄辛參心欺?Database窃写覚嚥DataContext?遇糞?阻IDatabase俊笥? EntityDataAccessAdapter聞喘System.Data.Linq.Table宸倖Linq議宰伉窃糞?阻IEntityDataAccess俊笥 。 Database窃議旗鷹泌和? 1: using System; 2: using System.Collections.Generic; 3: using System.Data.Linq; 4: using System.Data.Linq.Mapping; 5: using System.IO; 6: using System.Linq; 7: using System.Reflection; 8: using System.Text; 9: 10: using DongBlog.Common; 11: using DongBlog.Business; 12: using DongBlog.Business.Blogs; 13: 14: namespace DongBlog.DataAccess 15: { 16: /// <summary> 17: /// 方象垂 18: /// </summary> 19: public class Database : DataContext, IDatabase 20: { 21: #region Singelton Pattern 22: 23: private const string MAPPING_SOURCE_RESOURCE_NAME = "Srims.DataAccess.MappingSource.Xml"; 24: private static MappingSource _MappingSource; 25: private static object _Locker = new object(); 26: 27: private Database(string connectionString, MappingSource mappingSource) 28: : base(connectionString, mappingSource) 29: { 30: } 31: 32: private static MappingSource getMappingSource() 33: { 34: if (_MappingSource == null) 35: { 36: lock (_Locker) 37: { 38: if (_MappingSource == null) 39: { 40: Stream mapingSourceStream = Assembly.GetAssembly(typeof(Database)) .GetManifestResourceStream(MAPPING_SOURCE_RESOURCE_NAME); 41: if (mapingSourceStream == null) 42: throw new System.IO .InvalidDataException( "啌符猟周音贋壓?艇鳩協厮将繍凪園咎奉來譜崔葎\"廼秘彿坿?Embedded Resource?\"?"); 43: _MappingSource = XmlMappingSource.FromStream(mapingSourceStream); 44: } 45: } 46: } 47: return _MappingSource; 48: } 49: 50: /// <summary> 51: /// 更夛仟議方象垂 52: /// </summary> 53: /// <param name="connectionString">方象垂銭俊忖憲堪</param> 54: /// <returns>方象垂糞箭</returns> 55: public static Database New(string connectionString) 56: { 57: if (str |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |