Search code examples
c#activerecordc#-4.0castle-activerecord

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


C# w/Castle 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?


Solution

  • 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; }
    }