No announcement yet.

MVC Web Apps & Controllers, Views & Models

  • Filter
  • Time
  • Show
Clear All
new posts

  • MVC Web Apps & Controllers, Views & Models

    Hey folks,

    Long time!
    I've come to get your take on a disagreement of practices surrounding the use of models vs viewmodels when passing them into razor views in MVC Wep applications.

    In our project at the moment, and has been in all our other projects, a practice of retrieving an object of type (say.. Customer, with only basic properties), and passing that object directly to the view to display as either a read only or as a form for editing and passing back through a serialized ajax request - the controllers then run validation over the object before passing it to a service interface.

    It's also worth noting that the way in which we have written our applications, means that a user is unable to execute a controller action without the required permissions first, our base controller simply wont pass the call down if the custom attributes of the action aren't met, and all controllers must inherit from the base controller.

    As a team, we have been using this style for a long time, but we have recently hired a new fella whose understanding of coding practices is leading him into writing excessive amounts of ViewModels for everything and using mappers everywhere, which is not inline with how the rest of the team (including myself) codes and is making peer review harder.

    I simply can't get my head around why our new dev believes so strongly that we have to use ViewModels on everything when nothing we pass to the view is anything more than a "dumb" object, with no access to services - and has to pass through quite a robust mechanism before being handled.

  • #2
    The tone of the post seems to suggest that you would like the new dev to adhere to the current code structure to keep a consistency - and I would tend to agree with you. There are some technical advantages to using ViewModels as I understand it, but if your objects only contain the required data for the view that it is being passed to.

    Customer data is an interesting one as it boils down to how that data is being held, I think the simplest example would be names and how they are displayed in the view. If you have first & last in the object, and display most of the time "FirstName Lastname", then the object should have a concat of first last, FullName or DisplayName. The classic view conundrum.

    It depends on how many times it's used in the view and your model that constructs your object really - but in old git grumpy mode, do it the way of the rest of the team or don't let the door hit you on the way out

    For fun, perhaps run some page compilation time tests, should be simply enough with say a dozen fields in a customer record.
    Please see thread, here for how post reports are dealt with.
    Forum Guidelines here