1. 建一个project 命名为DLinq ,添加一个Linq To SQL 的数据源,这里以经典的Northwind数据库为例,命名为NWDB.dbml 。
2
2. 建另一个Project 为DAL层 ,添加一个Table工厂, 这样我们就可以通过实体来获得Table
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DAL
- {
- public static class TableFactory
- {
- public static System.Data.Linq.Table<T> CreateTable<T>() where T : class
- {
- return Database.NWDB.GetTable<T>();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DAL
- {
- public static class Database
- {
- private static DLinq.NWDBDataContext _NWDB = null;
- public static DLinq.NWDBDataContext NWDB
- {
- get
- {
- if (_NWDB == null)
- _NWDB = new DLinq.NWDBDataContext();
- return _NWDB;
- }
- }
-
- }
- }
3. 借助Linq的特性,现在就可以写通用的数据库操作类了
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DAL
- {
- public class Utility
- {
- public static void Insert<T>(T TEntity) where T : class
- {
- var table = TableFactory.CreateTable<T>();
- table.InsertOnSubmit(TEntity);
- }
- public static IEnumerable<T> Where<T>(Func<T, bool> predicate) where T : class
- {
- var table = TableFactory.CreateTable<T>();
- return table.Where(predicate).AsEnumerable();
- }
- public static void SubmitChanges()
- {
- Database.NWDB.SubmitChanges();
- }
- }
- }
4. 现在让我们来写个测试方法来测试一下是否成功
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DAL;
- using DLinq;
- namespace DALTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- InsertTest();
- WhereTest();
- Console.WriteLine("All testings are success!");
- Console.Read();
- }
- private static void InsertTest()
- {
- Customer _customer=new Customer{
- CustomerID="Bruce",
- ContactName="Lee",
- CompanyName ="CodingSky",
- City ="Shenzhen"};
- Utility.Insert(_customer);
- Utility.SubmitChanges();
- }
- private static void WhereTest()
- {
- var _result= Utility.Where<Customer>(c => c.CustomerID == "Bruce");
- var _list = _result.ToList();
- if (_list.Count == 0)
- {
- Console.WriteLine("No result!");
- return;
- }
- Console.WriteLine("Query result is:");
- _list.ForEach(c => Console.WriteLine(Toolkits.StringExtension.ToString(c)));
- }
-
- }
- }
5. 其中WhereTest调用了另一个Project的StringExtension类,这个类主要扩展了ToString方法,通过Reflection 来读取实例的所有属性以及它们的值。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Toolkits
- {
- public class StringExtension
- {
- public static string ToString<T>(T t) where T:class
- {
- var typeInfo = BLReflection.GetProperties(typeof(T));
- var rType = (from q in typeInfo select new
- {
- TypeName= q.PropertyType.Name,
- PropName= q.Name ,
- Value= q.GetValue(t, null)
- }).ToList();
-
- StringBuilder sb = new StringBuilder();
- string header="Class Name: {0}\n";
- sb.AppendFormat(header , typeof(T).Name);
- rType.ForEach(c => sb.Append(String.Format ("\t{0}: {1} ({2}),\n", c.PropName, c.Value,c.TypeName) ));
- string result=sb.ToString ();
- return (result.Length > header.Length ? result.Substring(0, result.Length - 2)+"\n" : header);
- }
- }
- }
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!
好,今天就写到这里,希望能够带给你一点帮助!