Retrofit Web Forms with MVC & T4

27
Soe Tun http://geekswithblogs.net/stun Twitter: @SoeLinn Retrofit WebForms with ASP.NET MVC 3 T 4 &

description

Presentation on ASP.NET MVC 3 for WebForms developers.

Transcript of Retrofit Web Forms with MVC & T4

Page 1: Retrofit Web Forms with MVC & T4

Soe Tunhttp://geekswithblogs.net/stunTwitter: @SoeLinn

Retrofit WebForms withASP.NET

MVC 3

T4&

Page 2: Retrofit Web Forms with MVC & T4

GrumpyGood Morning!

Page 3: Retrofit Web Forms with MVC & T4

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

Page 4: Retrofit Web Forms with MVC & T4

Model

ViewController

MVC Basics Demo

Page 5: Retrofit Web Forms with MVC & T4

HTML Template

(View)

“Code Behind”(Controller)

Data Transfer Object

(Model, View Model)

Page 6: Retrofit Web Forms with MVC & T4

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” });

Page 7: Retrofit Web Forms with MVC & T4

DemoApplication Functionality

Page 8: Retrofit Web Forms with MVC & T4

Core Functionality

User Interface Consistency Maintainability

Data Binding Reliability Security

Page 9: Retrofit Web Forms with MVC & T4

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

Page 10: Retrofit Web Forms with MVC & T4

User Interface #2Cluttered Code v.s Pure Presentation

Web Forms

ASP.NET MVC

Themes

Cluttered

Ugly

Page 11: Retrofit Web Forms with MVC & T4

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

Page 12: Retrofit Web Forms with MVC & T4

View TemplatesASP.NET MVC “Themes/Skins”

Page 13: Retrofit Web Forms with MVC & T4

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)

Page 14: Retrofit Web Forms with MVC & T4

Data BindingPage_Load and Postback

Page 15: Retrofit Web Forms with MVC & T4

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

Page 16: Retrofit Web Forms with MVC & T4

Data Binding #2Data Binding on PostBack v.s HTTP POST

Web Forms

ASP.NET MVC

1. View Model2. Data Binding3. Server-Side

Validation

Page 17: Retrofit Web Forms with MVC & T4

DemoT4 - Text Template Transformation Toolkit

Page 18: Retrofit Web Forms with MVC & T4

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

Page 19: Retrofit Web Forms with MVC & T4

[Custom Tools] Property

Leave [Custom Tool] property empty for all files with .t4 extension.

Page 20: Retrofit Web Forms with MVC & T4

DemoModel Metadata = Presentation + Validation

Page 21: Retrofit Web Forms with MVC & T4

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)

Page 22: Retrofit Web Forms with MVC & T4

Model Metadata #2Resource Files (.resx)

Page 23: Retrofit Web Forms with MVC & T4

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

Page 24: Retrofit Web Forms with MVC & T4

SummaryWebForms ASP.NET MVC

Web Controls Html.EditorFor()

Themes / Skins Use View Templates

Validation Attributes from Data Annotations

Data Binding View ModelsandViewData[“PropertyName”] = value;

Page 25: Retrofit Web Forms with MVC & T4

Confused ???Questions ??

Page 26: Retrofit Web Forms with MVC & T4

Soe Tunhttp://geekswithblogs.net/stunTwitter: @SoeLinnEmail: [email protected]

Thank you!

Page 27: Retrofit Web Forms with MVC & T4

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.