Download - ASP.NET MVC Extensibility

Transcript
Page 1: ASP.NET MVC Extensibility

Extending ASP.NET MVCPart 1

Simone ChiarettaArchitect, Council of the EUhttp://codeclimber.net.nzTwitter: @simonech

June 23rd, 2010

Page 2: ASP.NET MVC Extensibility

Who the hell am I?

► Simone Chiaretta► Microsoft MVP ASP.NET► ASP Insider► Blogger – http://codeclimber.net.nz ► Italian ALT.NET UG Founder► OpenSource developer► Climber► All Around Nice Guy

Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"

Page 3: ASP.NET MVC Extensibility

Agenda

► The default ASP.NET MVC Pipeline► Extensibility points’ list► The Most important extensibility points

Page 4: ASP.NET MVC Extensibility

The Default ASP.NET MVC Pipeline

Page 5: ASP.NET MVC Extensibility

The default ASP.NET MVC Pipeline

Request

URLRouting

Route RouteHandler

HttpHandler

Controller Factory Controller

Action

ViewFactory View

Response

5

Page 6: ASP.NET MVC Extensibility

The default components

► URL Routing:– Parses the URL, and instantiate the MvcHandler

► Controller Factory– Takes URL parameters, create controller via reflection based on Controller name

► Action Invoker– Invokes the action based on the name, with the filters before and after

► View Engine– WebForms view engine

► Template– Renders a TextBox almost for everything

► HtmlHelper– Has a bunch of standard methods

Page 7: ASP.NET MVC Extensibility

ASP.NET MVC IS extensible

► If you don’t like them► If you need something else► Change them

Page 8: ASP.NET MVC Extensibility

ASP.NET MVC IS extensible

► Almost every aspect of the framework can be extented/replaced

Page 9: ASP.NET MVC Extensibility

Extensibility points’ list

Page 10: ASP.NET MVC Extensibility

Routing extensibility

► RouteConstraint– Validates route parameters with code

► RouteHandler– Defines how the request must be handled

Page 11: ASP.NET MVC Extensibility

Controller Extensibility

► ControllerFactory– Responsible for creating Controllers

► ActionInvoker– Invokes an action, based only on its name

► ActionMethodSelector Attribute– Helps the action invoker decide which action to invoke when the name is not unique

► Controller– The base class for every controller

► ActionResult– Decides how to send the output to the user

Page 12: ASP.NET MVC Extensibility

Action Filter Extensibility

► AuthorizationFilter– Makes sure the current request is allowed

► Action Filters– Executed before or after the action executes

► Result Filters– Executed before or after an action result executes

Page 13: ASP.NET MVC Extensibility

Binding/Validation Extensibility

► ModelBinder– Populates the action method parameters from the request

► ModelValidator Provider– Retrieves the validation rules

► Server-side Validation Rules– The actual server-side validation rule

Page 14: ASP.NET MVC Extensibility

View Extensibility

► ViewEngine– The service that transforms in HTML the data for the user

► HtmlHelpers– Utility functions that hide away the generation of some HTML markup or JavaScript code

► Client-side Validation Rules– Client-side validation rules

► ModelMetadata Provider– Retrieves the metadata needed for the templated helpers

► Custom Templates– Renders the html to edit/display specific types

Page 15: ASP.NET MVC Extensibility

The Most important extensibility points

Page 16: ASP.NET MVC Extensibility

The Most important Extensibilit points► The ones you will definitely use► The ones you have to start using► The ones you might or might not need to use

► The ones you most likely will not write yourself– but probably use written by others

Page 17: ASP.NET MVC Extensibility

Extensibility points you will use

Page 18: ASP.NET MVC Extensibility

Custom Templates

Page 19: ASP.NET MVC Extensibility

Custom Templates

► WHAT: Renders the html to edit/display specific types

► DEFAULT: Everything is a label or a textbox

► WHY: Add your own to customize specific data-types

Page 20: ASP.NET MVC Extensibility

Custom Templates

► Add PartialViews in:– /Views/Shared/DisplayTemplates– /Views/Shared/EditorTemplates

Demo

Page 21: ASP.NET MVC Extensibility

Server-side Validation Rules

Page 22: ASP.NET MVC Extensibility

Server-side Validation Rules

► WHAT: Define how a property is validated in the server side validation

► DEFAULT: Required, Length, wrong type► WHY: Add your own rules

Page 23: ASP.NET MVC Extensibility

Server-side Validation Rules

► Write a new ValidationAttribute► Implement the IsValid method► Apply attribute to your model

Demo

Page 24: ASP.NET MVC Extensibility

Client-side Validation Rules

Page 25: ASP.NET MVC Extensibility

Client-side Validation Rules

► WHAT: Define how a property is validated in the client-side validation

► DEFAULT: Client-side counterparts of the default server-side validators

► WHY: Add your own validators

Page 26: ASP.NET MVC Extensibility

Client-side Validation Rules

► First make a server-side validator► Make validation logic in JavaScript► Write adapter to push validation rules to client-side

► Register validation function via JS► Register adapter in Global.asax

Demo

Page 27: ASP.NET MVC Extensibility

Extensibility points you have to start using

Page 28: ASP.NET MVC Extensibility

Base Controller

Page 29: ASP.NET MVC Extensibility

Base Controller

► WHAT: The base class for every Controller► DEFAULT: Default implementation of helper methods

► WHY: Extend if you want to enforce you own conventions

Page 30: ASP.NET MVC Extensibility

Base Controller

► Override Controller► Your controllers override from BaseController instead of Controller

Demo

Page 31: ASP.NET MVC Extensibility

HtmlHelpers

Page 32: ASP.NET MVC Extensibility

HtmlHelpers

► WHAT: Utility functions that hide away the generation of HTML markup or JavaScript code

► DEFAULT: Html.TextBox, Html.Encode, Html.Partial, …

► WHY: “If there is an if, write an Helper”

Page 33: ASP.NET MVC Extensibility

HtmlHelpers

► Add new methods as extension methods

Demo

Page 34: ASP.NET MVC Extensibility

Rating

If you liked this talk, please consider rating it:

http://speakerrate.com/talks/3669-asp-net-mvc-extensibility

34 Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"

Page 35: ASP.NET MVC Extensibility

Extending ASP.NET MVCPart 2

Simone ChiarettaArchitect, Council of the EUhttp://codeclimber.net.nzTwitter: @simonech

June 23rd, 2010

Page 36: ASP.NET MVC Extensibility

Filters

Page 37: ASP.NET MVC Extensibility

Authorization Filter

► WHAT: Makes sure the current request is allowed

► DEFAULT: It is based on the ASP.NET Membership provider

► WHY: Change if you want not to use ASP.NET MVC or if you want to enhance the route dictionary

Page 38: ASP.NET MVC Extensibility

Action Filters

► WHAT: Executed before or after the action executes

► DEFAULT: Output cache► WHY: Add your own based on your needs

Page 39: ASP.NET MVC Extensibility

Result Filters

► WHAT: Executed before or after an action result executes

► DEFAULT: No default result filters► WHY: Add your own based on your needs

Page 40: ASP.NET MVC Extensibility

Authorization Filter

► Implement IAuthorizationFilter– OnAuthorization

Page 41: ASP.NET MVC Extensibility

Filters

► Implement IActionFilter + IResultFilter► Make new ActionFilterAttribute

– OnActionExecuting– OnActionExecuted

► Implement IResultFilter– OnResultExecuting– OnResultExecuted

Page 42: ASP.NET MVC Extensibility

Extensibility points you might want to use

Page 43: ASP.NET MVC Extensibility

RouteConstraint

Page 44: ASP.NET MVC Extensibility

RouteConstraint

► WHAT: Validates route parameters► DEFAULT: No default implementation► WHY: Add your own when needed

Page 45: ASP.NET MVC Extensibility

RouteConstraint

► Implement– IRouteConstraint.Match

► Inspect request and return true or false

Demo

Page 46: ASP.NET MVC Extensibility

RouteHandler

Page 47: ASP.NET MVC Extensibility

RouteHandler

► WHAT: Defines how the request must be handled

► DEFAULT: Routes the request to MvcHandler► WHY: Change if you want not to use ASP.NET MVC

Page 48: ASP.NET MVC Extensibility

RouteHandler

► Implement– IRouteHandler.GetHttpHandler

Demo

Page 49: ASP.NET MVC Extensibility

ActionMethodSelector Attribute

Page 50: ASP.NET MVC Extensibility

ActionMethodSelector Attribute

► WHAT: Helps the action invoker decide which action to invoke when the name is not unique

► DEFAULT: HttpMethod attributes: decide based on the HttpMethod

► WHY: Add your own to support different scenarios

Page 51: ASP.NET MVC Extensibility

ActionMethodSelector Attribute

► Create new Attibute inheriting from– ActionMethodSelectorAttribute

► Implement:– IsValidForRequest

Demo

Page 52: ASP.NET MVC Extensibility

ActionResult

Page 53: ASP.NET MVC Extensibility

ActionResult

► WHAT: Sends the output to the user► DEFAULT: ViewResult, RedirectResult, FileResult, JsonResult, etc…

► WHY: Add your own if you need an output not available in the default results

Page 54: ASP.NET MVC Extensibility

ActionResult

► Create a new result inheriting from:– ActionResult

► Implement:– ExecuteResult

► Optionally create an helper method in you “base controller”

Demo

Page 55: ASP.NET MVC Extensibility

ModelBinder

Page 56: ASP.NET MVC Extensibility

ModelBinder

► WHAT: Populates the action method parameters from the request

► DEFAULT: Binds request’s values based on names

► WHY: Extend if you need to add other way of binding

Page 57: ASP.NET MVC Extensibility

ModelBinder

► Implement IModelBinder– BindModel

► Inherit from DefaultModelBinder– BindProperty– OnModelUpdated– OnModelUpdating

► Register the ModelBinder

Demo

Page 58: ASP.NET MVC Extensibility

Very unlikely to write your own

Page 59: ASP.NET MVC Extensibility

ControllerFactory

Page 60: ASP.NET MVC Extensibility

Controller Factory

► WHAT: Responsible for creating Controllers► DEFAULT: Create an instance of the Controller via Reflection based on its name

► WHY: Change if you want to create the controller in other ways or if you want to add some funtionalities

► Most of the main IoC container have a custom ControllerFactory

Page 61: ASP.NET MVC Extensibility

Controller Factory

► Implement IControllerFactory– CreateController– ReleaseController

► Override DefaultControllerFactory– GetControllerInstance(Type controllerType)

Demo

Page 62: ASP.NET MVC Extensibility

ActionInvoker

Page 63: ASP.NET MVC Extensibility

ActionInvoker

► WHAT: Invokes an action, based only on its name

► DEFAULT: Call the action method, via reflection, based on its name, gathering filters via attributes and calling them before and after

► WHY: Change if you want to change the way methods are called, or want different way to configure Filters

Page 64: ASP.NET MVC Extensibility

ActionInvoker

► Implement IActionInvoker– InvokeAction

► Override ControllerActionInvoker– InvokeActionMethodWithFilters– InvokeActionResultWithFilters– GetFilters– …

Demo

Page 65: ASP.NET MVC Extensibility

ViewEngine

Page 66: ASP.NET MVC Extensibility

View Engine

► WHAT: The service that transforms in HTML the data for the user

► DEFAULT: WebForm view engine► WHY: Change if you don’t like the WebForm approach

Page 67: ASP.NET MVC Extensibility

View Engine

► IViewEngine:– Usually override VirtualPathProviderViewEngine– CreateView

► IView:– Render

Demo

Page 68: ASP.NET MVC Extensibility

ModelValidator Provider

Page 69: ASP.NET MVC Extensibility

ModelValidator Provider

► WHAT: Retrieves the validation rules► DEFAULT: Validation rules are defined with DataAnnotation attributes

► WHY: Change if you want to specify validation rules in other ways (XML, Database, etc…)

Page 70: ASP.NET MVC Extensibility

ModelValidator Provider

► Override from ModelValidatorProvider► Implement GetValidators

Page 71: ASP.NET MVC Extensibility

ModelMetadata Provider

Page 72: ASP.NET MVC Extensibility

ModelMetadata Provider

► WHAT: Retrieves the metadata needed for the templated helpers

► DEFAULT: Metadata are defined with DataAnnotation attributes

► WHY: Change if you want to specify metadata in other ways (XML, Database, etc…)

Page 73: ASP.NET MVC Extensibility

ModelMetadata Provider

► Inherit from ModelMetadataProvider► Implement

– GetMetadataForType– GetMetadataForProperty– GetMetadataForProperties

Page 74: ASP.NET MVC Extensibility

Contacts – Simone Chiaretta

► MSN: [email protected]► Blog:

– English: http://codeclimber.net.nz/– Italian: http://blogs.ugidotnet.org/piyo/

► Twitter: @simonech

74

Page 75: ASP.NET MVC Extensibility

Rating

If you liked this talk, please consider rating it:

http://speakerrate.com/talks/3669-asp-net-mvc-extensibility

75 Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"