Web Applications MVC – Is it Possible to Keep Business Logic Out of the View?

crudmvcviewweb-applications

I've developed for several web application projects for the last 3 years, both personal and at work, and I can't seem to figure out whether it's possible for at least some business logic not ending up in the view layer of the application.

In most cases there will be problems like "If the user has selected option x then the application must enable him to supply info for y, if not then s/he should supply info z".
Or do some AJAX operation which should apply some changes to the model but NOT commit them until the user has explicitly requested so. These are some of the simplest problems I've encountered and I can't figure out how it's possible to avoid complex logic in the view.

Most of the books I've read describing MVC usually showcase some very trivial examples, like CRUD operations that just update data on the server and display them, but CRUD is not the case on most rich applications.

Is it possible to achieve having a view with no business logic at all?

Best Answer

Is it possible to achieve having a view with no business logic at all?

I find this a deceptively hard question to answer. (Thought-provoking question!)

Theoretically, yes, depending on what we define as business logic. In practice, strict separation becomes a lot harder, and maybe even undesirable.

Separation of concerns is a great way to think about building software: it provides you with ideas about where to place code, and it gives maintainers a good idea about where to look for the code. I'll argue that it's basically impossible for humans to build working software without separation of concerns. We need this.

But, as with all things, there are trade-offs. The best conceptual location may not be the best location for other reasons. Maybe there's too much load on your web server, so you add some javascript to your web pages to catch easy input errors before they hit your server; now you have some business logic in your view.

The view itself, on its own, has no value without the business logic. And to be effective in use and display, implicitly or explicitly, the view will have some knowledge of the business processes going on behind it. We can limit that amount of knowledge, and we can cordon off parts of it, but practical considerations will often force us to 'break' separation of concerns.

Related Topic