Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use client-side MVC/MVVM patterns with MVC server-side pattern

Considering the most popular MVC/MVVM client-side patterns (like Knockout.js, Angular.js, Ember.js, and others), I have one great doubt:

Also considering the modeling redundance in both sides, what is the advantages and disvantages to use those client-side patterns with MVC server-side patterns?

like image 622
Gustavo Gondim Avatar asked Aug 01 '12 17:08

Gustavo Gondim


People also ask

Is MVC client side or server side?

ASP.Net MVC is a server side framework.

Can you use MVC with MVVM?

The key to MVVM in MVC is moving logic out of the MVC controller and into a view model class that contains properties that you can bind to the user interface. The controller should only call a method in your view model, and periodically set a property or two prior to calling that method.

What is the difference between MVC and client server architecture?

Client server is a network architecture. MVC is a a pattern in software architecture that doesn't even require to run on a client server architecture (you can use it to write a purely single user desktop app where everything runs on a single local machine).

Does MVC use server side rendering?

ASP.NET MVC (Server-side)With MVC, all the grunt work is done on the server and the browser is given straightforward HTML to render. The user attempts to navigate to a URL in the browser. The browser pings this requests over to the server. ASP.NET MVC takes over, forwards the request to the relevant controller.


2 Answers

I struggled with how to answer this question... hopefully this helps, even if it is in a round-about way.

While some of the pros/cons have already been stated, I think the best rundown is in this answer.

For me, the biggest advantage to using client-side logic is the rich UI aspect.

But the key part of your question seems to be "model redundancy" (I'd call it duplicated logic, or at least having potential for duplicated logic). In my opinion, that is a problem which may exist independently of the pros/cons in the previous link.

So first of all, I think that the decision of whether or not to use a client-side framework should be made based on the well-documented pros and cons. Once that decision is made, the associated problems can be solved.

Lets assume you are using some sort of server-side framework/platform, as well as a client-side framework to provide a little bit of UI interactivity. Now there is a problem with where to put the model logic: on the client, server, or both.

One way to solve the problem is to define your model logic in only the client or the server. Then you have no code duplication, but it affects some of the higher-level pros/cons.

For example, if your model logic is 100% server-side, you lose some of the interactive part of the UI. Or, you are constantly throwing the model to/from the server, which will have a few cons.

If your model logic is 100% client-side, you could suffer performance problems, depending on the size of your view / model. This is one of the reasons Twitter is moving to a server-side processing model.

Then there is "both"... having model logic exist in both the client and the server. I think this is the best solution, as long as no logic is duplicated.

For example, on a shopping cart page, you may recalculate the cost of an order based on the price of a product, and a user-editable quantity box. I think this logic should only exist on the client. Other model properties that do not change once loaded are probably fine hosted on the server.

There's a lot of gray area here... I struggle with putting all the eggs in one basket. For example, choosing a client-side framework, creating a lot of client-side logic, and then [hypothetically] running into problems with performance, browser support, or something like that. Now you may want to tweak a page or two for performance (like move it server-side, a la Twitter). But I think being smart about how you structure your code will help mitigate that issue. If your code is maintainable and clean, moving logic from client to server won't be difficult.

like image 193
Jason Capriotti Avatar answered Nov 03 '22 20:11

Jason Capriotti


The advantage is that the client side patterns are applicable at the client where the server has no direct reach. If you're building a rich, interactive HTML UI then use client side MVVM. Server side MVC may still be relevant in that case for delivering appropriate content to the client. For example, the ASP.NET WebAPI is a framework for creating HTTP APIs which has a similar controller architecture to the ASP.NET MVC framework. The API implemented with this framework may be called by client side code resulting in MVC on the server side and MVVM on the client side. Normally, when using MVC server side and MVVM client side, the responsibilities of the respective sides are very different and thus there is no redundancy.

like image 5
eulerfx Avatar answered Nov 03 '22 19:11

eulerfx