You are previewing ASP.NET MVC in Action with MvcContrib, NHibernate, and More.
O'Reilly logo
ASP.NET MVC in Action with MvcContrib, NHibernate, and More

Book Description

ASP.NET MVC implements the Model-View-Controller pattern on the ASP.NET runtime. It works well with open source projects like NHibernate, Castle, StructureMap, AutoMapper, and MvcContrib.

ASP.NET MVC in Action is a guide to pragmatic MVC-based web development. After a thorough overview, it dives into issues of architecture and maintainability. The book assumes basic knowledge of ASP.NET (v. 3.5) and expands your expertise. Some of the topics covered:

  • How to effectively perform unit and full-system tests.

  • How to implement dependency injection using StructureMap or Windsor

  • How to work with the domain and presentation model

  • How to work with persistence layers like NHibernate

The book's many examples are in C#.

"Shows how to put all the features of ASP.NET MVC together to build a great application."
-From the Foreword by Phil Haack, Senior Program Manager, ASP.NET MVC Team, Microsoft

"This book put me in control of ASP.NET MVC."
-Mark Monster, Software Engineer, Rubicon

"Of all the offerings, this one got it right!"
-Andrew Siemer, Principal Architect, OTX Research

"Highly recommended for those switching from Web Forms to MVC."
-Frank Wang, Chief Software Architect, DigitalVelocity LLC

Table of Contents

  1. Copyright
  2. Foreword
  3. Preface
  4. Acknowledgments
    1. Jeffrey Palermo
    2. Ben Scheirman
    3. Jimmy Bogard
  5. About this Book
    1. Who should read this book?
    2. Roadmap
    3. Code conventions and downloads
    4. Author Online
  6. About the Authors
    1. About the technical reviewers
  7. About the Cover Illustration
  8. 1. Getting started with the ASP.NET MVC Framework
    1. 1.1. Picking apart the default application
      1. 1.1.1. Creating the project
      2. 1.1.2. Your first routes
      3. 1.1.3. Running with the starter project
    2. 1.2. Your first ASP.NET MVC controller from scratch
    3. 1.3. Our first view
    4. 1.4. Ensuring the application is maintainable
    5. 1.5. Testing controller classes
    6. 1.6. Summary
  9. 2. The model in depth
    1. 2.1. Understanding the basics of domain-driven design
    2. 2.2. Domain model for this book
      1. 2.2.1. Key entities and value objects
      2. 2.2.2. Aggregates
      3. 2.2.3. Persistence for the domain model
    3. 2.3. Presentation model
      1. 2.3.1. Presentation model responsibilities
      2. 2.3.2. Projecting from the domain model
    4. 2.4. Working with the model
      1. 2.4.1. Crafting the route
      2. 2.4.2. Crafting the controller action
      3. 2.4.3. Test-driving the feature
      4. 2.4.4. Finishing the view
    5. 2.5. Summary
  10. 3. The controller in depth
    1. 3.1. The controller action
    2. 3.2. Simple controllers do not need a view
    3. 3.3. Testing controllers
      1. 3.3.1. Testing the RedirectController
      2. 3.3.2. Making dependencies explicit
      3. 3.3.3. Using test doubles, such as stubs and mocks
      4. 3.3.4. Elements of a good controller unit test
    4. 3.4. Simple actions and views
    5. 3.5. Working with form values
    6. 3.6. Processing querystring parameters
    7. 3.7. Binding more complex objects in action parameters
    8. 3.8. Options for passing ViewData
    9. 3.9. Filters
    10. 3.10. Summary
  11. 4. The view in depth
    1. 4.1. How ASP.NET MVC views differ from Web Forms
    2. 4.2. Folder structure and view basics
    3. 4.3. Overview of view basics
      1. 4.3.1. Examining the IViewEngine abstraction
      2. 4.3.2. Understanding master pages in the ASP.NET MVC Framework
      3. 4.3.3. Using ViewData to send objects to a view
      4. 4.3.4. Partial views can help decompose a complex screen
    4. 4.4. Leveraging the view to create dynamic screens
      1. 4.4.1. Rendering forms with view helpers and data binding
      2. 4.4.2. Posting HTML forms back to the server
      3. 4.4.3. Validation and error reporting
      4. 4.4.4. Extending HtmlHelper
    5. 4.5. Summary
  12. 5. Routing
    1. 5.1. What are routes?
      1. 5.1.1. What’s that curl command?
      2. 5.1.2. Taking back control of the URL with routing
    2. 5.2. Designing a URL schema
      1. 5.2.1. Make simple, clean URLs
      2. 5.2.2. Make hackable URLs
      3. 5.2.3. Allow URL parameters to clash
      4. 5.2.4. Keep URLs short
      5. 5.2.5. Avoid exposing database IDs wherever possible
      6. 5.2.6. Consider adding unnecessary information
    3. 5.3. Implementing routes in ASP.NET MVC
      1. 5.3.1. URL schema for an online store
      2. 5.3.2. Adding a custom static route
      3. 5.3.3. Adding a custom dynamic route
      4. 5.3.4. Catch-all routes
    4. 5.4. Using the routing system to generate URLs
    5. 5.5. Creating routes for Code Camp Server
    6. 5.6. Testing route behavior
    7. 5.7. Using routing with existing ASP.NET projects
    8. 5.8. Summary
  13. 6. Customizing and extending the ASP.NET MVC Framework
    1. 6.1. Extending URL routing
    2. 6.2. Creating your own ControllerFactory
      1. 6.2.1. The ControllerFactory implementation
      2. 6.2.2. Leveraging IoC for your controllers
        1. A Castle Windsor ControllerFactory
        2. A StructureMap Controller Factory
    3. 6.3. Extending the controller
      1. 6.3.1. Creating a FormattableController
      2. 6.3.2. Working with action filters
    4. 6.4. Creating a custom view engine
    5. 6.5. Customizing Visual Studio for ASP.NET MVC
      1. 6.5.1. Creating custom T4 templates
      2. 6.5.2. Adding a custom test project template to the new project wizard
    6. 6.6. Summary
  14. 7. Scaling the architecture for complex sites
    1. 7.1. Taming large controller actions
    2. 7.2. Whipping views into shape
      1. 7.2.1. Using and creating view helpers
      2. 7.2.2. Creating partials
      3. 7.2.3. Creating components
    3. 7.3. Using action filters to load common data
    4. 7.4. Organizing controllers into areas
      1. 7.4.1. Capturing the area for a request
      2. 7.4.2. Creating a view engine with support for areas
      3. 7.4.3. Tying it all together
    5. 7.5. Summary
  15. 8. Leveraging existing ASP.NET features
    1. 8.1. ASP.NET server controls
      1. 8.1.1. The TextBox
      2. 8.1.2. Other common controls
      3. 8.1.3. The GridView
      4. 8.1.4. Where do I get the good stuff?
    2. 8.2. State management
      1. 8.2.1. Caching
      2. 8.2.2. Session state
      3. 8.2.3. Cookies
      4. 8.2.4. Request storage
    3. 8.3. Tracing and debugging
      1. 8.3.1. TraceContext
      2. 8.3.2. Health monitoring
    4. 8.4. Implementing personalization and localization
      1. 8.4.1. Leveraging ASP.NET personalization
      2. 8.4.2. Leveraging ASP.NET localization
    5. 8.5. Implementing ASP.NET site maps
    6. 8.6. Summary
  16. 9. AJAX in ASP.NET MVC
    1. 9.1. Diving into AJAX with an example
    2. 9.2. AJAX with ASP.NET Web Forms
    3. 9.3. AJAX in ASP.NET MVC
      1. 9.3.1. Hijaxing Code Camp Server
      2. 9.3.2. AJAX with JSON
      3. 9.3.3. Adding alternate view formats to the controller
      4. 9.3.4. Consuming a JSON action from the view
      5. 9.3.5. AJAX helpers
    4. 9.4. Summary
  17. 10. Hosting and deployment
    1. 10.1. Deployment scenarios
    2. 10.2. XCOPY deployment
    3. 10.3. Deploying to IIS 7
    4. 10.4. Deploying to IIS 6 and earlier
      1. 10.4.1. Configuring routes to use the .aspx extension
      2. 10.4.2. Configuring routes to use a custom extension
      3. 10.4.3. Using wildcard mapping with selective disabling
      4. 10.4.4. Using URL rewriting
    5. 10.5. Automating deployments
      1. 10.5.1. Employing continuous integration
      2. 10.5.2. Enabling push-button XCOPY deployments
      3. 10.5.3. Managing environment configurations
    6. 10.6. Summary
  18. 11. Exploring MonoRail and Ruby on Rails
    1. 11.1. MonoRail
      1. 11.1.1. Feature overview
        1. Filters
        2. Flash
        3. Rescues
      2. 11.1.2. ActiveRecord and Windsor
        1. ActiveRecord
        2. Castle Windsor
      3. 11.1.3. MonoRail and Castle features available in ASP.NET MVC
    2. 11.2. Ruby on Rails
      1. 11.2.1. Convention over configuration and “the Rails way”
      2. 11.2.2. Active Record
      3. 11.2.3. ActionPack
    3. 11.3. Summary
  19. 12. Best practices
    1. 12.1. Controllers
      1. 12.1.1. Layer Supertype
      2. 12.1.2. Filters
      3. 12.1.3. Smart binders
      4. 12.1.4. Hardcoded strings
      5. 12.1.5. Separated view models
      6. 12.1.6. Validation
    2. 12.2. Views
      1. 12.2.1. Strongly typed views
      2. 12.2.2. Fighting duplication
      3. 12.2.3. Embracing expressions
    3. 12.3. Routes
      1. 12.3.1. Testing routes
      2. 12.3.2. Action naming
    4. 12.4. Testing
      1. 12.4.1. Controller unit tests
      2. 12.4.2. Model binder unit tests
      3. 12.4.3. Action filter unit tests
      4. 12.4.4. Testing the last mile with UI tests
    5. 12.5. Summary
  20. 13. Recipes
    1. 13.1. jQuery autocomplete text box
    2. 13.2. Automatic client-side validation
    3. 13.3. Data access with NHibernate
      1. 13.3.1. Functional overview of reference implementation
      2. 13.3.2. Application architecture overview
      3. 13.3.3. Domain model—the application core
      4. 13.3.4. NHibernate configuration—infrastructure of the application
        1. Like Any .Net Application, NHibernate Needs Configuration
        2. The NHibernate Mapping Files—Simple but powerful
        3. Initializing the Configuration
      5. 13.3.5. UI leverages domain model
      6. 13.3.6. Pulling it together
      7. 13.3.7. Wrapping up data access with NHibernate
    4. 13.4. Designing views with the Spark view engine
      1. 13.4.1. Installing and configuring Spark
      2. 13.4.2. Simple Spark view example
    5. 13.5. Summary