下面就让我们来对上一篇提到的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;
- #