Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM pattern for ASP.NET Webforms?

Tags:

asp.net

mvvm

I'm looking for an MVVM implementation for ASP.NET. How should I approach this ? Can you propose any design that solves this problem particularly for ASP.NET Webforms ?

Thanks.

like image 769
this. __curious_geek Avatar asked Oct 19 '09 09:10

this. __curious_geek


People also ask

Is asp net webforms dead?

In response to a comment questioning Web Forms support, Microsoft said: "Web Forms is NOT deprecated, the framework support is limited to critical updates, but all ASP.NET full . NET Framework projects are fully supported.

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 .

Does .NET core support webforms?

ASP.NET Web Forms isn't supported in ASP.NET Core (nor are ASP.NET Web Pages). Typically, the functionality of these pages must be rewritten when porting to ASP.NET Core. There are, however, some strategies you can apply before or during such migration to help reduce the overall effort required.

What is MVVM in web development?

Model–view–viewmodel (MVVM) is an architectural pattern in computer software that facilitates the separation of the development of the graphical user interface (GUI; the view)—be it via a markup language or GUI code—from the development of the business logic or back-end logic (the model) such that the view is not ...


3 Answers

Take a look at ASP.NET MVVM on CodePlex.

The framework author also talks about it a little in Presentation Model in Action.

like image 134
Bless Yahu Avatar answered Nov 15 '22 09:11

Bless Yahu


Try DotVVM.

It is not compatible with Web Forms, but it shares its principles (postbacks, server controls etc.), it solves their biggest issues (clean HTML, no viewstate, testable viewmodels) and it supports both full .NET Framework (through OWIN) and .NET Core.

You don't even need to write any javascript, it uses Knockout JS on the background, the framework solves everything concerning client-server communication for you.

It has also a nice Visual Studio integration and it is open source.

The views look like this:

<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>

And the viewmodel is pure C# class.

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

    public void Submit() 
    {
        ContactService.Submit(Name, Email);
    }
}
like image 39
Tomáš Herceg Avatar answered Nov 15 '22 08:11

Tomáš Herceg


A lot of MVC'ers are doing something akin to a view model in the sense that instead of returning domain objects to the controller, they have a flattened data structure (a view model) of all the data needed for that view regardless of how many domain objects worth of data it contains. In that regard a view model is very doable with MVC, and I'm sure it could be leveraged in webforms as well. However, there is no way that I know of to do the two way databinding / commanding / event aggregation that is associated with MVVM in WPF.

Although I don't know of any webform implimentations you could try some of the approaches described here:
Jimmy Bogard - How we do MVC

Here is a very interesting article on how to do MVP in winforms:
Castle Windsor's MVP with ASP.NET

Maybe you can create a hybrid of these two approaches using webforms.

like image 4
Daniel Auger Avatar answered Nov 15 '22 07:11

Daniel Auger