1 / 22

WEB402: Design Considerations for ASP.NET MVC Applications

WEB402: Design Considerations for ASP.NET MVC Applications. Dino Esposito R&D Manager Crionet Solutions (dino.esposito@crionet.it). Content. Controllers Structure of the class & implementation of methods Injection of dependencies Defining data models for the views Action filters and AOP.

christmas
Download Presentation

WEB402: Design Considerations for ASP.NET MVC Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WEB402: Design Considerations for ASP.NET MVC Applications Dino Esposito R&D Manager Crionet Solutions (dino.esposito@crionet.it)

  2. Content • Controllers • Structure of the class & implementation of methods • Injection of dependencies • Defining data models for the views • Action filters and AOP

  3. Controller methods vs. Button1_Click • Abstractly speaking, some inherent similarities exist … public ActionResult Index(...) { // Perform the expected task // and render the next view } public void Button1_Click(...) { // Perform the expected task // and update server controls }

  4. Role of the controller Button1_Click Controllers Tightly bound to user requests Further layering is up to you Full testability … if dependencies are properly managed Input data comes from parameters Response results from view engines • Tightly bound to events • Further layering is up to you • Impractical to test • … even when layered • Input data comes through server controls • Response results from server controls

  5. What flavor of controller would you like?Applicable stereotypes from RDD • Service Provider • Performs work • Controller • Makes decisions and closely directs the actions of other objects • Coordinator • Reacts to events by delegating tasks to others

  6. Fat-free controllersLean and mean collections of endpoints • Extremely simple action methods • Single call to get data for the view • Yield to the view engine • Grab exceptions via attributes • Who really does the job? • Controller delegates to other components the task • Arranges data into a view model

  7. Fat-free controllers Dream code Handles all exceptions as may be raised • [HandleError] • public class BookingController : Controller • { • public IBookingControllerActionServiceActionService { get; private set; } • public BookingController(IBookingControllerWorkerService service) • { • Contract.Requires<ArgumentNullException>(service != null); • ActionService = service; • } • [BookingAddHandleError] • public ActionResult Add(BookingAddInputModelinputModel) • { • Contract.Requires<ArgumentNullException>(inputModel != null); • var model = ActionService.InsertNewBooking(inputModel); • return View(model); • } • : • } Handles any exceptions raised within the action Unpacks input data Calls the middle tier Creates view model Packs data into model

  8. Action servicesPart of the presentation layer Controller Exception layer Action Action Action Input View model Action exception service Action exception service Action exception service Action Service Action Service Action Service Service (Application) Layer Domain Model Data Access Layer

  9. Fat-free controllers demo

  10. One step furtherDo we really need controllers? • Is the controller a mere pass-through layer? • Stateless and mere container of (rich & componentized) actions • Fundamental part of the ASP.NET MVC deal • Food for thought • Controller-less actions • Command pattern in a custom invoker • Instantiate an action object with methods based on HTTP verbs • Implementation: still needs to implement IController

  11. View ModelsData worked on in the view • Classic weakly-typed approach • ViewData works well, easy to use, easy to understand • Strongly-typed approach • Avoid magic strings/casts, catch errors at compile time, IntelliSense • IMHO, only viable option in medium-sized applications • Dynamic approach • Based on dynamic type in .NET 4

  12. Dynamic view models demo

  13. Frequently required componentsController factory • Every requests passes through the controller factory • Write yours and gain control over the request lifecycle • Nice things to do • Set your tailor-made invoker • User ad hoc controllers for some types (or bypass controllers) • Plug in your favorite IoC framework • Trigger MEF composition

  14. Frequently required componentsAction invoker • Controls the performance of any action for a controller • Write yours and gain control over the code being executed • Nice things to do • Set culture on the current thread • Add/remove action filters • Override methods to control/alter request lifecycle

  15. Unity-based controller factory demo

  16. Action filtersNative aspect-orientation of ASP.NET MVC • Pre/post process actions • Pre/post process generation of action results • Built-in filters • Authorization, error handling, output caching

  17. Action filtersGallery • Detect which of many submit buttons was clicked • Complete the view model with common/shared data • Compress/Track/Customize output • Build browser-specific views for any action • Load filters dynamically (from a configuration file)

  18. Multiple submit buttons demo

  19. Conclusions • Fat-free controllers • Strongly-typed view • Single Responsibility Principle • Leverage built-in capabilities (binding, filters, contracts) • Build your own facilities on top of ASP.NET MVC capabilities

  20. Session Evaluations Tell us what you think, and you could win! All evaluations submitted are automatically entered into a daily prize draw*  Sign-in to the Schedule Builder at http://europe.msteched.com/topic/list/ * Details of prize draw rules can be obtained from the Information Desk.

  21. © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

More Related