4 ms·
The issue of model code creeping into controllers is common with rails and rails-style frameworks precisely because they are not MVC frameworks. MVC was a patt
by papsosouid 14y ago
The issue of model code creeping into controllers is common with rails and rails-style frameworks precisely because they are not MVC frameworks. MVC was a pattern created for making GUI apps in smalltalk in the 70s/80s. It has gradually been warped and twisted since then, especially when people tried to cludge it onto web development. The resulting thing doesn't really resemble MVC at all, and certainly doesn't deliver the benefits MVC was created to deliver.
MVC is a triangle, rails style frameworks are a line. This is called MVA (Model-View-Adapter). The "adapter" being in between the view and the model who can only interact with the adapter and not each other. The second issue is that in adapting MVC to the web, many frameworks continued with the "one controller per view" notion, despite that being done because of the limitations imposed when creating GUI apps. There is no need for multiple controllers in a web app, all input comes in the same way no matter what view the user is seeing. In fact, the user doesn't even need to be seeing a view, there is nothing tying a view to what actions are possible, you can always submit any http request you want. Your "routes" are your controller, there is no need for a second level of controllers on top of that.
The goal of MVC was to expose model functionality to the user directly so they could understand and work with the domain model clearly and easily. Controller meant "controls the input and output" not "controls the users experience". The guy who created MVC considers naked objects style frameworks to be the closest thing to MVC for the web, not rails style MVA frameworks.
And there is something very weird about this statement:
>On top of that, it’s very common to see logic pushed down into the model
Of course that is common, that is what is supposed to happen. Models are not "stupid containers to pretend to kinda represent my DB sorta but not really". Models are the business logic. Models are the actual application. You are modelling the domain, not modelling the database. The model is the domain, it is all the business logic. The view is the way we present information to the user. The controller is the part that handles input and output. Very simple and effective system, but very poorly understood and virtually never used any more.
- karmajunkie 14y agoThe problem with "logic getting pushed down into the model" and the gist of the statement is that not all logic is domain logic. Reaching out and touching facebook for a status update, for example, is not domain logic and doesn't belong in a model. And while I agree that we should be modeling the domain, not the database, I think its clear from working on a lot of complex and legacy applications over the last several years rails developers model what the database allows, not the actual domain, thanks to the widespread acceptance of ActiveRecord as the only tool in that toolchest. I'm in 100% agreement WRT to MVC—Rails' idea of MVC was never really MVC as MVC was originally envisioned.
- papsosouid 14y agoI don't understand the distinction of "not domain logic". If your application requirements are "when user does X, update facebook" then that is clearly part of the domain and should absolutely be in the models.
- karmajunkie 14y agoOh, i disagree with that completely. A user having a status is domain logic. Pulling that status from an external dependency is decidedly not.
- endersshadow 14y agoHow is it not? What draws the line between domain logic and non-domain logic? It would seem to me that, by definition, anything your application does would fall into that application's domain, right? What other domain would it be in? Where would you put a request to a third party for supplemental data, if not in the models?
- karmajunkie 14y agoIf it were part of your domain, it'd be part of your application. Connection to facebook by definition is outside the scope of your domain. You can take a couple of different strategies to it, but in general the best way to handle that is to introduce a class whose responsibility is to connect to facebook and return a status (generally speaking, a Service) and something like a Command or Job that accesses the FacebookService and updates your user model. Part of the problem with speaking about domains here (aside from being nonspecific about something that is by its very nature quite specific) is that Rails has an extraordinariliy half-assed approach to domain oriented design (I don't want to use 'DDD' here just to avoid buzzwords). Developers have been told for years to just push it into the model because all the work belongs there, but that's BS. a LOT of the work has no business being there.
- papsosouid 14y ago