Funny Linq Part2: 建一个 Linq 数据库通用的操作类 (下)
上一篇文章介绍了如何实现一个通用的Insert操作, 即直接 Utility.Insert(xxx) 就可以实现了。那么, 大家可能就顺理成章的想到是否也可以实现通用的Update, Delete , Query 操作, 答案是肯定的!
下面就让我们来对上一篇提到的DAL扩展一下:
- 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 void Delete<T>(T TEntity) where T : class
- {
- var table = TableFactory.CreateTable<T>();
- table.DeleteOnSubmit(TEntity);
- }
- public static void Delete<T>(Func<T, bool> predicate) where T : class
- {
- var table = TableFactory.CreateTable<T>();
- var dResult = Where<T>(predicate);
- table.DeleteAllOnSubmit(dResult );
- }
- public static void Update<T>(T TEntity, Action<T> action)
- {
- action(TEntity);
- SubmitChanges();
- }
- public static void InsertAll<T>(IEnumerable <T> TEntities) where T : class
- {
- var table = TableFactory.CreateTable<T>();
- table.InsertAllOnSubmit( TEntities);
- }
- public static void DeleteAll<T>(IEnumerable<T> TEntities) where T : class
- {
- var table = TableFactory.CreateTable<T>();
- table.DeleteAllOnSubmit(TEntities);
- }
- public static IEnumerable<T> SelectAll<T>() where T : class
- {
- var table = TableFactory.CreateTable<T>();
- return table.Select(c => c).AsEnumerable();
- }
- 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();
- }
- }
- }
同样的, 我们也是写一些Test 方法来验证一下是否正确
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DAL;
- using DLinq;
- using Toolkits;
- namespace DALTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- //SelectAllTest();
- InsertTest();
- WhereTest();
- UpdateTest();
- WhereTest();
- DeleteTest1();
- 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 DeleteTest1()
- {
- Utility.Delete<Customer>(c => c.CustomerID == "Bruce");
- Utility.SubmitChanges();
- }
- private static void DeleteTest2()
- {
- var _result= Utility.Where<Customer>(c => c.CustomerID == "Bruce");
- if (_result.Count() > 0)
- {
- Utility.Delete(_result.First());
- Utility.SubmitChanges();
- }
- }
- private static void UpdateTest()
- {
- var _result= Utility.Where<Customer>(c => c.CustomerID == "Bruce");
- if (_result.Count() > 0)
- {
- var _customer = _result.First();
- if (_customer != null)
- {
- Utility.Update(_customer, c =>
- {
- c.ContactName = "Jack";
- c.CompanyName = "Microsoft";
- c.City = "Beijing";
- });
- Utility.SubmitChanges();
- }
- }
- }
- private static void SelectAllTest()
- {
- var _result = Utility.SelectAll<Customer>();
- var _list = _result.ToList();
- if (_list.Count == 0)
- {
- Console.WriteLine("No result!");
- return;
- }
- _list.ForEach(c => Console.WriteLine(StringExtension.ToString(c)));
- }
- 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(StringExtension.ToString(c)));
- }
- }
- }
其他代码(例如TableFactory,StringExtension)请参考上一篇 :http://blog.csdn.net/fengart/archive/2008/08/19/2798534.aspx
以上的Test是先在Customers表中Insert一个叫Bruce的家伙,接着把他的ContactName修改为Jack ,最后Delete他。
(其中Customers表的数据太多,所以我把SelectAllTest方法注释掉了)
最后打印的结果应该是这样:
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)
Query result is:
Class Name: Customer
CustomerID: Bruce (String),
CompanyName: Microsoft (String),
ContactName: Jack (String),
ContactTitle: (String),
Address: (String),
City: Beijing (String),
Region: (String),
PostalCode: (String),
Country: (String),
Phone: (String),
Fax: (String),
Orders: System.Data.Linq.EntitySet`1[DLinq.Order] (EntitySet`1)
No result!
All testings are success!
如果你有什么更好的建议,欢迎交流一下!我会非常乐意聆听。