▲ | sunir 4 days ago | |||||||||||||||||||||||||||||||||||||||||||
The model is the source of truth. For almost all apps, it should always be valid. (For those apps that isn’t true such as massively distributed systems, it should present a projection of what can be considered valid to your locality and handle delta internally.) There has to be a boundary that controls changes to the model. The confusion with MVC is where is the best place for this boundary. Well more than one place as it turns out because there are at least two models of reality trying to converge. The model itself and the view of the model (and the user’s mind). The view’s job is to present a projection of the model and then collect change events to the model. Thus could be a UX or an API. Other events can also change the model like say sensor data. The controller decides what view to show and retrieves model data to project and translates change events coming from the external world (views or events) into changes the model should interpret. This includes gatekeeping such as auth, and error handling. That’s a lot for one class so it can get confusing very quickly. Why localize it in one place? So viewtrollers come around where the controller is in the view class but in the onhandle methods. This also makes sense since each view has a mini controller to handle all the jiggling bits. This works well when there are no orthogonal injections like auth or eventing. When those are added it makes sense ins viewtroller to extend the model with controller functionality to for eg control authorization or have a thin event receiver to fsm in the model. This all works but three years later it’s hard to figure out when I read the code again. So I have learnt to treat the model as pure data as much as possible and the view as much about rendering as possible. Views can have little controllers for handling the jiggling. What the controller cares about is when a change to the system needs to happen. Then I can put the system control fsm in one place. I can put all event handling in the same fsm to avoid race conditions. The goal is to make it easier to reason about. What I don’t want are multiple threads of fsms in conflict with each other. | ||||||||||||||||||||||||||||||||||||||||||||
▲ | mpweiher 4 days ago | parent [-] | |||||||||||||||||||||||||||||||||||||||||||
> The model itself and the view of the model (and the user’s mind). Trygve was very explicit about the model being a model of how the user thinks about the problem: There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand. https://web.archive.org/web/20090424042645/http://heim.ifi.u... Second paragraph. > There has to be a boundary that controls changes to the model. Yes. It's called the API of the model. The model makes available API for all semantically valid operations. As I wrote elsewhere, I usually have a facade that acts as the top-level API for the model. The views can call this API. So can other entities. > The controller ... decides what view to show possibly. But views can handle their own subview. > The controller ... and retrieves model data to project Nope. Not the job of the controller. > The controller ... and translates change events coming from the external world (views or events) into changes the model should interpret. Nope, it doesn't. The views typically do that as well. Controllers may be get involved if there is a complex sequence of steps that doesn't really naturally fit into a view. > That’s a lot for one class so it can get confusing very quickly. Yeah, if you put all sorts of stuff in the controller that doesn't belong there. > Why localize it in one place? Indeed. Don't incorrectly localize all these things in one place. MVC, for example, tells you not to do this. > This all works but three years later it’s hard to figure out when I read the code again. Yes. MVC is much better than that thing you came up with. | ||||||||||||||||||||||||||||||||||||||||||||
|