开发者

Can multiple classes share a single db table in ActiveRecord/NHibernate

C# w/Cast开发者_开发问答le ActiveRecord

I've two (or more) classes in C# that I'd like to interact with the same database table. These two classes do NOT share a base class, so I can't use a Discriminator. I'd simply like to divide the table columns over two (or more) classes. Kind of the opposite of the purpose of the JoinedTable attribute. Can this be done?


To expand on my comment, you can logically group the columns in the active record object by inheriting multiple interfaces...

interface IPerson
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

interface IAddress
{
    string AddressLine1 { get; set; }
    string AddressLine2 { get; set; }
    string City { get; set; }
    string Province { get; set; }
    string Country { get; set; }
}

class CustomerRecord : IPerson, IAddress // ActiveRecord object map to table...
{  
    // IPerson members...
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // IAddress members...
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string Country { get; set; }
}


There are two strategies to map several classes to one table:

Inheritance: yes, I know you don't have it, but you could also use an interface as "base class". It doesn't make sense to have several (root) classes in the same table witch don't have anything in common.

Components: are the actual opposite of join. You can put parts of the table to separate classes. In this case, there is only one root entity which aggregates other classes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜