4

This is a pretty basic question but it's bugging me. Should I combine similar Model properties together into one Class Model or is it better to split them up and add inheritance where it makes sense? I tend to like to return only data necessary for each request but I question it when it's a smaller amount of data. It's better to explain with an example.

I have a web application that does various AJAX calls. Many of the calls return IEnumerables

public class GroupBaseModel {
    public int GroupId { get; set; }
    public int GroupName { get; set; }
}
public class RoleBaseModel
{
    public int RoleId { get; set; }
    public int RoleName { get; set; }
}
public class GroupMemberModel : GroupBaseModel
{
    public bool IsMember { get; set; }
}
public class GroupRoleBaseModel {
    public int GroupId { get; set; }
    public int GroupName { get; set; }
    public int RoleId { get; set; }
    public int RoleName { get; set; }
}

I have calls that return IEnumerables of each of these classes on several different pages. Some calls just return the GroupRoleModel, while another will return the GroupRoleBaseModel etc. (each is pulling the minimum required data for each request at this time)

So my first question, is it possible to concatenate two existing Classes? For example above, the GroupRoleBaseModel is just the properties of the GroupBaseModel + GroupRoleModel, can I combine them without implementing all the properties again?

The second question is, since we are talking about very small classes, should I just do like below and use one single class? Even though some requests will return unneeded data?

public class GroupBaseModel {
    public int GroupId { get; set; }
    public int GroupName { get; set; }
    public int RoleId { get; set; }
    public int RoleName { get; set; }
    public bool IsMember { get; set; }
}

Thanks for your thoughts!

sparkyShorts
  • 630
  • 9
  • 28
cmartin
  • 2,819
  • 1
  • 26
  • 31

2 Answers2

4

Multiple class inheritance is not possible in C# due to language restrictions.

Your Options are.

  1. Nesting the 2 classes into one container class
  2. Use 1 class containing both properties (Maybe split into 2 partial classes for code clarity
  3. Use inheritance chains using the most common properties on each parent class.
  4. Implement a "Multiple Inheritance" simulation using interfaces.

It really really depends on your use case. But make sure you are duplicating the least properties possible . For example in your case don't write all properties again on GroupRoleBaseModel but choose one to inherit.

    public class GroupBaseModel
{
    public int GroupId { get; set; }
    public int GroupName { get; set; }
}
public class RoleBaseModel
{
    public int RoleId { get; set; }
    public int RoleName { get; set; }
}
public class GroupMemberModel : GroupBaseModel
{
    public bool IsMember { get; set; }
}
public class GroupRoleBaseModel : GroupBaseModel
{
    public int RoleId { get; set; }
    public int RoleName { get; set; }
}

Each option has it's pros and cons , although i know nothing is exactly what you want.

There is nothing wrong containing all properties into a single class but why return all data? You can just write multiple functions to populate only the needed properties.

Community
  • 1
  • 1
Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47
  • Thank you for the response. I think I will go with the approach displayed above. I contemplated interfaces for the GroupBaseModel and RoleBaseModel and then decided against that approach in this situation. It would complicate the code and just make the code base larger because all the classes would then have to inherit and implement each property. And one of my goals is to refactor the code base into a smaller set where logical while maintaining sending only required data to the client. – cmartin Dec 08 '15 at 13:24
0

"is it possible to concatenate two existing Classes" Yes, you can use dynamic. There is no out of the box solution and I do not recommend to even try to do that.

should I just do like below and use one single class.
To answer that question ask your self. How many reasons to change GroupBaseModel class you will have in the future. If you have more then one you need to split. Keep going until all you model classes will have only one reason to change.

skalinkin
  • 1,024
  • 9
  • 19