Retrofit Web Forms with MVC & T4
-
Upload
soelinn -
Category
Technology
-
view
3.426 -
download
5
description
Transcript of Retrofit Web Forms with MVC & T4
Soe Tunhttp://geekswithblogs.net/stunTwitter: @SoeLinn
Retrofit WebForms withASP.NET
MVC 3
T4&
GrumpyGood Morning!
About Me
Soe Tun American College of Cardiology (ACC) Email: [email protected] Twitter: @SoeLinn Blog: http://geekswithblogs.net/stun/
DC .NET User Group Twitter: @dcdnug 2400 N St. NW DC 20037 http://dcdnug.org
Model
ViewController
MVC Basics Demo
HTML Template
(View)
“Code Behind”(Controller)
Data Transfer Object
(Model, View Model)
public ActionResult Index(){ IList<Patient> model = GetPatients();
return View(“Index”, model);}
PatientController
http://localhost/Patient/Index
http://localhost/Patient/
http://localhost/
Least Specific Most Specific
Additional Infohttp://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cshttp://haacked.com/archive/2010/02/12/asp-net-mvc-2-optional-url-parameters.aspx
routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Patient”, action = “Index” });
DemoApplication Functionality
Core Functionality
User Interface Consistency Maintainability
Data Binding Reliability Security
User Interface #1[Web Controls] v.s. [HTML Helper Methods]
<asp:TextBox /> Date Number (int,
decimal, ...) String
<asp:DropDownList /> Foreign Keys Nullable<bool>
<asp:CheckBox /> Boolean
Html.EditorFor(m => m.FirstName)
One MethodTo Rule Them
All
Web Forms
ASP.NET MVC
MVC
User Interface #2Cluttered Code v.s Pure Presentation
Web Forms
ASP.NET MVC
Themes
Cluttered
Ugly
User Interface #3 HTML Helper Methods Strings v.s Strongly Typed
Other HTML Helper methods Html.TextBox(“FirstName”); Html.DropDownList(“UidBloodType”);
Html.EditorFor( m => m.FirstName ) Strongly-Typed Intellisense
View TemplatesASP.NET MVC “Themes/Skins”
View Templates ASP.NET MVC “Themes/Skins”
Matched on System.Type Name DateTime.ascx for DateTime
Using UIHintAttribute [UIHint(“SelectionElement”)]
public int UidBloodType { get; set; }
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
Brad Wilson – ASP.NET MVC 2 Templates (series)
Data BindingPage_Load and Postback
Data Binding #1Data Binding on Page_Load v.s HTTP GET
Web Forms
Patient model = service.GetPatient( id );PatientViewModel viewModel = Mapper.Map<Patient, PatientViewModel>(model);
viewModel.RaceCheckBoxes = GetRaceSelections(model.UidPatient); viewModel.BloodTypes = new SelectList(service.GetBloodTypes(), "UidBloodType", "BloodTypeDesc", model.UidBloodType);
return View("Edit", viewModel);
ASP.NET MVC
Data Binding #2Data Binding on PostBack v.s HTTP POST
Web Forms
ASP.NET MVC
1. View Model2. Data Binding3. Server-Side
Validation
DemoT4 - Text Template Transformation Toolkit
T4 Dependencies
T4 Toolboxo http://t4toolbox.codeplex.com/
Microsoft SQL Server 2008 SDK C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies
▪ Microsoft.SqlServer.ConnectionInfo.dll▪ Microsoft.SqlServer.Management.Sdk.Sfc.dll▪ Microsoft.SqlServer.Smo.dll
[Custom Tools] Property
Leave [Custom Tool] property empty for all files with .t4 extension.
DemoModel Metadata = Presentation + Validation
Model Metadata #1Data Annotations (namespace System.ComponentModel.DataAnnotations)
Presentation [DisplayName] and [Display].NET 4.0
[HiddenInput]
Validation [StringLength] [RegularExpression]
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.htmlhttp://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
Brad Wilson – ASP.NET MVC 2 Templates (series)
Model Metadata #2Resource Files (.resx)
Client-Side ValidationjQuery forever!
ASP.NET MVC 3 Built-in support for Validation Summary
with jQueryhttp://geekswithblogs.net/stun/archive/2011/01/28/aspnet-mvc-3-client-side-validation-summary-with-jquery-validation-unobtrusive-javascript.aspx
SummaryWebForms ASP.NET MVC
Web Controls Html.EditorFor()
Themes / Skins Use View Templates
Validation Attributes from Data Annotations
Data Binding View ModelsandViewData[“PropertyName”] = value;
Confused ???Questions ??
Soe Tunhttp://geekswithblogs.net/stunTwitter: @SoeLinnEmail: [email protected]
Thank you!
Additional Resources
Phil HaackCustom Validation Attributeshttp://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx
Kazi ManzurPost-Redirect-Get (PRG) Patternhttp://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx
Martijn BolandPaging with ASP.NET MVC http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/Note: I modified his code a little bit in my demo project.