ASP.NET MVC One Step Deeper

3,843 views
3,694 views

Published on

This is a one step deeper into ASP.NET MVC, the presentation was originally part of Jordev web camp happened in Jordan on the 14th of May 2011.

The presentation is general and its target is to throw terms on the audience so they can have general understanding that they can use it later for more detailed study.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,843
On SlideShare
0
From Embeds
0
Number of Embeds
668
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • Model is ignorantView knows about Model onlyController knows about both Model and View
  • Image from Dino Esposito’s article
  • Check the references to see where from this figure was used
  • 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.
  • 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.
  • 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
  • Filter Providers are filters of Filters by which you can manipulate filters before action
  • 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
  • Razor knows where to break
  • PageData to pass data to the Layout page@functions you can declare code thereUse @Url.Content for proper path
  • Html Helpers are Extension methods that will make it easy to render html code for different uses (displaying links, fields, etc…)
  • A SelectList instance can be used to populate the list with the default value
  • 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
  • ASP.NET MVC One Step Deeper

    1. 1. ASP.NET MVC <br />One Step Deeper<br />
    2. 2. Who I am<br />Emad Alashi<br />Technical Team Leader in eSense software<br />http://www.EmadAshi.com<br />http://www.DotNetArabi.com<br />@EmadAshi<br />
    3. 3. DotNetArabi دوت نت عربي<br />
    4. 4. ASP.NET MVC <br />One Step Deeper<br />
    5. 5. ASP.NET MVC One Step Deeper<br /><ul><li>Fast intro
    6. 6. Dependency Injection
    7. 7. Action Filters
    8. 8. Razor
    9. 9. HTML Forms</li></li></ul><li>MVC Pattern<br />
    10. 10. ASP.NET MVC<br />
    11. 11. ASP.NET MVC<br />
    12. 12. Dependency Injection<br />ASP.NET MVC One Step Deeper<br />
    13. 13. Dependency Injection<br />AccountingCalculations<br />IoC Container<br />JOTaxCalculator<br />ITaxCalculator<br /><ul><li>Example of IoC containers:
    14. 14. Windsor
    15. 15. Ninject
    16. 16. Unity</li></li></ul><li>Dependency Injection<br /><ul><li>Service Locators (hides containers)
    17. 17. IDependencyResolver
    18. 18. GetService
    19. 19. GetServices
    20. 20. DependencyResolver.SetResolver(Object)</li></li></ul><li>Dependency Injection<br /><ul><li>IDependencyResolver</li></li></ul><li>Dependency Injection<br /><ul><li>DependencyResolver.SetResolver(…)</li></li></ul><li>Dependency Injection<br /><ul><li>DI needed in creating Controllers</li></li></ul><li>Dependency Injection<br />DI needed for creating Controllers:<br /><ul><li>No default constructor for StoreController</li></ul>StoreController(IStoreRepository repository)<br />IDependencyResolver kicks in<br />The original IoC container is called through “GetService()”<br />Controller is created<br /><ul><li>No need for ControllerFactory</li></li></ul><li>Filters<br />ASP.NET MVC One Step Deeper<br />
    21. 21. Filters<br /><ul><li>Types of Filters:
    22. 22. Authorization (IAuthorizationFilter)
    23. 23. Action (IActionFilter)
    24. 24. Result (IResultFilter)
    25. 25. Exception (IExceptionFilter)
    26. 26. ActionFilterAttribute: IActionFilter, IResultFilter
    27. 27. Examples of Filters:
    28. 28. Authorize
    29. 29. OutputCache
    30. 30. ChildActionOnly
    31. 31. HandleError
    32. 32. RequireHttps
    33. 33. ValidateAntiForgeryToken</li></li></ul><li>Action Filters<br /><ul><li>Filter Providers, IFilterProvider (MVC 3):
    34. 34. Filters Filters
    35. 35. Can manipulate Filters by DI their properties
    36. 36. Default Filter Providers:
    37. 37. GlobalFilters.Filters
    38. 38. FilterAttributeFilterProvider
    39. 39. ControllerInstanceFilterProvider
    40. 40. You can use multiple Providers</li></li></ul><li>Action Filters<br />Example of Filter Provider<br />
    41. 41. Razor<br />ASP.NET MVC 3 One Step Deeper<br />
    42. 42. Razor<br /><ul><li>Compact, Expressive, and Fluid
    43. 43. Easy to Learn
    44. 44. Is not a new language
    45. 45. Has great Intellisense
    46. 46. Unit Testable</li></li></ul><li>Razor<br />Webforms<br />Razor<br />
    47. 47. Razor<br />Webforms<br />Razor<br />
    48. 48. Razor<br /><ul><li>Multiline statement
    49. 49. Multiline token</li></li></ul><li>Razor<br /><ul><li>Layout (= MasterPages)</li></ul>@RenderBody()<br />@RenderSection(<br />“SectionName”, optional:true<br />)<br />
    50. 50. Razor<br /><ul><li>Content page for Layouts</li></li></ul><li>Razor<br /><ul><li>Content page for Layouts</li></ul>Content<br />@section sectionName<br />{<br />}<br />
    51. 51. Razor<br /><ul><li>_ViewStart.cshtml:
    52. 52. The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering
    53. 53. PageData[“ApplicationTitle”]
    54. 54. @functions
    55. 55. @Url.Content
    56. 56. Razor Inline Templates (Advanced)</li></li></ul><li>HTML Forms<br />ASP.NET MVC 3 One Step Deeper<br />
    57. 57. HTML Forms<br /><ul><li>HTML Helpers (Html.HelperMethod())
    58. 58. ActionLink
    59. 59. BeginForm
    60. 60. CheckBox
    61. 61. DropDownList
    62. 62. Password
    63. 63. Hidden
    64. 64. …</li></li></ul><li>HTML Forms<br /><ul><li>HTML Helpers (Html.HelperMethod())
    65. 65. SelectList</li></li></ul><li>HTML Forms<br /><ul><li>EditorForModel() & EditorFor(m=>m.Property)
    66. 66. Shows proper field types depending on the Model Property types
    67. 67. Properties can be given special behavior through attributes:</li></li></ul><li>HTML Forms<br /><ul><li>UIHint(“NameOfTemplate”)</li></li></ul><li>Q/A<br />Thanks for attending<br />http://www.EmadAshi.com<br />http://www.DotNetArabi.com<br />@EmadAshi<br />
    68. 68. References<br /><ul><li>ASP.NET MVC 3 Dependency Injection
    69. 69. ASP.NET MVC 3 Service Location (series by Brad Wilson)
    70. 70. Action Filters in ASP.NET MVC (by Dino Esposito)
    71. 71. Get to Know Action Filters in ASP.NET MVC 3 Using HandleError
    72. 72. Dependency Injection for Filters in MVC3
    73. 73. Conditional Filters in ASP.NET MVC 3
    74. 74. Introducing “Razor” – a new view engine for ASP.NET
    75. 75. Razor Template in ASP.NET MVC 3
    76. 76. MVC 3 HTML Helpers
    77. 77. Webcamps.ms slides in training kit
    78. 78. Patterns and practices in the Composite Application Library – part 2
    79. 79. Understanding Action Filters</li>

    ×