开发者

Map multiple entities to EF4 abstract base class or interface?

开发者 https://www.devze.com 2023-04-08 10:13 出处:网络
I have multiple tables in a db that are identical in terms of column structure. I\'d like to have this modelled in EF4 so that they all inherit from a single abstract base class, so I can write commo

I have multiple tables in a db that are identical in terms of column structure.

I'd like to have this modelled in EF4 so that they all inherit from a single abstract base class, so I can write common methods etc.

I've tried an abstract base class, but this means that I need to add a property to each entity that is NOT in the base class. It also appears I lose my collections, as each one is now a collection of the base type?

I can add partial classes and inherit each from a common interface?

e.g.

Table 1     Table 2     Table 3
Id          Id          Id
Month1开发者_运维问答      Month1      Month1
Month2      Month2      Month2
Month3      Month3      Month3
Quarter2    Quarter2    Quarter2
Quarter3    Quarter3    Quarter3
Quarter4    Quarter4    Quarter4

How can I have a base class or interface called "table" so that I can write all my methods against that and not against each individual table?


If you are force to keep separate tables, and you just want to be able to write common methods, you can use an Interface and Extension methods like below:

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
    //
}

And in consumers, you can call all methods defined in ITableBaseExtensions class for all tables:

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.
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号