Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM for Web Development

I've been reading up on MVVM and so far have found it very interesting. Most of the examples I've found, however, are for Windows apps, as opposed to Web apps. I've also seen a lot of mention of MVVM used with Silverlight, and I know Silverlight can be used for either Web or Windows apps.

So my question is - is MVVM a valid pattern for Web-based apps? If it is, does the UI have to be Silverlight? I'm in the process of deciding which technologies to use for a new mid-size website we need to design, and Silverlight may be a hard sell to the powers-that-be, though what we use behind the scenes doesn't matter so much.

Any info anyone can supply on using MVVM in a web environment would be appreciated. Example code would be great as well.

like image 901
BDW Avatar asked Aug 06 '10 13:08

BDW


People also ask

Is MVVM good for web development?

The pattern is very good; it makes it very easy to construct applications that are simple yet powerful, and that are easy to test and maintain. 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.

Is MVVM better than MVC?

MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing.

Is MVVM an overkill?

MVVM creator John Gossman points out that implementing MVVM is "overkill" for simple UI operations, and that for larger applications, generalizing the ViewModel becomes more difficult. There is considerable memory consumption with data binding in very large applications.

Is ASP NET MVC or MVVM?

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 .


2 Answers

DotVVM is an open source ASP.NET-based MVVM framework based on Knockout JS. It's simple to use and you don't have to write tons of Javascript code. For most scenarios, you need just C# and HTML with CSS.

The view looks like this - it is a HTML extended with server controls and data-bindings:

<div class="form-control">
    <dot:TextBox Text="{value: Name}" />
</div>
<div class="form-control">
    <dot:TextBox Text="{value: Email}" />
</div>
<div class="button-bar">
    <dot:Button Text="Submit" Click="{command: Submit()}" />
</div>

The viewmodel is a C# class which looks like this:

public class ContactFormViewModel 
{
    public string Name { get; set; }
    public string Email { get; set; }

    public void Submit() 
    {
        ContactService.Submit(Name, Email);
    }
}

There is also Visual Studio Extension which adds IntelliSense and project templates.

The framework handles validation, localization, SPAs and other frequently used features. It supports both .NET Framework and .NET Core.

like image 159
Tomáš Herceg Avatar answered Sep 19 '22 05:09

Tomáš Herceg


Of course MVVM is valid "web" pattern but currently it has very limited uses.

Main difference between MVC and MVVM is in updating your application data. For currrent web applications MVC is preferred because web is mostly one-way communication and all user input is encapsulated with forms.

MVVM gets usefull when creating really interactive applications with rich UI.

So to make it simple. If you are bulding web solution with ASP.NET (or any other server-side oriented tehnique) then use MVC. If you are making rich UI application use MVVM and if you don't like Silverlight try KnockoutJS for Javascript solution.

like image 35
Bizniztime Avatar answered Sep 20 '22 05:09

Bizniztime