ASP.NET MVCVolkanUzun
Web Developer at California State University, San BernardinoMS. Computer Science, MCP, and MCTSInterest Areas: TDD, Web Programming, Secure CodingUser group maniac, visiting many user groups in a monthBlog: http://www.msnetprogrammer.net/blog,Email: volkan@iedotnetug.orgTwitter: http://www.twitter.com/volkanuzunAbout Me
Classic ASP (Another Slow Page!?).NET Framework (Hurraay!!)ASP.NET v1.0 (No more slow pages)ASP.NET v1.1ASP.NET v2.0ASP.NET Ajax ASP.NET 3.5Silverlight ?!?!ASP.NET MVCASP.NET WebForms 4.0….MS Web Developer History
Easy to learn but?Maintenance?Test?Debugging?Complexity?What was wrong with ASP?
Desktop like programming paradigmLot’s of out ofbox controlsNo more spaghetti code?ASP.NET Web Forms
ViewState (supposed to help you but?)DRYCan’t control HTML(Power is nothing without control)Best Practices? Separation of Concerns? (I don’t have any concern)What is wrong with ASP.NET WebForms?
New Patterns
MVCDifferent people reading about MVC in different places take different ideas from it and describe these as “MVC”.Martin Fowler, GUI Architectures Essay (July 2006)
Model–View–Controller (MVC) is an architectural pattern Formulated in 1979 by TrygveMikkjel Heyerdahl ReenskaugThe model is the domain-specific representation of the data upon which the application operatesThe controller receives input and initiates a response by making calls on model objects.The view renders the model into a form suitable for interactionWikipedia: MVC
A new web project type built on top of ASP.NET.
NOT replacing ASP.NET Web Forms.
Giving you more control over html.
An easier to test framework.
Another option to web development.
Open source.
Convention over configuration.ASP.NET MVC
No View State, No PostbackVery flexible, pluggable.Very clean URLs, rest friendly urls.Supports ASP.NET Providers: Membership Provider, Role Provider, Session, Cache etc…Open Source.Community Support.Supports Separation of Concerns.Plays well with othersWhy is it good?
Complete control over html!!No View State, No Postback!!Paradigm change.Few components .No formal validation support.Any Disadvantage?
WebForms are dead?MVC does not have as much support as WebForms?MVC guarantees better coding?A project is either MVC or WebForms?MVC is faster than WebForms?True / False
Browser requests /Products/ListRoute is selectedController is activated (Products)Controller executes the action method (List)Action talks to Model, prepares the ViewDataAction renders ViewBut what is it?
Located in Controllers folder, You can change itderived from Controller base classThe class name should be controller name + “Controller”Action Methods does the workDelete AccountControllerMove Controller’s to a separate projectController
The ambiguous part of ASP.NET MVCM is the only part of MVC that is not implemented in ASP.NET MVCView Model: How your data looks on the page, flat dataDomain Model: How your business data is, hierarchical dataAny ORM is fineMODEL
Just renders the page, no biz logic!!!No ViewState, no server controlsNO Codebehind, pls DON’TDefault ViewEngine is WebFormsCommunicates with Controller with ViewData DictionaryStrongly Typed ViewVIEW
FILE => NEW…Project Structure
Separate Domain Model and View ModelUse an ORMUse Repository PatternDomain Model should only validate domain rulesPresentation layer should NOT care about persistence layerBest Practices on Model
FALSEViewModel == DomainModelThe most annoying part of Presentation Model is the synchronization between Presentation Model and view…Martin Fowler, GUI Architectures Essay, July 2004
Music Store AppDisplay AlbumsDisplay CustomersRefactor the ModelUse AutoMapperDEMO
No Business LogicNo Concrete DependenciesSmall Action MethodsDon’t mix HttpVerbsAvoid magic stringsUse controller as a traffic flow managerUnit test your expectationsAll the public methods are web accessibleUse OutputCacheBest Practices on Controller
Define Views in an enum:return View(viewNames.Index);Even Better use T4MVCView.ModelMVCContrib for RedirectToAction:return RedirectToAction<HomeController>(c=>c.Index)Removing Magics in Controller
Refactor View Names and Links with T4MVCCache the outputWrite simple unit testDEMO
Dependency Injectionprivate IBookRepositorybookRepository;public HomeController(){bookRepository = new BookRepository();} What if BookRepository has dependencies?
 Is it HomeContoller’s job to satisfy dependencies?
 Why does Home Controller have to know about BookRepository()?

ASP.NET MVC Presentation

  • 1.
  • 2.
    Web Developer atCalifornia State University, San BernardinoMS. Computer Science, MCP, and MCTSInterest Areas: TDD, Web Programming, Secure CodingUser group maniac, visiting many user groups in a monthBlog: http://www.msnetprogrammer.net/blog,Email: volkan@iedotnetug.orgTwitter: http://www.twitter.com/volkanuzunAbout Me
  • 4.
    Classic ASP (AnotherSlow Page!?).NET Framework (Hurraay!!)ASP.NET v1.0 (No more slow pages)ASP.NET v1.1ASP.NET v2.0ASP.NET Ajax ASP.NET 3.5Silverlight ?!?!ASP.NET MVCASP.NET WebForms 4.0….MS Web Developer History
  • 5.
    Easy to learnbut?Maintenance?Test?Debugging?Complexity?What was wrong with ASP?
  • 6.
    Desktop like programmingparadigmLot’s of out ofbox controlsNo more spaghetti code?ASP.NET Web Forms
  • 7.
    ViewState (supposed to help you but?)DRYCan’t control HTML(Power is nothing without control)Best Practices? Separation of Concerns? (I don’t have any concern)What is wrong with ASP.NET WebForms?
  • 8.
  • 9.
    MVCDifferent people readingabout MVC in different places take different ideas from it and describe these as “MVC”.Martin Fowler, GUI Architectures Essay (July 2006)
  • 10.
    Model–View–Controller (MVC) is an architecturalpattern Formulated in 1979 by TrygveMikkjel Heyerdahl ReenskaugThe model is the domain-specific representation of the data upon which the application operatesThe controller receives input and initiates a response by making calls on model objects.The view renders the model into a form suitable for interactionWikipedia: MVC
  • 11.
    A new webproject type built on top of ASP.NET.
  • 12.
  • 13.
    Giving you morecontrol over html.
  • 14.
    An easier totest framework.
  • 15.
    Another option toweb development.
  • 16.
  • 17.
  • 18.
    No View State,No PostbackVery flexible, pluggable.Very clean URLs, rest friendly urls.Supports ASP.NET Providers: Membership Provider, Role Provider, Session, Cache etc…Open Source.Community Support.Supports Separation of Concerns.Plays well with othersWhy is it good?
  • 19.
    Complete control overhtml!!No View State, No Postback!!Paradigm change.Few components .No formal validation support.Any Disadvantage?
  • 20.
    WebForms are dead?MVCdoes not have as much support as WebForms?MVC guarantees better coding?A project is either MVC or WebForms?MVC is faster than WebForms?True / False
  • 21.
    Browser requests /Products/ListRouteis selectedController is activated (Products)Controller executes the action method (List)Action talks to Model, prepares the ViewDataAction renders ViewBut what is it?
  • 22.
    Located in Controllersfolder, You can change itderived from Controller base classThe class name should be controller name + “Controller”Action Methods does the workDelete AccountControllerMove Controller’s to a separate projectController
  • 23.
    The ambiguous partof ASP.NET MVCM is the only part of MVC that is not implemented in ASP.NET MVCView Model: How your data looks on the page, flat dataDomain Model: How your business data is, hierarchical dataAny ORM is fineMODEL
  • 24.
    Just renders thepage, no biz logic!!!No ViewState, no server controlsNO Codebehind, pls DON’TDefault ViewEngine is WebFormsCommunicates with Controller with ViewData DictionaryStrongly Typed ViewVIEW
  • 25.
  • 26.
    Separate Domain Modeland View ModelUse an ORMUse Repository PatternDomain Model should only validate domain rulesPresentation layer should NOT care about persistence layerBest Practices on Model
  • 27.
    FALSEViewModel == DomainModelThemost annoying part of Presentation Model is the synchronization between Presentation Model and view…Martin Fowler, GUI Architectures Essay, July 2004
  • 28.
    Music Store AppDisplayAlbumsDisplay CustomersRefactor the ModelUse AutoMapperDEMO
  • 29.
    No Business LogicNoConcrete DependenciesSmall Action MethodsDon’t mix HttpVerbsAvoid magic stringsUse controller as a traffic flow managerUnit test your expectationsAll the public methods are web accessibleUse OutputCacheBest Practices on Controller
  • 30.
    Define Views inan enum:return View(viewNames.Index);Even Better use T4MVCView.ModelMVCContrib for RedirectToAction:return RedirectToAction<HomeController>(c=>c.Index)Removing Magics in Controller
  • 31.
    Refactor View Namesand Links with T4MVCCache the outputWrite simple unit testDEMO
  • 32.
    Dependency Injectionprivate IBookRepositorybookRepository;publicHomeController(){bookRepository = new BookRepository();} What if BookRepository has dependencies?
  • 33.
    Is itHomeContoller’s job to satisfy dependencies?
  • 34.
    Why doesHome Controller have to know about BookRepository()?
  • 35.
    What ifHomeController needs initialization param in the constructor?private IBookRepositorybookRepository;public HomeController(IBookRepository repo){bookRepository = repo;} But who calls the controller with a parameterized constructor? NOT MVC Framework, at least not the default ControllerFactoryBetter Injection
  • 36.
    Extend ControllerFactoryUse oneof the IOC frameworks:StructureMap, Ninject, Windsor etc…Even Better Injection
  • 37.
  • 38.
    No Magic String(use T4MVC)Change ../ pathsStrongly Typed ViewRenderPartialHtmlHelpersUse HTML if you canIf there is an “If” , time to check HtmlHelpersEncode input stringBest Practices On View
  • 39.
  • 40.
    Separate ControllersUse IoCNoBusiness Logic in ControllersNo Magic Strings in any componentUse HTML HelpersDRYTestStrongly Types ViewsList of Practices
  • 41.
    Client side validationwith JQueryTemplatingStrongly typed helpersAreas SupportAsync Controller ActionsNew Things With MVC 2
  • 42.
    Thanks to JonasFollesø (http://jonas.follesoe.no)Thank you

Editor's Notes

  • #18  Use Repository Pattern Separate the Model layer from Control layerMVC is in presentation layer, model is in persistence layerDon’t let your UI talk to business model directlyUse orm, orm is not for only if I change the db later…
  • #22 Use automapper, silverlight and wpf has binding support , and mvvm support