I have a situation where I have a bunch of common properties in a view model but have certain cases where I need to extend it. I created a couple of derrived view models, which I pas into views that expect the derived view Model. (I realize I could use a Polymorphic Binder but I'm happy with just passing in the derived view model for now.
The code looks something like this:
public class CommmonViewModel
{
public int? CommonA{ get; set; }
public int? CommonB{ get; set; }
public int? CommonC{ get; set; }
}
public class SpecificViewModel1 : CommmonViewModel
{
public int? SpecificD{ get; set; }
}
public class SpecificViewModel2 : CommmonViewModel
{
public int? SpecificE{ get; set; }
}
The code reviewer wants me to have three distinct view models and duplicate the code like so:
public class SpecificViewModel1
{
public int? CommonA{ get; set; }
public int? CommonB{ get; set; }
public int? CommonC{ get; set; }
public int? SpecificD{ get; set; }
}
public class SpecificViewModel2
{
public int? CommonA{ get; set; }
public int? CommonB{ get; set; }
public int? CommonC{ get; set; }
public int? SpecificE{ get; set; }
}
Now bearing in mind that the real life view models are quite a bit more complex than what I have shown here; I hate that the solution should be to duplicate the code as it feels like a pretty bad violation of DRY to me.
I know there are generally reasons why we would be careful with inheritance and favor composition but in his case inheritance works fine, especially since this part of the system is likely to change in future.
In short, is he right? is this a case where I should be happy with duplicating code? What are the advantages\disadvantages of inheriting in this scenario?
Thanks for your thoughts