Overview
In this article i will show you how to build a component model for ASP.NET MVC3 with Builder and Factory design patterns. You could use this model to write your strongly type components to binding data, handling hierarchical objects and write jQuery UI controls / Html5 controls for ASP.NET MVC3.
Background
As we know in classical ASP.NET we could write controls by using ASP.NET control model for our web applications. Today in MVC3 we’re usually write helpers instead of controls. I think helper methods is a good choice for some of simple use scenarios but not for all. For render single tag, img or scripts helper methods does well. I can help thing how to develop more complex controls with help methods ? How to write template container controls ? How to write a data bindable treeview and menu ? How to write and use that complex controls easily ? In fact i love the helper method and i hate them at the same time. Because helper methods is the easy way to output my tags on view, but that is not OOP way ! Helpers is easy to use but hard to extend and maintenance. How could you read, remember and maintenance a method with over 10 overload version ? That is hell! So we need another way out! The way that could easy to learn, more readable, OOP , extensible and familiar for our control developing.
Before you start
There are some concepts you need to know:
- ASP.NET MVC : http://msdn.microsoft.com/en-us/library/dd381412(VS.98).aspx
- Creating Custom HTML Helpers : http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs
- Razor syntax : http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax
- Builder design pattern :[/publishing/home/2011/06/17/6859/builder.html | https://dotnetage.com/publishing/home/2011/06/17/6859/builder.html ]
- Factory method design pattern:[/publishing/home/2011/06/17/6857/factory-method.html |https://dotnetage.com/publishing/home/2011/06/17/6859/builder.html ]
Objectives
We need a component model for ASP.NET MVC
- Supports Razor and Web Form syntax.
- Offers interfaces or abstract classes to implement.
- Supports control template.
- Supports composite components.
- Supports data binding.
- Easy to integrate with client ability (javascripts jQuery).
Usage Design
The biggest difference between Mvc3 and classic ASP.NET is Mvc3 using Views and helpers to render html tags directly but no any component model like classic ASP.NET. So before design the component model we need to know how many ways could write controls in Mvc3 then we also need to know the common behaviors, properties, usages of the controls. Now let’s look back in Mvc3 and do some testing.
Usage1:Extension method
We calls “Html.XXX()” and “Ajax.XXX()” Mvc Helpers or Extensions,actually they are static extension method that is a good way to inject new methods to an exists class. When you are using Razor it provides a new syntax to write the helper method by using @helper. I put build in helpers which we are often use in the following list,(notes:each method has more then 5 overload versions.):
- Html.Display()
- Html.DisplayFor()
- Html.Label()
- Html.LabelFor()
- Html.LablelForModel()
- Html.ActionLink()
- Html.RoutLink()
- Html.Editor()
- Html.EditorFor()
- Html.EditorForModel()
- Html.CheckBox()
- Html.CheckBoxFor()
- Html.Hidden()
- Html.Password()
- Html.PasswordFor()
- Html.RadioButton()
- Html.RadioButtonFor()
- Html.TextBox()
- Html.TextBoxFor()
- Html.ListBox()
- Html.ListBoxFor()
- Html.DropDownList()
- Html.DropDownListFor()
- Html.TextArea()
- Html.TextAreaFor()
There are almost over 100 methods with overload versions of this list! Please note that these helpers only use to render