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?
Are we doing MVC wrong?
21–25 of 25 posts
Re: Are we doing MVC wrong?
#22Unfortunately 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?
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?
#23It'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.
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?
#24Unfortunately 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…
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?
#25The 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.