Search This Blog

Friday, April 8, 2016

MVC and separation of concerns

There is a couple of "hype" words in the development world which somehow hurts my feeling of old monkey developer. One is "Test Driven Development" and the other is "MVC" framework.

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

MVC is an old (1970s) design pattern which could split the responsibilities of a process in 3 pieces. This design pattern evolved a lot since then, and I would say it's not till the last 3-5 years that we really started to hear it for web development.

In an ideal implementation the MVC pattern should really be split in 3 parts, where the model contains just the data (and is maybe responsible of storing / retrieving them from the DB), the controller which received the input of the user and applies the modification to the model and finally the view which should present the data from the model to the user.

MVC pattern as implemented by Microsoft somehow mix parts together, where the controller also serves the view, and the view has more than a little bit of logic inside.

Overall this can be a design choice and can make sense specially in web development, however I tend to find difficult to call those implementations true MVC.

MVC for me has a few points which makes it interesting:
- Separation of concerns (which means each piece of code works only on its main task not mix the tasks of another piece).
- Possibility to change the view (and therefore have multiple views) without having to change neither the controller neither the model. For example to offer multiple interfaces to the same data.

However with those standard implementations, you hardly achieve those 2 goals due to the mixing of the concerns.

Thanks to the new web techs like AJAX which let you now make a Single Page Application, you could actually separate the view on the client, while keeping the controller and the model on the server. This would then allows to have multiple different views sharing the same back-end, and even offering full 3rd party API.

I would personally push others to really think a bit more out of the box and see what their needs are before jumping on the latest framework. Even if you develop with .NET as I do, it doesn't mean you MUST follow all what Microsoft offers. You can certainly use some of the ideas or the tools offered but use them with what fits your project best.

No comments:

Post a Comment