Reputation: 4369
I have on my controller a Create and an Edit action.
I have for each action a viewmodel. Both viewmodels have approximately 15 properties. 10 are common for both models and the rest differs.
My question : Should I create some base model with the common properties (keeping DRY) or should I dont care here ?
Upvotes: 3
Views: 228
Reputation: 18877
This is almost certainly going to come down to the actual situation you are in. The most important question to ask yourself is: do these shared properties form some kind of base entity? If the answer is yes, you are probably safe having a base class. If not, I would stay away from it and just put the properties on the individual models.
If you are leaving out some properties in the create view model that become editable in the edit view, the shared properties probably do not make up some type of base class, and you should avoid the base view model approach. If the extra properties are just helper properties, like select lists, then you can safely have a base model that has the common properties.
Upvotes: 4
Reputation: 244
My view is that if I am creating or editing something, it is essentially the same thing. Really there shouldn't be any difference in the core data structure.
But assuming there are differences, I would use a base class to share the common properties as you would anywhere else.
Upvotes: 0
Reputation: 3954
Personally I would go the base class option so that your having to repeat data annoation attributes in multiple places.
Upvotes: 0
Reputation: 22334
You should definitely care. Writing good, clean code is good for the soul, besides we all know every time you write bad code, God kills a kitten. :( And we don't want that.
Move the common code / properties to a base class and then have two descendants, it's nice, clean and makes maintenance a lot easier.
Upvotes: 1