§ MVC, HTTP, CQRS

09 Feb 2016 | Anton Kril Tweet

MVC (Model-View-Controller) is one of the most well-known and least-understood decomposition patterns.

The MVC approach was introduced in “fat” client applications. Model, View, and Controller in such applications were all loaded in memory and could interact immediately:

MVC on fat clients

The design had the following benefits:

Later web applications adopted the MVC approach. But some of the web frameworks implemented it differently:

Completely wrong implementation of MVC on server

There are a couple of problems with such implementation:

By design, HTTP protocol follows the CQRS principle: commands (POST, PUT, DELETE) must be separate from queries (GET). So if we follow HTTP, we will follow CQRS on the communication layer:

Wrong implementation of MVC for HTTP

Here data retrieval request (GET) is separated from data modification (POST), but Controller is still an entry point for both violating the CQRS principle on the application level. A lot of modern web frameworks are built this way.

One simple step is needed to build a proper client-server MVC: remove the Controller from the data retrieval request.

Correct implementation of MVC for HTTP

You can see that this diagram is almost identical to the initial correct fat-client MVC diagram:

Both MVC and HTTP follow the CQRS principle.

If we apply CQRS further, we will separate our business operations infrastructure from reporting:

CQRS applied to web application

One of the evolution branches of the CQRS principle is Event Sourcing architecture.