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

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

    很久之前,在我刚学数据库编程的时候,就希望直接建一个  数据库通用的操作类, 即写 Insert(类实例), 那么剩下的工作就交给底层代码来实现如何Insert。 现在接触了Linq, 感觉离这个梦想的实现已经不远了。

    下面就分享一下我的源代码,很简单,估计刚学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!

 

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值