I am writing a simple ASP.NET Web Forms application. I want to improve the code by implementing any design pattern with which abstraction is achieved and increases manageability and understandability.

Which pattern is recommended? Please also provide links to sample applications.

  • is it an enterprise level application? – Yusubov Jun 30 '12 at 15:19

You might consider using the Model-View-View model MVVM pattern. It is a derivative of MVC, but is designed / intended to be used in exactly the situation you describe. There is a wealth of information on the MSDN site if you don't feel like googling it.

MVVM facilitates a clear separation of the development of the graphical user interface 
(either as markup language or GUI code) from the development of the business logic or back 
end logic known as the model (also known as the data model to distinguish it from the view 
model). The view model of MVVM is a value converter[4] meaning that the view model is 
responsible for exposing the data objects from the model in such a way that those objects 
are easily managed and consumed. In this respect, the view model is more model than view, 
and handles most if not all of the view’s display logic (though the demarcation between 
what functions are handled by which layer is a subject of ongoing discussion[5] and 
exploration). The view model may also implement a mediator pattern organising access to the 
backend logic around the set of use cases supported by the view.
  • I read about ASP.NET Model View Presenter framework at codeplex. Is MVVM available for Web Forms through any implementation? – RPK Jun 30 '12 at 11:54
  • MVP and MVVM are essentially the same. The Wikipedia article explains this too: MVP is the first derivative in this chain from MVC. Martin Fowler is credited with publishing the pattern and it deals with some of the challenges that a Controller can have in an event driven realm. MVVM was a refinement from there to take advantage of data binding between the view and view-model. – user53019 Jun 30 '12 at 12:03
  • Most of the applications I saw use Repository Pattern alone. What is reason? – RPK Jun 30 '12 at 12:07
  • 1
    The repository pattern is a little faster to implement and not as heavy as MVP / MVVM. The View is hitting the model directly instead of having an intermediary layer. Compared to MVP / MVVM it will be faster to implement with less code to write. OTOH, it will be more brittle because it is tightly coupled to the model / data access layer. Repository Pattern would possibly work for the circumstances you described, but if your project grows, you'll wish you had the intermediary Presenter / VM layer. – user53019 Jun 30 '12 at 12:54

With ASP.NET Web Form, you typically use the Model View Presenter pattern.

GlenH7 mentioned on his answer about MVVM, while it is an elegant pattern, ASPX Webform just doesn't have great support for that. MVVM is more popular in the WPF world.


Short answer is : look at MVP pattern that avoids potential code-behind asp.net forms issue by loosely coupling (de-coupling) your view and logic.

You may also want to go one step further and look at comparison MVC, MVP, ASP.NET.

The following posts are good entry points and examples:


The one thing you need to avoid at all costs is the WebForms anti-pattern of putting logic in your code-behind. Whether you use MVP or a flavor of that or not, your code-behind files should be very sparse for the sake of your sanity.

  • 1
    At present my code-behind is tightly couple with the logic. And this is the biggest mistake Web Form developers often do. – RPK Jun 30 '12 at 12:14

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView.

Here is the technique I use:

I design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where I include the controls that bind to model properties using Binding expressions.

I set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks I always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view.

I run the logic inside the ViewModel and on PreRender I rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and of course ensure that is serializable and cached in ViewState or any other place I choose.

Finally I disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state.

This technique never failed me so far and I only wish I could find a solution for command/button binding too, like WPF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.