Linq 数据库通用的操作类

作者: Robin 分类: Asp.net 发布时间: 2008-12-07 15:14
  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 void Delete<T>(T TEntity) where T : class
  15.         {
  16.             var table = TableFactory.CreateTable<T>();
  17.             table.DeleteOnSubmit(TEntity);
  18.         }
  19.         public static void Delete<T>(Func<T, bool> predicate) where T : class
  20.         {
  21.             var table = TableFactory.CreateTable<T>();
  22.             var dResult = Where<T>(predicate);
  23.             table.DeleteAllOnSubmit(dResult );
  24.         }
  25.         public static void Update<T>(T TEntity, Action<T> action)
  26.         {
  27.             action(TEntity);
  28.             SubmitChanges();
  29.         }
  30.         public static void InsertAll<T>(IEnumerable <T> TEntities) where T : class
  31.         {
  32.             var table = TableFactory.CreateTable<T>();
  33.             table.InsertAllOnSubmit( TEntities);
  34.         }
  35.         public static void DeleteAll<T>(IEnumerable<T> TEntities) where T : class
  36.         {
  37.             var table = TableFactory.CreateTable<T>();
  38.             table.DeleteAllOnSubmit(TEntities);
  39.         }
  40.         public static IEnumerable<T> SelectAll<T>() where T : class
  41.         {
  42.             var table = TableFactory.CreateTable<T>();
  43.             return table.Select(c => c).AsEnumerable();
  44.         }
  45.         public static IEnumerable<T> Where<T>(Func<T, bool> predicate) where T : class
  46.         {
  47.             var table = TableFactory.CreateTable<T>();
  48.             return table.Where(predicate).AsEnumerable();
  49.         }
  50.         public static void SubmitChanges()
  51.         {
  52.             Database.NWDB.SubmitChanges();
  53.         }
  54.     }
  55. }

 

    同样的, 我们也是写一些Test 方法来验证一下是否正确

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DAL;
  6. using DLinq;
  7. using Toolkits;
  8. namespace DALTest
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //SelectAllTest(); 
  15.             InsertTest();
  16.             WhereTest();
  17.             UpdateTest();
  18.             WhereTest();
  19.             DeleteTest1();
  20.             WhereTest();
  21.             Console.WriteLine(“All testings are success!”);
  22.             Console.Read();
  23.         }
  24.         private static void InsertTest()
  25.         {
  26.             Customer _customer=new Customer{ 
  27.                 CustomerID=“Bruce”,
  28.                  ContactName=“Lee”,
  29.                  CompanyName =“CodingSky”,
  30.                  City =“Shenzhen”};
  31.             Utility.Insert(_customer);
  32.             Utility.SubmitChanges();
  33.         }
  34.         private static void DeleteTest1()
  35.         {
  36.             Utility.Delete<Customer>(c => c.CustomerID == “Bruce”);
  37.             Utility.SubmitChanges();
  38.         }
  39.         private static void DeleteTest2()
  40.         {
  41.             var _result= Utility.Where<Customer>(c => c.CustomerID == “Bruce”);
  42.             if (_result.Count() > 0)
  43.             {
  44.                 Utility.Delete(_result.First());
  45.                 Utility.SubmitChanges();
  46.             }
  47.         }
  48.         private static void UpdateTest()
  49.         {
  50.             var _result= Utility.Where<Customer>(c => c.CustomerID == “Bruce”);
  51.             if (_result.Count() > 0)
  52.             {
  53.                 var _customer = _result.First();
  54.                 if (_customer != null)
  55.                 {
  56.                     Utility.Update(_customer, c =>
  57.                     {
  58.                         c.ContactName = “Jack”;
  59.                         c.CompanyName = “Microsoft”;
  60.                         c.City = “Beijing”;
  61.                     });
  62.                     Utility.SubmitChanges();
  63.                 }
  64.             }
  65.         }
  66.         private static void SelectAllTest()
  67.         {
  68.             var _result = Utility.SelectAll<Customer>();
  69.             var _list = _result.ToList();
  70.             if (_list.Count == 0)
  71.             {
  72.                 Console.WriteLine(“No result!”);
  73.                 return;
  74.             }
  75.             _list.ForEach(c => Console.WriteLine(StringExtension.ToString(c)));
  76.         }
  77.         private static void WhereTest()
  78.         {
  79.             var _result= Utility.Where<Customer>(c => c.CustomerID == “Bruce”);
  80.             var _list = _result.ToList();
  81.             if (_list.Count == 0)
  82.             {
  83.                 Console.WriteLine(“No result!”);
  84.                 return;
  85.             }
  86.             Console.WriteLine(“Query result is:”);
  87.             _list.ForEach(c => Console.WriteLine(StringExtension.ToString(c)));
  88.         }
  89.         
  90.     }
  91. }

    其他代码(例如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.EntitySet1[DLinq.Order] (EntitySet1)

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.EntitySet1[DLinq.Order] (EntitySet1)

No result!
All testings are success!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

标签云