Web-development – Why decouple view and controller (web)

asp.netmvcruby-on-railsweb-development

Why can't we put them in the same page, like each action paired up with its view? Not using code island, but controller code at top then view code at bottom? What are the problems with this approach?

By MVC, I am referring to frameworks such as ASP.NET MVC and Ruby on Rails, and I am under the impression that the V and C are actually the UI layer.

Best Answer

The reason why they are generally decoupled is because you want your view to use a controller in order to get at your model. But the architecture should allow you to replace one view with another one without having to change business logic (i.e. object model or how those objects are retrieved).

By not tying your controller directly to the view, later on it would me much more easier to add other functionality like import/export which can use the controller/model directly without having to rely on any UI.

Another advantage of pushing as much code as possible out of the UI is because UIs are much harder to unit test than business layer behind them. By separating as much as you can out of the view itself, you can write much more unit tests to ensure your controller/model and application logic are correct.

Related Topic