0

I'm using ViewModels in MVC. I'm find them incredibly klutzy and wonder if I'm doing something wrong. (Let's leave Automapper out of this for the sake of discussion.) I use viewmodels to send data to the client as well as receive form submissions. Some properties are sent to the browser for display only, others are sent & retrieved (eg. fields).

I typically have to implement the following to make a viewmodel work:

1) Create a view model class

2) Create a general function to initialize the view model & copy the properties from my entities & other sources

3) Write more code to write some values from the viewmodel back to the entities (or other destination) on submission

4) If there are server side validation errors, I need to write more code (particularly messy) to repopulate the read-only parts of the viewmodel (which were included in the submission), while taking care to not overwrite the user submitted data.

For a simple property (eg. "myViewModel.FirstName") this requires code to be written in no less than 4 places. That's not including the stuff in the Domain and Views. This pattern seems fragile to me - it's easy to break code eg. forget to implement a change in all locations. Definitely not DRY.

Am I missing the point or do all patterns using ViewModel have this kind of klutziness?

1
  • The point your missing is what you intentionally left out... Automapper.
    – mxmissile
    Dec 8, 2015 at 19:55

1 Answer 1

2

I'm not sure what you find klutzy? MVC is about separation of concerns. Keeping your business logic, data layer and GUI code separate. This in itself makes your code easier to manage, easier to test and far less confusing to debug.

And lets look at your four points.

Point 1. Firstly is creating a POCO for your view model really that much of an issue? I'd say not, since you can accomplish this in around 3 lines of code, given your example. And should your model need to change, would it not be more beneficial for it to be in its own view model class rather than in code directly in every action method in every controller where you may have used it?

Points 2 + 3. Here you speak about going from data layer to logic layer to view and back again. This is the whole point of MVC. Just because you have to possibly code three classes (model, service, repository) to handle this transaction does not make this cumbersome, it makes it clean. Just imagine if you dumped all this together on the controller action method. How would you handle re-use? How would you prevent repetition of code in other actions or controllers? Things would start to get very difficult.

Point 4. I don't really see this as a valid argument since you can just pass back the model submitted by the user without any need to update it or edit it. By using data annotations and ModelState for validation it is very simple to create a clean and testable unit of code. So I imagine that its not the fact your using a ViewModel but perhaps more to do with your implementation.

3
  • Thanks David. The 4 points I listed weren't arguments, rather listing all the places I have to add code in order to add a simple field. (in addition to the domain & views). Mar 21, 2014 at 22:44
  • {{since you can just pass back the model submitted by the user without any need to update it or edit it}} - This isn't the case! If your viewmodel has any "read only" fields - eg. values passed to the view for display purposes but not user driven fields, then these will be blank on the viewmodel when your form is submitted. (A hack is to make them all hidden fields so they're included in the submitted data.) What is a typical solution to this? Mar 21, 2014 at 22:45
  • It depends on why you are using readonly fields. Normally I never do because in practice I treat anything coming from the user as dangerous, only processing properties I know they should have updated. So if you are outputting a fullname for display, you have 2 options. Use a normal property & include hidden fields as you found which allows you to pass the model back. Alternatively re-use the method that obtains this data once youv'e processed the properties you expect to update. I have seen projects that put display data in sessions for reuse, but I think this is pretty messy as an approach. Mar 24, 2014 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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