ASP.NET MVC One Step Deeper
Who I amEmad AlashiTechnical Team Leader in eSense softwarehttp://www.EmadAshi.comhttp://www.DotNetArabi.com@EmadAshi
DotNetArabi دوت نت عربي
ASP.NET MVC One Step Deeper
ASP.NET MVC One Step DeeperFast intro
Dependency Injection
Action Filters
Razor
HTML FormsMVC Pattern
ASP.NET MVC
ASP.NET MVC
Dependency InjectionASP.NET MVC One Step Deeper
Dependency InjectionAccountingCalculationsIoC ContainerJOTaxCalculatorITaxCalculatorExample of IoC containers:
Windsor
Ninject
UnityDependency InjectionService Locators (hides containers)
IDependencyResolver
GetService
GetServices
DependencyResolver.SetResolver(Object)Dependency InjectionIDependencyResolverDependency InjectionDependencyResolver.SetResolver(…)Dependency InjectionDI needed in creating ControllersDependency InjectionDI needed for creating Controllers:No default constructor for StoreControllerStoreController(IStoreRepository repository)IDependencyResolver kicks inThe original IoC container is called through “GetService()”Controller is createdNo need for ControllerFactoryFiltersASP.NET MVC One Step Deeper
FiltersTypes of Filters:
Authorization (IAuthorizationFilter)
Action (IActionFilter)
Result (IResultFilter)
Exception (IExceptionFilter)
ActionFilterAttribute: IActionFilter, IResultFilter
Examples of Filters:
Authorize
OutputCache
ChildActionOnly
HandleError
RequireHttps
ValidateAntiForgeryTokenAction FiltersFilter Providers, IFilterProvider (MVC 3):
Filters Filters

ASP.NET MVC One Step Deeper

Editor's Notes

  • #7 Model is ignorantView knows about Model onlyController knows about both Model and View
  • #8 Image from Dino Esposito’s article
  • #11 Check the references to see where from this figure was used
  • #12 Service Locators will act as a layer over Dependency Injection containersYou have to implement IDependencyResolver in order to use the container you want and add any specific logic you might need, though it’s advised not to go too sophisticated thereNinject will implmentIDependencyResolver for youYou will call DependencyResolver.SetResolver to set the resolver Dah!Each part of MVC will use this Service Locator and in the way it sees fit; Controllers have DefaultControllerActivator that uses your new implementation of IDependencyResolver, View have DefaultViewPageActivator, …etc.
  • #13 Service Locators will act as a layer over Dependency Injection containersYou have to implement IDependencyResolver in order to use the container you want and add any specific logic you might need, though it’s advised not to go too sophisticated thereNinject will implmentIDependencyResolver for youEach part of MVC will use this Service Locator and in the way it sees fit; Controllers have DefaultControllerActivator that uses your new implementation of IDependencyResolver, View have DefaultViewPageActivator, …etc.
  • #18 Better Inherit from “ActionFilterAttribute” if you need to do attribute filterIf you want to do Authorization filter consider inheriting from “FilterAttribute” classFilters can decorate an Action, a Controller (ControllerInstance), and to be global Authorize: checks to see whether the current user is logged in, and matches a provided username or role name (or names), and if not it returns a 401 status, which in turn invokes the configured authentication providerChildActionOnly: used to indicate that the action method may only be called as part of a parent request, to render inline markup, rather then returning a full view template.OutputCache: tells ASP.NET to cache the output of the requested action, and to serve the cached output based on the parameters provided. HandleError: provides a mechanism for mapping exceptions to specific View templates, so that you can easily provide custom error pages to your users for specific exceptions (or simply have a generic error view template that handles all exceptions). RequireHttps: forces a switch from http to https by redirecting GET requests to the https version of the requested URL, and rejects non-https POST requestsValidateAntiForgeryToken: checks to see whether the server request has been tampered with. Used in conjunction with the AntiForgeryToken HTML Helper, which injects a hidden input field and cookie for later verification
  • #19 Filter Providers are filters of Filters by which you can manipulate filters before action
  • #20 You can inherit from a Provider (e.g. FilterAttributeFilterProvider) and override GetFilters().AsEnumerabe<Filter>() something I learned by reading blogs and trying code out. I can’t recall when or how, but this shows how important reading blogs and trying code out is, I would have had hard time figuring how to return an IEnumerable the way I wanted
  • #23 Razor knows where to break
  • #29 PageData to pass data to the Layout page@functions you can declare code thereUse @Url.Content for proper path
  • #31 Html Helpers are Extension methods that will make it easy to render html code for different uses (displaying links, fields, etc…)
  • #32 A SelectList instance can be used to populate the list with the default value
  • #34 UIHint is an attribute used to decorate a property in the Model class, will specify the name of the control to be used to display/edit the property