水平分割数据表

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using System.Linq.Expressions;

  6 namespace Model

  7 {

  8     public interface  ITable

  9     {

 10         int ID

 11         {

 12             get;

 13             set;

 14         }

 15         string Name

 16         {

 17             get;

 18             set;

 19         }

 20         System.Nullable<int> Age

 21         {

 22             get;

 23             set;

 24         }

 25     }

 26 

 27     public partial class TableA : ITable

 28     {

 29     }

 30     public partial class TableB : ITable

 31     {

 32     }

 33     public partial class TableC : ITable

 34     {

 35     }

 36 

 37     public enum TableType

 38     {

 39         TableA,

 40         TableB,

 41         TableC

 42     }

 43 

 44     // 对于3个表结构一样,编写共用的方法

 45     public class TableX

 46     {

 47         protected static CoreDataContext Core=new CoreDataContext();        

 48         public TableX(TableType tabletype)

 49         {

 50             type=tabletype;

 51         }

 52         private TableType type;

 53         public  IQueryable<ITable> GetList()//协变引用 

 54         {

 55            switch (type)

 56             {

 57                 case TableType.TableA:

 58                        return Core.TableAs;

 59                 case TableType.TableB:

 60                        return Core.TableBs;

 61                 case TableType.TableC:

 62                        return Core.TableCs;                      

 63                 default:

 64                        return null;

 65             }

 66         }

 67 

 68         public  ITable Get(int id)

 69         {

 70             return GetList().FirstOrDefault(o => o.ID==id);

 71         }

 72         public ITable Set(string name, int age)

 73         {

 74             ITable item;

 75             switch (type)

 76             {

 77                 case TableType.TableA:

 78                     item=new TableA();

 79                     break;

 80                 case TableType.TableB:

 81                     item=new TableB();

 82                     break;

 83                 default:

 84                     item=new TableC();

 85                     break; 

 86             }

 87             item.Name=name;

 88             item.Age=age;

 89             return  item;

 90         }

 91      

 92         public  void Add(string name,int age)

 93         { 

 94             ITable t=Set(name, age);

 95             switch (type)

 96             {

 97                 case TableType.TableA:                 

 98                      Core.TableAs.InsertOnSubmit(t as TableA);

 99                      break;

100                 case TableType.TableB:

101                      Core.TableBs.InsertOnSubmit(t as TableB);

102                      break;

103                 default:

104                      Core.TableCs.InsertOnSubmit(t as TableC);

105                      break;

106             }

107             Core.SubmitChanges();         

108             

109         }

110 

111         public  void Update(ITable item)

112         {

113             var it=Get(item.ID);

114             if (it==null)

115                   return;

116             it.Name=item.Name;

117             it.Age=item.Age;

118             Core.SubmitChanges();

119         }

120    

121         public  void Delete(int ID)

122         {

123             var it=Get(ID);

124             if (it==null)

125                 return;

126             switch (type)

127             {

128                 case TableType.TableA:

129                       Core.TableAs.DeleteOnSubmit(it as TableA);      

130                       break;

131                 case TableType.TableB:

132                      Core.TableBs.DeleteOnSubmit(it as TableB);

133                      break;

134                 default:

135                      Core.TableCs.DeleteOnSubmit(it as TableC);

136                      break;

137             }

138             Core.SubmitChanges();

139         }

140          

141     }

142 

143 }

水平分割数据表

1             string name="wangriqiang";

2             int age=101;

3             var t=new TableX(TableType.TableA);

4             t.Add(name, age);

二  使用BaseRepository<T>

 1     public class ITableRepository<T>: BaseRepository<T> where T :class,ITable,new()

 2     {

 3     }

 4 

 5     public class Tablexx<T> where T:class,ITable,new()

 6     {

 7         ITableRepository<T> rep=new ITableRepository<T>();

 8         

 9         public IQueryable<ITable> GetList()

10         {

11             return rep.All as IQueryable<ITable>;

12         }

13       

14         public ITable GetByID(int id)

15         {

16            return  rep.GetById(id);

17         }

18 

19         public void DeleteByID(int id)

20         {

21             rep.DeleteById(id);

22             rep.Save();

23         }

24 

25         public void Add(ITable it)

26         {   

27             T t=new T();

28             t.ID=it.ID;

29             t.Name=it.Name;

30             t.Age=it.Age;

31             rep.Add(t);

32             rep.Save();

33         }

34         public void Delete(ITable it)

35         {

36             rep.DeleteById(it.ID);

37             rep.Save();

38         }

39 

40         public void Update(ITable it)

41         {

42             var t=GetByID(it.ID);

43             t.Name=it.Name;

44             t.Age=it.Age;

45             rep.Save();

46         }

47 

48     }

49     public class TableHelper

50     {

51         public static void Add(ITable it)

52         {

53             Call("Add", it);

54         }

55        

56         public static void Delete(ITable it)

57         {

58             Call("Delete", it);

59         } 

60         public static void Update(ITable it)

61         {

62             Call("Update", it);           

63         }

64 

65         public static void Call(string funcname, ITable it)

66         {

67             Type tabletype=it.GetType();

68             var tx=typeof(Tablexx<>);

69             Type constructed=tx.MakeGenericType(tabletype);

70             object o = Activator.CreateInstance(constructed);

71             var addmm=constructed.GetMethod(funcname);

72             addmm.Invoke(o, new[] { it });

73         }

74 

75     }

76 }

 

你可能感兴趣的:(数据)