ASP.NET WebForms Model Binding – Overview
This is the first post in the part of series around Model Binding. In this post I will introduce Model Binding integration in ASP.NET WebForms 4.5 while focusing on history, benefits, principles and the integration of Model Binding. In the upcoming posts, I will be going into the details about the features that are supported by this system.
Update: Following are the posts in this series
What is Model Binding
Model Binding is a system which binds a value submitted from the client to a model on the server so that the model is available for inspection for validation rules and processing on the server. In essence the system has two fundamental functions – Binding & Validation.
In a WebForms application, data binding(from client to server and vice versa) typically happens via Data Bound and Data Source controls. While this approach has the advantages of an experience where you can get your application up and running easily, it does have some drawbacks for some developers who want to tap in more modern ways of developing applications, such as : doing validation using Data Annotations, PRG pattern, Repository patters, cleaner page code behind etc.
History Behind Model Binding
Model Binding as a system has existed in many Frameworks. Rails implements the core functions of Binding & Validation in its own way. ASP.NET MVC had Model binding support since its early stages. When ASP.NET MVC2 was released, the team released a prototype of an “Extensible Model Binding system”. This prototype made it possible for a Framework developer to write their own Model Binding system, should they choose to do so.(of course anyone in their right minds would not attempt it, but the ASP.NET team decided anyways to do this for WebForms)
Infact this Extensible Model Binding was how the team derived lots of inspiration from while trying to implement a Model Binding System that integrates the flexibility of Model Binding system and power of controls. ASP.NET WebAPI also derives some inspiration from this extensible model when ASP.NET WebAPI added support for binding using different formatters.
Benefits of Model Binding
With the introduction of Model Binding, it is now much easier to build modern webforms applications. Following are the areas where this integration adds value
- Rich controls support
- All the existing data bound controls such as GridView, ListView etc have been updated to work with Model Binding so you should be able to reuse the rich functionality of these controls
- Modern ways of validation using Data Annotation attributes
- Easier to propagate validation errors from Business Layer back to the UI on the page
- Easier to integrate model level validation errors(from ORM such as EF or anything of your choice) when saving the record in the database
- Dynamic Data support
- Easier to add validation via Data Annotations and client side validation
- Easier to customize the UI for columns using Data Annotations and Field Templates
- Add rich UI for integrating jquery based DateTime picker for DateTime fields
- Easier to customize the entities using Data Annotations and Entity Templates
- Easier to embrace the following patterns
- Unit Testing the application layer
- PRG pattern
- Repository pattern
- Cleaner page code behind methods. These server side code files do not have any application functional code and they only focus on UI interactions
- Extensible Model Binding system
- Support scenarios of binding in advanced scenarios such as complex types, ad-hoc Model Binding
- Customize the existing implementation to enable more commonly used scenarios easier such as Master Details view
- Add more Binders and Value providers
I hope so far this would help you understand the bigger picture with WebForms Model Binding and value it brings to an application developer. As Scott Hanselman noted, this integration also brings ASP.NET MVC/WebForms application development much closer.
In the next post we will be looking at some high level fundamentals and features on Model Binding in WebForms