Currently I'm passing my domain objects to my views, and binding directly to them from POSTs. Everyone says this is bad, so I'm attempting to add in the ViewModel concept.
However, I can't find a way to do this very elegantly, and I'd like to know what other people's solutions are to not ending up with a very messy controller action.
the typical process for say some "add person" functionality looks like this:
- make a GET request for a view representing a blank Person viewmodel
- post back (in)valid data
- controller binds posted data onto a person viewmodel
- if binding fails, i need to do the same action as in (1) but with some data, not a blank object and errors
- if the binding suceeded, i need to map the properties from the VM onto a real model
- validate the model
- if validation passed: save the person, commit, map the users details to a display VM and return it in a view
- if validation failed, do the same actions as in (1) but with some data and errors
Doing all this in a controller action (ignoring the GET) certainly isnt SRP or DRY.
Im trying to think of a way of breaking this process up so that it does abide by SRP, is clean, modular and above all testable.
What are peoples solution to this?
I've been experimenting with custom controller-action-invokers to separate the concerns up into individual methods, smart modelbinders and just plain brute force but i havent yet come across a solution in happy with.
P.S. as it adds so much complexity, convince me why i even need to bother