使用LINQ实现数据访问层
数据访问层包含与Data Linq交互的代码,它使用集成语言查询来访问Data Linq层。下图2.1 展示了数据访问层的细节。基本上,它包括了所有与上层交互的方法,并完成与数据库相关的操作。
2.1:数据访问层-详细视图
 
在示例程序中,数据访问层包含了一个简单的组件DALCustomer,相关代码如下(http://www.EntLib.com 开源小组注: 这里相关的示例代码采用C#,原文为VB代码):
 
代码片段1.1:数据访问层
    public class DALCustomer
    {
        private DBLinqDataContext objDataContext = new DBLinqDataContext();
 
        public Table<Customer> SelectRecordAll()
        {
            try
            {
               return objDataContext.Customers;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
 
        public Customer SelectRecordByID(string customerID)
        {
            try
            {
                return (from cust in objDataContext.Customers
                        where cust.CustomerID == customerID
                        select cust).Single();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public List<Customer> SelectRecordByIDListable(string customerID)
        {
            List<Customer> localTable;
            try
            {
                localTable = (from cust in objDataContext.Customers
                              where cust.CustomerID == customerID
                              select cust).ToList();
 
                return localTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public string InsertRecord(Customer localTable)
        {
            try
            {
                objDataContext.Customers.InsertOnSubmit(localTable);
                objDataContext.SubmitChanges();
 
                return localTable.CustomerID;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public void UpdateRecord(Customer localTable)
        {
            try
            {
                objDataContext.Customers.Attach(localTable);
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, localTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public void DeleteRecord(string customerID)
        {
            try
            {
                objDataContext.Customers.DeleteOnSubmit((from cust in objDataContext.Customers
                                                        where cust.CustomerID==customerID
                                                             select cust).Single());
                objDataContext.SubmitChanges();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
 
        public Table<Order> SelectAllOrder()
        {
            try
            {
                return objDataContext.Orders;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public Table<Order_Detail> SelectAllOrderDetail()
        {
            try
            {
                return objDataContext.Order_Details;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
 
在数据访问层创建的DataContext实例负责访问数据库操作相关的方法和属性。正如我们前面讨论的,DataContext类包含了数据库表的属性和产生动态SQL脚本执行数据库操作的相关方法。对于每一个数据表,均有一个对应的实体类,实体类的属性映射到实际的数据表。访问DataContext类的属性,可以返回一个Table实体集合。
DALCustomer组件包含了下面类图中的公有方法列表。SelectRecordAll 方法访问DataContext类的属性,检索所有的记录。代码如下:
return objDataContext.Customers;
 
SelectRecordAll()方法实际上调用DataContext类的GetTable()方法,检索所有记录。并且这些返回的记录存放在Table对象内,可以通过LINQ进行检索。可以注意到SelectRecordByID()方法查询objDataContext.Customers 返回的Table实体集合,并得到一条唯一的记录。
return (from cust in objDataContext.Customers
        where cust.CustomerID == customerID
        select cust).Single();
 
Insert()方法接收传入的实体对象参数,该参数包含了需要插入到数据库的新纪录。代码如下:
                objDataContext.Customers.InsertOnSubmit(localTable);
                objDataContext.SubmitChanges();
 
增加新纪录到数据集合,并调用SubmitChanges() 方法传入更新信息到数据库。
Update() 方法接收传入的实体对象参数,该参数包含有更新的记录。代码如下:
                objDataContext.Customers.Attach(localTable);
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, localTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
 
附带(Attach)有更新的记录到数据集合,然后调用Refresh() 方法,定义如何处理附带的记录,SubmitChanges() 方法-传入ConflictMode.ContinueOnConflict 参数,决定如何更新数据库。
 
DeleteRecord() 方法接收传入的ID参数,并查询数据集合,选择一条唯一的记录,然后调用DataContext类的DeleteOnSubmit () 方法,删除前面从数据集合中选择的记录。最后,SubmitChanges() 方法负责将更新的信息传回数据库。
objDataContext.Customers.DeleteOnSubmit((from cust in objDataContext.Customers
                                         where cust.CustomerID==customerID
                                         select cust).Single());
objDataContext.SubmitChanges();
 
DALCustomer组件的类图如下所示:

图 2.2:数据访问层 – 类图


EntLib.com 开源小组注:本文翻译《Building Multi-Tier Web Application in .NET 3.5 Framework Using LINQ to SQL》。后面内容待续。欢迎交流LINQ相关技术。
posted on 2009-04-27 10:45  ~辰~  阅读(118)  评论(0编辑  收藏  举报