如果你是力,以保持單獨的表,而你只是希望能夠寫 常用的方法,你可以使用一個接口和擴展方法如下圖所示:
public interface ITableBase{
int Id{ get; set; }
// other properties
void Method1();
int Method2();
string Method3(int some_arguments);
}
public partial class Table1 : ITableBase{
// implement ITableBase and other properties and methods
}
public partial class Table2 : ITableBase{
// implement ITableBase and other properties and methods
}
public partial class Table2 : ITableBase{
// implement ITableBase and other properties and methods
}
static public class ITableBaseExtensions{
static public string GetIdAsString(this ITableBase table){
return table.Id.ToString();
}
static public int UsingMethod2(this ITableBase table){
var i = table.Method2();
return i * 5 + 9 - 14/3 % 7 /* etc... */;
}
static public void AddingNewMethodToTables(this ITableBase table, string some_string,
int some_int, YourType any_other_parameter /* etc... */){
// implement the method you want add to all Table classes
}
//
// You can add any method you want to add to table classes, here, as an Extension method
//
}
而在消費者,你可以調用ITableBaseExtensions
類中定義的所有方法中的所有表:
public class MyConsumer{
public void MyMethod(){
Table1 t1 = new Table1();
Table1 t2 = new Table2();
Table1 t3 = new Table3();
t1.UsingMethod2(); // will be called from ITableBaseExtensions
t1.Method2(); // will be called from ITableBase - must implemented in Table1 class
t2.Method3(some_argument); // will be called from ITableBase - must implemented in Table2 class
t2.GetIdAsString(); // will be called from ITableBaseExtensions
// etc.
}
}
你能不能有作爲TABLETYPE另一列?那麼你可以有一張桌子。 – Damith
@DSW:出於性能考慮而拆分 - 每個表有數百萬行,所以我想將它們分開。 – BlueChippy
這將幫助你http://blogs.msdn.com/b/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx – Damith