DRY vs Safety and maintainability with MVC models and view

I like to strive for SUCHA, and obviously this is not always possible. However, I have to scratch my head over a concept that seems pretty common in MVC, like the β€œView Model”.

The viewing model is intended only to transmit the minimum amount of information to the presentation, both for security and for maintenance and testing. I understand. It makes sense.

However, from the point of view of DRY, the view model simply duplicates the data that you already have. The view model can be temporary and can only be used as a DTO, but you basically support two different versions of the same model, which apparently violate the DRY principle.

Can view models violate DRY? Are they a necessary evil? Do they do more good than bad?

+6
asp.net-mvc dry viewmodel
source share
2 answers

This has been repeated over and over again. Not only is this a pretty substantial fraud, but the answer is subjective and reasoned. ViewModels is a response to DDD and the concept of nonviolence of perseverance.

Saying that using ViewModels is wrong, ignoring the fact that Django and Rails and most PHP ORM / MVC frameworks don't care about these concepts at all. Do you want someone to tell you that all these other languages ​​and frameworks β€œdo it wrong?”.

Regardless of whether you want to use ViewModels, it is 100% dependent on what styles of architecture you are planning and what are the goals of the application.

Does this look like a drag request for GridViews in a WebForm application? Depends on many things.

There is also a misconception about DRY that you have. Do WCF Proxy classes violate DRY? Does the ViewModel have logic? The main purpose of DRY is to not have duplicated logic with a meaningful purpose. Did a couple of DTOs that share object templates break that?

The DDD limited context principle would also serve as a good read. If the ShoppingCart should work differently on the store website or on the e-commerce website, does that mean you have to share these types? What happens when the only common functionality is the total price (price + tax + shipping)? Do you create a base class just for this, thereby increasing the connection? What are the time / cost / service tradeoffs for 100% DRY for a simple method like GetTotal (). Does DRY break when it makes sense to actually reduce the complexity and overall cost of maintaining your code base?

I'm sorry that I answer so many questions, but I hope now you can see the nuances and subtleties of the question you asked.;)

+4
source share

It can also be noted that the use of viewing models is not a violation of the principle of single responsibility - your organization should not be contaminated with user interface problems.

I also believe that the real value of view models does not necessarily become apparent in version 1.0 of your application. You will thank yourself when working on version 2.0, when you completely change your mind about how your back-end works, but you do not need to transfer these changes to the presentation level.

+2
source share

All Articles