Essential .NET
February 4th-8th
Boston

February 18th-22nd
Orlando
March 3rd-7th
Los Angeles


NEW- Essential Windows Communication Foundation
February 4th-7th
New York

February 26th-29th
Boston


NEW-Essential Windows Presentation Foundation
January 14th-18th
New York

January 28th-February 1st
Boston

February 18th-22nd
Bellevue

March 10th-14th
Los Angeles


Essential ASP.NET
January 28th-February 1st
New York

February 11th-15th
Los Angeles

February 25th-29th
Orlando


NEW- Essential Windows WorkFlow Foundation
February 11th-15th
Los Angeles

March 10th-14th
Boston


.NET Architecture &
Design
February 4th-8th
Boston

February 25th-29th
Chicago


NEW-Essential SharePoint 2007 for Developers
January 28th-1st February
Los Angeles

February18th-22nd
New York

March 10th-14th
Boston


NEW-What's New in .NET 3.0
February 12th-15th
Los Angeles


NEW-Essential AJAX for ASP.NET Developers
February 4th-7th
New York

February 26th-29th
Boston


Essential SQL Server for Developers
January 28th-1st February
Los Angeles
March 3rd-7th
Boston


Essential C#
February 4th-8th
Orlando

February 18th-22nd
Los Angeles
February 25th-29th
Bellevue


Essential Visual Studio Team System
January 28th-31st
Bellevue


Essential BizTalk Server
February 25th-29th
Los Angeles


Essential Windows Forms
March 3rd-6th
Boston

 










Introducing ASP.NET MVC
(Model View Controller)
by Michael Kennedy

There's big news on the horizon for ASP.NET! Scott Guthrie and team have just released the first preview of a much anticipated and entirely new application framework for ASP.NET: a true model-view-controller framework for ASP.NET called ASP.NET MVC.

This is the most significant change to the programming model for ASP.NET since its introduction in 2002 with .NET 1.0. The ASP.NET team, learning from the tough lessons of the past, made the decision that this new MVC programming model will not replace the current models, but will instead be an additional application model for ASP.NET available in Visual Studio 2008 and beyond. This will increase your options for building websites in ASP.NET, providing now three different application models to choose from:

  • ASP.NET application- introduced in VS 2002, removed in VS 2005 and revived for VS 2008.
  • ASP.NET website - introduced in VS 2005.
  • ASP.NET MVC - introduced December 2007 as a community technology preview (CTP).
  • What is MVC?
    The model-view-controller design, for those not familiar with it, is an architectural methodology that was introduced with Smalltalk (way back in 1979!) and splits an application into three separate layers:

    (M)odel - the data being manipulated. This is typically wrapped with domain-specific logic and includes some form of data access layer.

    (V)iew - the presentation of the data (UI).

    (C)ontroller - the connection between the model and view which responds to events and triggers actions in the model.

    The MVC pattern has been used successfully in building highly scalable websites for years: the HTML is the view, the controller is the code generating the HTML and the data is typically XML or some persistent storage holding the raw information being rendered.

    Why formally add it to ASP.NET?
    Up to now, programmers have been forced to build a significant amount of code around the raw ASP.NET engine to utilize the MVC design pattern. With the introduction of this new programming model, you can now skip all that work and build directly on an extensible framework provided by Microsoft!

    Learning a new framework is a challenge, so it’s useful to look at what this MVC model will provide over the existing models you already know and love in ASP.NET:

    1. MVC enables clean separation of concerns (data vs. UI). This makes building testable and even test-driven websites easy. It allows the UI to change without affecting the data handling, and the underlying data to be reorganized without significantly impacting the user interface.
    2. MVC is highly extensible. For example, ASP.NET page templates (familiar from the existing models) are just one of the options for rendering views. You could choose another view engine such as NVelocity.
    3. MVC has a powerful URL mapping engine. This allows for cleaner URLs. For example, we can now express the purchase of an item as /Products/Buy/14 rather than /Products.aspx?action=Buy&ItemID=14. This gives you far more control over how URLs are mapped to logic.

    In addition to these fundamental design principles, MVC has deep support for existing ASP.NET concepts such as master pages, themes, membership providers, etc.

    Architecture
    The architecture of ASP.NET MVC is fairly straightforward however, the details of how the pieces are wired together takes some exploring. When implemented correctly, the ASP.NET MVC architecture should strongly resemble Martin Fowler's Passive View design pattern.

    First, let's look at the major components of the framework as we trace a request from start to finish.


    1. A request is made using a "clean" URL: /Products/Details/3.
    2. The request is mapped to a controller using the Url Routing Engine, which maps this URL to the ProductsController class.
    3. An instance of that controller is created for the request using a ControllerFactory class.
    4. The framework then maps the URL to an action method of the controller. Here the second part of the URL “/Details” maps to the Details method on the controller class.
    5. The Details method interacts with the business layer, data models, etc to generate the requisite data to render the view.
    6. The Details method selects the correct view to render the response.
    7. The Details method generates a data-transfer-object (DTO) to send to the view.
    8. The view uses the DTO to render an HTML response to the user.

    To stay true to the Passive View design pattern, the view should not interact with the model or other business layer components. It must only use the DTO data to generate HTML. This can take some diligence when first programming with MVC.

    Understanding the Project Structure
    Now it's time to see this architecture reflected in the Visual Studio project structure and discuss how the controllers and views are tied together.



    There are three top level folders.

    1. Controllers
    2. Views
    3. Models

    In the controllers folder, we see classes that are the controllers, e.g. PlaceOrderController. In the views folder, we have a little more structure. Each controller has a subfolder, e.g. Views/PlaceOrder. Within that folder, we have the views used by the PlaceOrder controller. There is also a Views/Shared folder for things like master pages.

    A Little Less Talk and A Lot More Action - ControllerAction That Is
    Let's put it all together by defining a controller, controller action, and render the result in a view.

    First we define a controller action to map to /PlaceOrder/ViewCart.



    Notice the ControllerAction attribute, reminiscent of WCF and ASP.NET Web Services. The action computes the information needed by the view to display the current cart contents, then creates a DTO (ViewCartData) to pass to the view which is done by calling RenderView.

    The naming conventions tie this together in the framework: when the PlaceOrderController class calls RenderView("ViewCart"), MVC routes the call to the Views/PlaceOrder/ViewCart.aspx template.

    On the view side we have two parts, the template and the code-behind. Both should only interact with the DTO to generate the final HTML for the UI. Notice in the code behind we inherit from ViewPage<ViewCartData>. This maps the DTO to this.ViewData on the view.

    View Template:
     

    View CodeBehind: 



    Conclusion
    ASP.NET MVC is an interesting new application architecture for ASP.NET. It enables a new class of web applications - well factored and test driven. The current release is just a CTP (Community Technology Preview) and will likely change as it evolves, but keep your eye on MVC -- it's expected to be released before the next version of Visual Studio.

    One final note: If you want to try ASP.NET MVC for yourself, you need to have VS 2008 installed and additionally install the ASP.NET 3.5 Extensions Preview. Then just choose File->New Project->Web and you're on your way!

    To contact Michael Kennedy, please send email to: michaelk@develop.com

     

    For more information email: onsites@develop.com or call 800.699.1932
     
    Featured New Course

    NEW- Guerrilla Enterprise .NET - Connecting WCF, Workflow, BizTalk 2006 R2 & More...
    March 17th-21st in Los Angeles

    What you’ll learn…
    This course presents common strategies in application design as it applies to .NET 3.0 and Visual Studio Orcas. Using WCF, WF and BizTalk you explore how to integrate applications, explain why certain technologies are not suitable for particular types of applications, and demonstrate how your applications benefit from effective use of design patterns.

    Highlights Include:

    • Application Design and Practices
    • Distributed Application Architecture and SOA
    • Design Patterns
    • Distributed Application Security
    • WCF Concepts and Architecture
    • Network and Queued Communication using WCF
    • WCF Security, CardSpace and Federation
    • Design by Contract, SOA and Service Factory
    • WF Concepts and Architecture
    • WF Activity model and hosting
    • Using WF for service composition
    • BizTalk Architecture and Orchestration
    • Scalability and Availability
    • Deployment and Application Monitoring


      Want to get a detailed course outline?

      CLICK HERE
      and put "Guerrilla Enterprise .NET" in the subject line.



     
    NEW- .NET: MyTeam Custom Editions

    We've developed 4 customizable .NET developer courses
    based on the experience level of your team!

    Start customizing your course today with a few simple answers. We'll recommend a custom edition that is designed for the experience level of your team. Then you can tailor the course to best support the application you're building.
    Customize now: www.develop.com/myteam
     

    Upcoming Guerrilla Events

    What You Can Expect:

    - Multiple instructors (2-5) per event

    - Extended course hours
    - A team of industry leading experts
    - Leading-edge technologies
    - Innovative course materials, including slides, labs, books, demos
    - Your own PC and development platform
    - Peer-to-peer collaboration and competition
    - An exciting, multi-media learning environment


    All Inclusive Price includes:
    5-NIGHTS HOTEL, ALL MEALS AND BEVERAGES

    ABOUT GUERRILLA EVENTS

    To get a detailed course outline for any of the Guerrilla Events listed below, please send an email to requests@develop.com and put the course name in the subject line.

    Featured Guerrilla Event

    Guerrilla .NET
    - Now includes: WCF, WPF, Workflow, LINQ and Silverlight
    February 4th-8th in Los Angeles register online or call 800.699.1932

    What you’ll learn…
    Guerrilla .NET gives you a comprehensive look at the modern .NET development. It will give you the skills and knowledge to build large reliable applications with confidence and troubleshoot them effectively.

    This course also includes cutting-edge coverage of new .NET technologies. You’ll work with Windows Communication Foundation (WCF), build GUIs with Windows Presentation Foundation (WPF) and see how Windows Workflow Foundation can help you build workflow-based applications. You’ll also get to see upcoming technologies like Language Independent Query (LINQ) and Silverlight. You’ll get a chance to write some code and see how current technologies will evolve in the near future, helping you plan effectively.

    Highlights Include:

    • Iterators and Anonymous Methods
    • Lambda Expression
    • Extension Methods
    • Implicit Typing and var
    • Dealing with Exceptions
    • Transactions
    • Threading and Concurrency
    • Memory Management
    • Using and Extending Visual Studio
    • Dealing with Production Errors using SOS and WinDBG
    • Windows Communication Foundation
    • Windows Presentation Foundation
    • Windows Workflow Foundation
    • Language Integrated Query (LINQ)
    • Silverlight


      Want to get a detailed course outline?
      CLICK HERE
      and put "Guerrilla .NET" in the subject line.

    back to top

     

    Upcoming Courses

    To get a detailed course outline for any of the courses listed below, please send an email to requests@develop.com and put the course name in the subject line.

    Essential .NET register online or call 800.699.1932
    February 4th-8th in Boston
    February 18th-22nd in Orlando
    March 3rd-7th in Los Angeles