Live data from Hacker News

Are we doing MVC wrong?

withouttheloop.com

11–20 of 25 posts

Re: Are we doing MVC wrong?

#11

My issue here is that BorrowABook is a verb. This isn't a model class, and in my understanding of OOP, doesn't really deserve a class by itself at all. You could instead put the borrow method in a class called Book that represented the model of your application. Then you call borrow on the book, the book does some logic, and returns a status code to the controller. Or since it's a bit silly to ask a book to borrow it…

BorrowABook is a Command. What the article is, albeit unknowingly, discussing is roughly the CQRS pattern. CQRS can rarely ever be considered to be a bad practice. As good developers mature, they come to realise that generally all the good patterns in the world worth pursuing have traits of immutability, messaging and functional programming (FP), even in OOP-land. Most good OOP designs are merely trying to emulate what FP does.

Re: Are we doing MVC wrong?

#12

My issue here is that BorrowABook is a verb. This isn't a model class, and in my understanding of OOP, doesn't really deserve a class by itself at all. You could instead put the borrow method in a class called Book that represented the model of your application. Then you call borrow on the book, the book does some logic, and returns a status code to the controller. Or since it's a bit silly to ask a book to borrow it…

I agree with this...perhaps it is more akin to a factory method, where parameters are passed in and a book, with its borrow status affected, is returned?

Re: Are we doing MVC wrong?

#13
post #5

The controller is always the ugly part, because it's the piece that's responsible for interfacing between transaction-specific logic and your general backend interface. The article is right that it's easy to put too much business logic in the controller, but fails to recognize that "MVC" isn't supposed to be your whole application - just the UI side. The functionality of this BorrowABook class doesn't belong in the U…

Controller is an adapter or anti-corruption layer designed to couple together two separate bounded contexts. It's feasible and likely there would be two variants of BorrowABook, one in the UI model and one in the service/domain model. They may be subtly different in what fields they hold.

Re: Are we doing MVC wrong?

#15
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...

When people talk about 'light' or 'skinny' controllers, this is what they're talking about.

So we're not doing MVC wrong, you've just developed bad habits from bad microsoft examples, work with some experienced Devs so they can teach you how to do it correctly.

Re: Are we doing MVC wrong?

#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?

Re: Are we doing MVC wrong?

#18
post #5

The controller is always the ugly part, because it's the piece that's responsible for interfacing between transaction-specific logic and your general backend interface. The article is right that it's easy to put too much business logic in the controller, but fails to recognize that "MVC" isn't supposed to be your whole application - just the UI side. The functionality of this BorrowABook class doesn't belong in the U…

MVC is something I've never really been able to wrap my head around. I wonder if this is why... All of the guides/articles that I've read on the subject have acted as if MVC was how the entire application was handled. Could you recommend any references for back-end patterns?

I like Patterns of Enterprise Application Architecture, which includes some domain logic patterns. A common one is SOA, by dividing the backend into services that your UI consumes.

Re: Are we doing MVC wrong?

#19
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?

Your controller should be passing the session state to the model (passing the items needed from session state for the model to manipulate data). Models should have no concept of a session. They should be almost entirely self-contained.

Controllers should do only basic validation (length, format, etc.)

Re: Are we doing MVC wrong?

#20
> Controllers are the UI

I agree.

> What is the responsibility of the controller?

Controllers are the UI ? but the UI of what ? HTTP. controllers should only deal with HTTP (request,respone,cookies,session) stuff , nothing else. The rest should be encapsulated in services, which communicates through models.

The truth is , it is not MVC , MVC is a poor label for that kind of architecture since MVC means something very specific in software engineering.

Post reply on HN