去看更多

wangshijie
Welcome

转: 建一个 Linq 数据库通用的操作类 (上)

1. 建一个project 命名为DLinq ,添加一个Linq To SQL 的数据源,这里以经典的Northwind数据库为例,命名为NWDB.dbml 。

    

    2

    

    2. 建另一个Project 为DAL层 ,添加一个Table工厂, 这样我们就可以通过实体来获得Table

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace DAL
  6. {
  7.     public static  class TableFactory
  8.     { 
  9.         public static System.Data.Linq.Table<T> CreateTable<T>() where T : class
  10.         {
  11.             return Database.NWDB.GetTable<T>();
  12.         }
  13.     }
  14. }

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace DAL
  6. {
  7.     public   static class Database
  8.     {
  9.         private static DLinq.NWDBDataContext _NWDB = null;
  10.         public static DLinq.NWDBDataContext NWDB
  11.         {
  12.             get
  13.             {
  14.                 if (_NWDB == null)
  15.                     _NWDB = new DLinq.NWDBDataContext();
  16.                 return _NWDB;
  17.             }
  18.         }
  19.        
  20.     }
  21. }

    3. 借助Linq的特性,现在就可以写通用的数据库操作类了

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace DAL
  6. {
  7.     public class Utility
  8.     {
  9.         public static void Insert<T>(T TEntity) where T : class
  10.         {
  11.             var table = TableFactory.CreateTable<T>();
  12.             table.InsertOnSubmit(TEntity);
  13.         }
  14.         public static IEnumerable<T> Where<T>(Func<T, bool> predicate) where T : class
  15.         {
  16.             var table = TableFactory.CreateTable<T>();
  17.             return table.Where(predicate).AsEnumerable();
  18.         }
  19.         public static void SubmitChanges()
  20.         {
  21.             Database.NWDB.SubmitChanges();
  22.         }
  23.     }
  24. }

    4. 现在让我们来写个测试方法来测试一下是否成功

 

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using DAL;
  5. using DLinq;
  6. namespace DALTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             InsertTest();
  13.             WhereTest();
  14.             Console.WriteLine("All testings are success!");
  15.             Console.Read();
  16.         }
  17.         private static void InsertTest()
  18.         {
  19.             Customer _customer=new Customer{ 
  20.                 CustomerID="Bruce",
  21.                  ContactName="Lee",
  22.                  CompanyName ="CodingSky",
  23.                  City ="Shenzhen"};
  24.             Utility.Insert(_customer);
  25.             Utility.SubmitChanges();
  26.         }
  27.         private static void WhereTest()
  28.         {
  29.             var _result= Utility.Where<Customer>(c => c.CustomerID == "Bruce");
  30.             var _list = _result.ToList();
  31.             if (_list.Count == 0)
  32.             {
  33.                 Console.WriteLine("No result!");
  34.                 return;
  35.             }
  36.             Console.WriteLine("Query result is:");
  37.             _list.ForEach(c => Console.WriteLine(Toolkits.StringExtension.ToString(c)));
  38.         }
  39.         
  40.     }
  41. }

    5. 其中WhereTest调用了另一个Project的StringExtension类,这个类主要扩展了ToString方法,通过Reflection 来读取实例的所有属性以及它们的值。

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Toolkits
  6. {
  7.     public class StringExtension
  8.     {
  9.         public static string ToString<T>(T t) where T:class 
  10.         {
  11.             var typeInfo = BLReflection.GetProperties(typeof(T));
  12.             var rType = (from q in typeInfo select new 
  13.             {
  14.                            TypeName=  q.PropertyType.Name,
  15.                            PropName= q.Name ,
  16.                            Value= q.GetValue(t, null)
  17.             }).ToList();
  18.             
  19.             StringBuilder sb = new StringBuilder();
  20.             string header="Class Name: {0}\n";
  21.             sb.AppendFormat(header , typeof(T).Name);
  22.             rType.ForEach(c => sb.Append(String.Format ("\t{0}: {1} ({2}),\n", c.PropName, c.Value,c.TypeName) ));
  23.             string result=sb.ToString ();
  24.             return (result.Length > header.Length ? result.Substring(0, result.Length - 2)+"\n" : header);           
  25.         }
  26.     }
  27. }

    6. 最后,输出的结果应该是这样:

 

Query result is:
Class Name: Customer
        CustomerID: Bruce (String),
        CompanyName: CodingSky (String),
        ContactName: Lee (String),
        ContactTitle:  (String),
        Address:  (String),
        City: Shenzhen (String),
        Region:  (String),
        PostalCode:  (String),
        Country:  (String),
        Phone:  (String),
        Fax:  (String),
        Orders: System.Data.Linq.EntitySet`1[DLinq.Order] (EntitySet`1)

 

All testings are success!

 

好,今天就写到这里,希望能够带给你一点帮助!

posted @ 2009-05-06 17:20  jeer  阅读(499)  评论(1编辑  收藏  举报