1

I'm building websites in ASP.NET MVC and I quickly figured out that my models are getting harder to organise as they grow.

What I normally do is create one model to add and edit data. But in my add view I have 2 extra properties. So I'm re-using the same model with 2 extra properties but I don't use these my edit view.

Now I have a few questions regarding this.

How do you organise it?

  • Do you create one file class with multiple models in it or is it better to create a new file for each model?

  • Is it better to create a new model if you need a few more properties?

0

In theory, each view should have its own Viewmodel. A Viewmodel (especially with a web-based UI) is your method of representing the relevant state or input from the View. With this in mind, there would be an edit Viewmodel and a create Viewmodel.

However, in practice, edit and create are nearly identical actions as far as the View is concerned. There's likely going to be very little difference between the screens (in this case, only two fields?), and they both generally represent the same object. I personally use the same Viewmodel for both views, despite a small bit of functional difference. In general, I'd say the reason the fields aren't used is relevant to whether or not you should reuse the model. Reusing a Viewmodel to represent the same general data structure in two separate places is probably fine, but reusing a class just because it happens to have the right properties with the right names is breaking the pattern.

Regarding files, especially while using an IDE like Visual Studio that gives you a nice project explorer, it's best to keep one class per file. When the code is compiled, it makes absolutely no difference, so barring file system issues, this is a matter of usability and ease of programmer interaction. Also considering source control, where you might only be able to commit file-by-file, it's best not to lump more than one class together. When you change a class, you only one that one class to be considered "modified", but any other classes in the file would be included.

  • -1 You can have the same view model utilised in different views. For example, you might have a SelectRecipientViewModel - view model for selecting a recipient. Now you might have two ways of selecting recipients, so you'd have two views making a use of the same view model. It's common for clients asking for the same functionality but presented in a slightly different way. – CodeART Apr 24 '14 at 14:29
  • 1
    @CodeART All I said was that in theory, each View would have its own associated ViewModel. I agree that blind adherence to "patterns" and "best practices" is foolish, and that there are often practical examples where simplicity is more important. In fact, I explicitly told him that it's okay to reuse his ViewModel, and that "Reusing a Viewmodel to represent the same general data structure in two separate places is probably fine". So while your down vote reason isn't a bad reason to downvote, it's not an accurate reflection of my answer. – TheRubberDuck Apr 24 '14 at 15:01
0

I don't recommend putting multiple classes in a single file. One file, one class, one responsibility.

With regards to the models, I would encourage people to create a new model or create an abstraction with common properties and then inherit from that abstraction. So you'd have an abstract class and two classes inheriting from it.

  • Haven't thought about using abstract classes, good idea. Thanks! – Jamie Apr 24 '14 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.