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.
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.
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 .
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.
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 ...
Take a look at ASP.NET MVVM on CodePlex.
The framework author also talks about it a little in Presentation Model in Action.
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With