📜 ⬆️ ⬇️

The rule of separation - do not try to abuse!

The commentary on the article on the “rules of separation” has turned into a small separate article, which again proves that the development of any application is more complicated than Hello World requires sound decisions and a creative approach, sometimes refuting these same decisions.



In his article , Rauf cites MVC as a very valid design pattern using a division of responsibility. I would not agree with this, but the first good template division that comes to my mind is the Observer.
')
In my opinion, MVC, like MVP (Model View Presenter) templates are powerful, but quite blurry. This means that you can never draw a clear line between M, V and C / P - that is, without being tied to a specific project, you can never tell where the controller ends and the presentation begins, or which code we will write in the model, and which in the controller.

As for the web / client server / server-side + JS + AJAX, I would also not really agree that it is necessary to very strictly divide logic and presentation.

The thing is:

1. Again, the edges are blurred - the simplest example is the same project selling kiosk computers at Svyaznoy. Here, of course, the specificity of the application plays a role (and I'm leading to that)! Have you ever seen that the basket was stored and processed on the client side (in the browser), and remembered on the server side, only at the end of the path - after pressing the "Order" button? We have that way.

2. Again about blurry edges and the same offline web application (yes, yes, I was NOT sealed). The application has a lot of client-side code, and JavaScript is a specific language (callbacks, closures, runtime extensions). In some places I have to use mega-hacks, which are not possible to put in order personally. How would you, for example, deal with deferred rendering in Chrome / Chromium ??? About when and how Chromium renders lazily / deferred I plan a separate article.

So - the data is pulled using AJAX from the server - they would be received in JSON, and rendered already on the client ... Not there it was - the specificity of the application dictates to give not JSON, but HTML - otherwise the brakes and again abut in deferred rendering.

3. DOM-Model, CSS and JavaScript are closely related. I would like to do some things with clean markup, for example, dies with rounded frames and shadows - a picture, but it turns out that when there are a lot of them, the brakes start and we have to use CSS3 (for us, it’s easier - we only have Chromium). For animation, I would like to use CSS3-Animation, courtesy of the WebKit engine, but practice shows that it slows down demanding of resources.

Total for DOM + CSS + JavaScript + AJAX:

The implementation is strongly influenced by technology and channel width. As a result, you often have to do something differently, not as beautiful, but as it will work well. Well, it turns out that govnokod and crutches they rule, no matter how much it surprised me ...

But let's go back to MVC ... I would like to say that they should not be abused at all. Take for example the simplest govnosayt-business card, which I once produced a dozen two.

As usual on every page we have a sidebar with news. This is where the dilemma arises and the edge between the Controller and the View becomes soft ...

To the point! It would be nice to make a selection of a collection of the five latest news in the controller, but I personally do not like it for one simple reason - it will have to be done in each (almost) controller.

Experienced OOPeshnik :) will say that it is necessary to allocate the parent CommonController class with the fetchLastNews method, which will be launched each time immediately before the execution of the main * Action. Not here it was !!! Imagine that this is not just the five latest news, but some rather heavy sample (and caching doesn’t roll), moreover, this news is used only on 65% of the pages. Inherit controllers from different base classes ??? MESS!!! (IMHO)

But there is an exquisite crutch if you have a template with simple execution of PHP code (how are things in other languages, I’m still weakly present) - I usually use Macro from limb-project.com or native-php (though Smarty is suitable, but I don’t like the execution of the code in it {PHP} GEST {/ PHP}) ... So, if you have a good PULL templating engine, it’s better to break the V | C line in this place and make a choice of news in a sub-template that can be at any time pull in any template / Layout statement include.

I already anticipate the tomatoes flying into me from MVC privezhentsy (and I also love him), but the article is intended not to disprove its power, but only to emphasize the vagueness of the components, in order to warn developers who see silver bullet in MVC.

There is no silver bullet - there is a golden mean, but you (and me) still have to find it.

Source: https://habr.com/ru/post/103297/


All Articles