Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What role does MVVM play in ASP.NET MVC 4 web applications?

While I'm reading the book "ASP.NET MVC 4" I'm wondering about MVVM. I started googling and cannot find any books about developing web applications using MVVM, so I must be missing a bit of information here.

From what I understand, MVVM is used in web applications on the client side via knockout.js and other frameworks. If however I was to develop a Windows Phone application, I could use MVVM directly without using MVC. Does that mean, the concept of MVVM / data binding just does not apply to client-server web applications?

like image 307
Krumelur Avatar asked May 29 '13 12:05

Krumelur


People also ask

What is MVVM in ASP NET MVC?

MVVM stands for Model-View-View Model. This pattern supports two-way data binding between view and View model. This enables automatic propagation of changes, within the state of view model to the View. Typically, the view model uses the observer pattern to notify changes in the view model to model.

What is the purpose of MVVM?

The Model-View-ViewModel (MVVM) pattern helps to cleanly separate the business and presentation logic of an application from its user interface (UI).

Can we use MVVM for Web application?

If you're looking to use MVVM in a web application that is NOT Silverlight, look into ASP.NET MVC. MVVM is also an option if you are using Silverlight. You can even mix the two, hosting your Silverlight app in a MVC website.

What is MVC and MVVM in C#?

MVC and MVVM are two initialisms used to describe the architectures of software projects. The initialisms stand for Model-View-Controller and Modal-View-ViewModel, respectively. I find it useful to define these parts as: Model – code that cares about how data is stored. View – code that cares about how data is ...


Video Answer


4 Answers

MVVM is the standard design pattern for WPF/Silverlight development, and should not be confused with MVC for ASP.Net development.

The two may sound similar and share some common parts, but they are two different design patterns.

From what I learned about knockout.js, it was designed to create "data bindings" similar to what you would use in WPF/Silverlight development, which is why the MVVM design pattern applies there.

To quote from another answer of mine regarding the differences between MVVM and MVC

In MVVM, your code classes (ViewModels) are your application, while your Views are just a pretty user-friendly interface that sits on top of the application code and allows users to interact with it. This means the ViewModels have a huge job, because they are your application, and are responsible for everything from application flow to business logic.

With MVC, your Views are your application, while your Controller handles application flow. Application logic is typically found in ViewModels, which are considered part of the M in MVC (sidenote: the M in MVC cannot be considered the same as the M in MVVM because MVC's M layer contains more functionality than MVVM's M layer). A user is given a screen (View), they interact with it then submit something to the Controller, and the Controller decides who does what with the data and returns a new View to the user.

like image 147
Rachel Avatar answered Oct 14 '22 12:10

Rachel


MVVM is really sort of a subpattern. There's not really any "MVVM" web app frameworks out there. They're all MVC and you pretty much just incorporate a view model if you want one.

With ASP.NET MVC, in particular, you just create a class, generally with a name in the form of [Model Name]ViewModel or [Model Name]VM. That class will have only the properties from your model that you'll need to work with and anything extra that doesn't make sense to put on your actual database-backed model, like SelectLists, etc.

In your action, you just pass an instance of this view model to your view instead of your model:

return View(viewModelInstance);

And, of course, make sure your view accepts that:

@model Namespace.To.MyViewModel

The only slightly complicated part is wiring the view model to the model (i.e., getting data to/from the view model/model. You can do this manually by explicitly mapping the properties, or you can use something like AutoMapper.

like image 26
Chris Pratt Avatar answered Oct 14 '22 13:10

Chris Pratt


Does that mean, the concept of MVVM / data binding just does not apply to client-server web applications?

No, you can apply the MVVM pattern to client-server web applications.

In fact Asp.Net MVC actually kind of does use this pattern - when the controller creates the view, it can pass in a "view-model". This view-model is often a POCO data object with all the data that a particular view needs, drawn from the model (database). The view uses this data to render the html page.

MVVM on wikipedia says it was introduced by Microsoft with WPF. Specifically, the view binds to properties on the view-model. The view-model then maps this to the database. By this definition then, Asp.Net does not exactly match that. Client-side frameworks like knockout.js and vue.js do support this kind of 2-way binding with view-model properties.

All these patterns are based on the fantastic MV* pattern. It was originally called the MVC design pattern. So this is the exact same pattern as Asp.Net MVC then? Actually, not quite. Controller means something completely different to start with (see MVC on wikipedia). The original MVC controller handles all user input directly not via the view. Second, the original MVC pattern was designed for a desktop app GUI and Asp.Net MVC adapted the pattern for use in a client-server web app. An ASP.Net controller is a collection of http end-points which the client-side html page can hit (eg form-post, page-navigation, ajax).

So there are a lot of M-something-V patterns and the general pattern is often called the MVC design pattern.

There's one more important wrinkle: client-side vs server-side. We've introduced rich client-side javascript frameworks and the fantastic MV* pattern is great here too. So now we could have something like: client-side View-Model-ServerHTTPEndPoints and server-side ServerHTTPEndPoints-ServerModel. The server-endpoints refers to Asp.Net controllers or the equivalent in whatever web framework or programming language you are using. From the server-side point of view, the entire client-side html is the view. The client-side model talks to the server ajax api (http endpoints) to sync data or trigger advanced actions. The ServerModel is normally a database. In knockout/vue, instead of client-side "Model", it would be ViewModel. If you use react/vue with redux/flux then the client-side would be View-ViewModel-Model-ServerHTTPEndPoints where the Model would be the redux/flux Stores. Also, often on the server-side, a service is introduced: ServerHTTPEndPoints-Service-Model. This way unit tests can hit the service directly rather than firing up the entire web server and making HTTP connections.

like image 28
Curtis Yallop Avatar answered Oct 14 '22 11:10

Curtis Yallop


MVC is a one-way data-binding system.

Fill your Model in Controller, then pass it to View.


MVVM is a two-way data-binding one.

Fill your Model, use it in View, when the View state's changes, your Model update automatically.(Vice-versa)

like image 27
ahmet Avatar answered Oct 14 '22 11:10

ahmet