Live data from Hacker News

Are we doing MVC wrong?

withouttheloop.com

21–25 of 25 posts

Re: Are we doing MVC wrong?

#21
post #16

Unfortunately you've misunderstood MVC. None of that code belongs in the controller. Your 'typical' controller is anything but. All that code belongs in the model. Your controllers should have zero LINQ statement. You should do all of the validation in the model and the validation failures should returned in a List. This stuff's been in MVC for years: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mv... Wh…

What's the advantage? Is the model constantly pulling session state to validate against? Does this prevent caching of data on the models? Or is there 'supposed' to be some even higher level API manager that deals with the responses before they get to the model?

[deleted]

Re: Are we doing MVC wrong?

#22
post #16

Unfortunately you've misunderstood MVC. None of that code belongs in the controller. Your 'typical' controller is anything but. All that code belongs in the model. Your controllers should have zero LINQ statement. You should do all of the validation in the model and the validation failures should returned in a List. This stuff's been in MVC for years: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mv... Wh…

What's the advantage? Is the model constantly pulling session state to validate against? Does this prevent caching of data on the models? Or is there 'supposed' to be some even higher level API manager that deals with the responses before they get to the model?

It sounds like you're stuck using `Session` object which you should almost never touch in ASP.Net MVC.

HTTP is stateless. You should program accordingly and not rely on the old ASP.Net webforms ways, they were extremely broken and taught some developers some very bad habits (view state was a terrible, terrible idea. Session state should be used in extremely rare circumstances and it's much better to never use it).

That's one of the reasons MVC caught on so fast. It actually reflects what in reality is happening and is not the terribly leaky abstraction that Webforms was.

In the controller you get an updated object from your repository on each request. You update it with the data that you received in the request (and MVC can do this for you itself). You ask it to validate itself. You save if valid. You return your success view. You don't save if not valid, you return the validation errors.

Hence the controller is only a few lines.

That's how you're supposed to program in MVC because HTTP is stateless. And because SQL is much faster than you probably think it is. And that makes the code vastly simpler. And you can cache persistent things like the data for common dropdowns in memory using the actual `System.Runtime.Caching` functionality.

Re: Are we doing MVC wrong?

#23
post #4

It's not that the MVC is wrong, it is that the model is not being built right. Moving to the BorrowABook feature is making a model class. All too often in my experience I see code that has the model rolled into the controller. Controllers and UI are structurally supported better in frameworks. We get lazy and put model code into controllers. It is the way we make spaghetti code today.

We get lazy because most of the time ,without a proper Ioc container , writing code the right way is too much verbose.

Ioc containers changed the way I code since I did not have to care anymore about how complicated it was to instanciate objects. It helps write very clean code and makes OOP easier.

Re: Are we doing MVC wrong?

#24

Unfortunately you've misunderstood MVC. None of that code belongs in the controller. Your 'typical' controller is anything but. All that code belongs in the model. Your controllers should have zero LINQ statement. You should do all of the validation in the model and the validation failures should returned in a List. This stuff's been in MVC for years: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mv... Wh…

So, kindly watch the way you throw the word "you" around. I did not write the article, I just wanted to see what the community might have to say about it. I am coming to understand MVC decently well.

That said, your points are good, and I agree, based on what I have learned myself to this point.

:-)

Re: Are we doing MVC wrong?

#25
Several peopls have commented that this is really just CQRS.

The approach I described above has nothing to do with CQRS, in fact it directly contradicts CQRS. The design of features is intended to combine commands and queries. If the controller is restricted to commands and queries then the logic of combining those operations ends up in the controller which is exactly what we need to avoid.

Post reply on HN