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!
好,今天就寫到這裡,希望能夠帶給你一點協助!