A short introduction to the MVVM-C design pattern
tech.trivago.com
tech.trivago.com
From a cursory read I like the architecture improvements they propose to 'fix' this situations. That said, I can't escape the feeling that for the majority of applications, this is way over-designed/over-engineered, and putting the cart before the horse. Your application should not be written to better suit the MVC (or MVVM-C) pattern, it should be the other way around. Often a merged VC class gets the job done just fine and keeps things simple and concise.
In my experience, my own applications end up with a single class that's both the view and the controller, for the simple fact that there is no reason to decouple them. I'm usually not interested in having multiple user-interfaces on top of the same model, so there is no incentive to re-use the controller. Note that IMO there is a clear distinction between decoupling the model from the view and/or controller (which is almost always desirable to separate data and presentation), and decoupling the view from the controller (which have a lot of overlapping resposibilities)
To be fair, I haven't written any applications that hat different UI for e.g. iPhone and iPad, and I haven't written any applications that also have a desktop counterpart. But even in those situations, I'm wondering how much there is to be gained from re-using controllers for different user interfaces...
a) Writing tests for the combined class is incredibly hard. Since you are relying on the UI framework to call your code you have very little control over when something happens (and what side effects it will cause).
b) Changing your code becomes a lot more time consuming. In an agile environment you want to be able to quickly change behavior and looks (sometimes even on the fly) which gets harder the stronger your coupling is.
The goal of MVVM is essentially to have extremely 'dumb' views that do nothing but relay data between model/logic and the UI.
>> a) Writing tests for the combined class is incredibly hard. Since you are relying on the UI framework to call your code you have very little control over when something happens (and what side effects it will cause).*
While this is definitely true, I fail to see how separating controllers and views helps to improve the reliability of your user interface code. Yes, it makes it easier to test individual parts of your UI code independently and without side-effects, but eventually all of it will end up on a user's device (that's the assumption, at least ;-), which means it has to 'rely on the UI framework' and 'has very little control over when things happen' anyway. If you are concerned about testing isolated parts of the UI code without side effects, it's probably better to factor them out into functions and test those directly.
>> b) Changing your code becomes a lot more time consuming. In an agile environment you want to be able to quickly change behavior and looks (sometimes even on the fly) which gets harder the stronger your coupling is.
I totally agree with this, but the question remains how many application developers face this scenario, and how the flexibility/complexity tradeoff scales for moderately complex user interfaces.
b) I don't think having the view and controller coupled makes this a big deal in a lot of cases[0] as long as the controller doesn't have behavior creep.
[0] There are exceptions to everything.
Basically, the View is presentation, the Controller is interaction and the Model is logic and data.
No, but if M-VC works, then it's fine. MS used Document/View architecture where the View was decoupled from the Document (model) in MFC and that was very popular and successful for years. "The humble dialog", IIRC, describes something similar.
I don't think we developers should fear the wrath of the the MVC police if we choose not to decouple everything.
So who in the world are these MVC police? Does anyone in the world actually do MVC correctly? Has anyone ever?
In a typical Rails application (or one of the myriad frameworks inspired by rails), the code in 'controllers/' is actually view code that happens to be written in Ruby rather than in the template language of choice.
The controller is actually in config/routes.rb.
But as you mentioned, this is actually a good thing for most Rails apps. If your app doesn't need a controller, putting a controller in is just over-engineering.
Here's the truth : your app probably has more than three major components, and the GUI isn't the most important aspect of it. Build your app as if it was to be accessed through a command line or a repl, then add a GUI on top in the end.
You'll have reusable, business centric components, and your controllers ( or whatever you call it) won't be bloated.
In theory, you set your service layer (just a class assembly) up so that it can handle requests from multiple UI layers (a web MVC layer, a phone app, or even one case, a command line prompt).
MVVM-C seems overly complicated.
Model is basically a collection of types, and services are where the business happen and shit gets done. And only in the end should you care about presenting all that stuff in a good looking way.
But even this is a bit of a simplification once you get into low-level architectural decisions : is your app real time, does it handle data synchronization with a backend, can it work online and offline , does it need to make certain operations atomically ? Do you have n publisher - n subscribers types of communication between your components ? etc etc.
Those are the true questions you need to ask yourself first, and not think in terms of three or four big boxes where you have to put all your junk trying to find descent names.
Well, if I restrict my scope to the rich web client ("SPA") style work I have mostly been doing for a bit over a year, it is. In fairness, that's because there is a back end with all of the "real" business logic in it, and I just put up what the view-model tells me to, wiring buttons and some other events to REST calls to phone home and find out what to do next.
Anyway, you are usually right, but the project type matters: there might be just enough UI to enter some selection bounds and then kick off a batch process, or it might be a small game that is little more than a "busy box" consisting almost entirely of pretty artwork and responses. Plan & budget the design effort for the UI accordingly.
The model is logic and data and has absolutely nothing to do with presentation.
But, I will concede there are 1001 ways to interpret these patterns (MV*).
So having structured, known ways to decouple views (or every other component for that matter) is valuable.
Better than polluting prepareForSegue, and it totally decouples your UIViewControllers from each other. The coupling is in the segue, which is where it should be.