Mastering the .NET Code Model


Published on

Learn how the .NET page life cycle works, review best practices for creating n-tier applications and discover how to use Ajax to create interactive applications.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • And another one for when a Record Control is displaying data.
  • And finally for a record control when saving data.
  • OK. So we talked about a whole lot of things. We talked about the philosophy behind Iron Speed Designer and how it operates at a different level of granularity. It allows you to operate at a page or panel level. Then we talked about how code is organized into Section 1 and Section 2, followed by the class and control hierarchy and finally we talked about the calling hierarchy. We hope this short introduction helped you understand what happens behind the scenes in an Iron Speed Designer generated application. With that, now we are ready for your questions. Please send them via Chat. Brooke will queue them up and we will try to get you an answer for each of them.
  • Mastering the .NET Code Model

    1. 1. Mastering the .NET Code Model Visit us: Download the Free Edition:
    2. 2. ASP.NET Code Model <ul><ul><li>Single-File Page Model </li></ul></ul><ul><ul><ul><li>Layout and Code in one ASPX file </li></ul></ul></ul><ul><ul><ul><li>Used only for the simplest applications </li></ul></ul></ul>
    3. 3. ASP.NET Code Model <ul><ul><li>Code-Behind Page Model </li></ul></ul><ul><ul><ul><li>Most common in practice </li></ul></ul></ul><ul><ul><ul><li>Separate Layout and Code </li></ul></ul></ul><ul><ul><ul><li>Layout in ASPX </li></ul></ul></ul><ul><ul><ul><li>Code-behind in .VB or .CS </li></ul></ul></ul><ul><ul><ul><li>No performance difference </li></ul></ul></ul><ul><ul><ul><li>Allows re-use of common code </li></ul></ul></ul><ul><ul><ul><li>Designers can design layout, while developers can write code </li></ul></ul></ul><ul><ul><ul><li>Best practice for an N-tier application </li></ul></ul></ul>
    4. 4. Code-Behind <ul><ul><li>Code-Behind is specified in the ASPX </li></ul></ul>
    5. 5. Code-Behind <ul><ul><li>Code-Behind is derived from the Page class (or another subclass of Page class) </li></ul></ul>
    6. 6. Best Practices - .NET Code Model <ul><ul><li>Use Code-Behind Page Model for extensibility </li></ul></ul>
    7. 7. Page Lifecycle
    8. 8. Page Lifecycle <ul><ul><li>Sequence of steps a page goes through </li></ul></ul><ul><ul><ul><li>Initialize </li></ul></ul></ul><ul><ul><ul><li>Load the user interface controls </li></ul></ul></ul><ul><ul><ul><li>Read data from the database </li></ul></ul></ul><ul><ul><ul><li>Display the page </li></ul></ul></ul><ul><ul><ul><li>And later… </li></ul></ul></ul><ul><ul><ul><li>Validate data </li></ul></ul></ul><ul><ul><ul><li>Save data into database </li></ul></ul></ul><ul><ul><ul><li>Redirect back to calling page </li></ul></ul></ul><ul><ul><ul><li>etc. </li></ul></ul></ul>
    9. 9. Page Lifecycle <ul><ul><li>.NET handles </li></ul></ul><ul><ul><ul><li>Loading of the page </li></ul></ul></ul><ul><ul><ul><li>Converting ASP controls to HTML controls </li></ul></ul></ul><ul><ul><ul><li>Rendering controls on the page </li></ul></ul></ul><ul><ul><li>But at key points during the page lifecycle, .NET raises events for you to do your work </li></ul></ul><ul><ul><li>Your job: Handle various events to load data from database, initialize content in controls </li></ul></ul>
    10. 10. .NET’s Event-based Model <ul><ul><li>Program by responding to events </li></ul></ul><ul><ul><li>Write an event handler </li></ul></ul><ul><ul><li>Initial page display has three events </li></ul></ul><ul><ul><li>User actions on page (e.g., button click) has one additional event </li></ul></ul>
    11. 12. After Displaying a Page <ul><ul><li>Server releases the page instance </li></ul></ul><ul><ul><li>Nothing about the page or the data on the page is kept on the server </li></ul></ul><ul><ul><li>Any state information must be maintained by you in “ViewState” </li></ul></ul>
    12. 13. What is a Postback? <ul><ul><li>User’s action causes a Postback </li></ul></ul><ul><ul><ul><li>An event is raised </li></ul></ul></ul><ul><ul><ul><li>Page instance is re-created </li></ul></ul></ul><ul><ul><ul><li>UI Control data is repopulated from ViewState </li></ul></ul></ul><ul><ul><ul><li>Server-side event handler is called </li></ul></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><ul><li>Button click </li></ul></ul></ul><ul><ul><ul><li>Selected item in dropdown </li></ul></ul></ul><ul><ul><ul><li>Typed text in a textbox </li></ul></ul></ul><ul><ul><li>Must register Event Handler to request a postback </li></ul></ul>
    13. 14. User Actions cause “Postback” <ul><ul><li>Four events raised during Postback </li></ul></ul><ul><ul><li>Registered Event Handler called between Load and PreRender </li></ul></ul>
    14. 16. During Postback <ul><ul><li>Check IsPostback to determine whether in Postback state </li></ul></ul><ul><ul><li>Performance Tip: Only load data during initial display (Not IsPostback) </li></ul></ul>
    15. 17. State Management <ul><ul><li>ViewState is a standard feature of .NET </li></ul></ul><ul><ul><li>By default, state information maintained in HTML sent to browser </li></ul></ul><ul><ul><li>Encoded, but not encrypted </li></ul></ul>
    16. 18. State Management <ul><ul><li>ViewState can be optionally saved in Session, Cache, File or Database - but: </li></ul></ul><ul><ul><ul><li>Additional work required </li></ul></ul></ul><ul><ul><ul><li>Session or Cache can get timed out </li></ul></ul></ul><ul><ul><ul><li>File or Database requires regular cleanup </li></ul></ul></ul><ul><ul><li>Bloats the page content, but best to leave it as Page </li></ul></ul>
    17. 19. Best Practices – Page Lifecycle <ul><ul><li>Do most of the work by handling Load event </li></ul></ul><ul><ul><ul><li>Init should be used only for initializing event handlers </li></ul></ul></ul><ul><ul><ul><li>PreRender should only be used to handle dependent controls </li></ul></ul></ul><ul><ul><ul><li>Security for a page should be checked in Load </li></ul></ul></ul><ul><ul><li>Use IsPostback extensively to improve performance and minimize loading data from database </li></ul></ul><ul><ul><li>Use “Page” ViewState – even though it sends lots of data to the browser </li></ul></ul>
    18. 20. Ajax
    19. 21. Using Ajax <ul><ul><li>ASP.NET Ajax creates fast and flicker-free updates </li></ul></ul><ul><ul><li>Extremely simple to implement – </li></ul></ul><ul><ul><ul><li>Just surround ASP Controls with UpdatePanel </li></ul></ul></ul><ul><ul><li>Only sends back content that has changed </li></ul></ul><ul><ul><li>Browser only paints changed content </li></ul></ul><ul><ul><li>Can have multiple and/or nested UpdatePanels </li></ul></ul><ul><ul><li>Code-behind does not change when using Ajax </li></ul></ul>
    20. 22. Using Ajax <ul><ul><li>Can show progress using UpdateProgress </li></ul></ul><ul><ul><li>Use animated GIF image </li></ul></ul>
    21. 23. Best Practices – Ajax <ul><ul><li>Best to use a single UpdatePanel </li></ul></ul><ul><ul><ul><li>Using multiple UpdatePanels may require special handling when updating dependent information </li></ul></ul></ul>
    22. 24. Master Pages
    23. 25. Master Pages <ul><ul><li>Creates a consistent layout </li></ul></ul><ul><ul><li>Centralizes common functionality </li></ul></ul><ul><ul><li>Easier to maintain </li></ul></ul><ul><ul><li>Typical use: </li></ul></ul><ul><ul><ul><li>Master Page contains: </li></ul></ul></ul><ul><ul><ul><ul><li>Styles </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Header, Menu and Footer </li></ul></ul></ul></ul><ul><ul><ul><li>Individual page contains: </li></ul></ul></ul><ul><ul><ul><ul><li>Content </li></ul></ul></ul></ul>
    24. 26. Master Pages
    25. 27. Master Pages <ul><ul><li>.NET merges Master pages with Page Content </li></ul></ul><ul><ul><li>Master pages can inherit from other master pages </li></ul></ul>
    26. 28. Best Practices – Master Pages <ul><ul><li>Create multiple Master Pages for different needs </li></ul></ul><ul><ul><li>Nest Master Pages if needed </li></ul></ul><ul><ul><li>Include all CSS Styles, Javascript, etc. in Master Pages </li></ul></ul><ul><ul><li>Keep Page Content focused on actual data </li></ul></ul>
    27. 29. N-Tier Applications
    28. 30. N-Tier Application <ul><ul><li>Divide application into N-Tiers </li></ul></ul><ul><ul><ul><li>Layout in ASPX pages </li></ul></ul></ul><ul><ul><ul><li>UI Code in Code-Behind code files </li></ul></ul></ul><ul><ul><ul><li>Business Layer </li></ul></ul></ul><ul><ul><ul><li>Data Access Layer </li></ul></ul></ul><ul><ul><ul><li>Stored Procedures </li></ul></ul></ul>
    29. 31. N-Tier Application
    30. 32. Best Practices – N-Tier Application <ul><ul><li>Changes to layout should not impact Code-Behind </li></ul></ul><ul><ul><li>Changes to Database Schema should have minimal impact on UI Layer </li></ul></ul><ul><ul><li>Data Access Layer must use Strongly Typed objects </li></ul></ul><ul><ul><li>Paging must be done in the database, not in memory </li></ul></ul><ul><ul><ul><li>ASP GridView, ListView etc. do paging in memory </li></ul></ul></ul><ul><ul><li>Concurrency handling is important in highly asynchronous applications </li></ul></ul>
    31. 33. Iron Speed Designer
    32. 34. Iron Speed Designer <ul><ul><li>Implements Best Practices for </li></ul></ul><ul><ul><ul><li>.NET Code Model </li></ul></ul></ul><ul><ul><ul><li>Page Lifecycle </li></ul></ul></ul><ul><ul><ul><li>Ajax </li></ul></ul></ul><ul><ul><ul><li>Master Pages </li></ul></ul></ul><ul><ul><ul><li>N-Tier Applications </li></ul></ul></ul><ul><ul><li>Lets you focus on what is most important </li></ul></ul><ul><ul><ul><li>Your business logic </li></ul></ul></ul><ul><ul><ul><li>Use the .NET Code Model knowledge to extend </li></ul></ul></ul>
    33. 35. Three Recent Game Changing Enhancements <ul><li>Layout Customizations </li></ul><ul><ul><li>Use Spreadsheet Grid </li></ul></ul><ul><li>Toolbox – Drag and drop </li></ul><ul><ul><li>Easily add pre-configured panels and controls </li></ul></ul><ul><li>Code Customizations </li></ul><ul><ul><li>Use Excel-like Formulas </li></ul></ul>
    34. 36. Page Layout Customization <ul><ul><li>Spreadsheet shows a ‘logical representation’ of page section </li></ul></ul><ul><ul><li>Drag and drop to arrange controls </li></ul></ul><ul><ul><li>Designer converts page layout to HTML (ASPX) (You don’t have to know any HTML or ASPX!) </li></ul></ul><ul><ul><li>Cell Editor allows fine-grained tuning and formatting </li></ul></ul><ul><ul><ul><li>Add any HTML or ASPX </li></ul></ul></ul><ul><ul><ul><li>Change font, bold, italics, alignment, cell borders, cell width </li></ul></ul></ul>
    35. 37. Using Toolbox Controls <ul><ul><li>Drag and drop controls onto your Web page </li></ul></ul><ul><ul><li>Toolbox controls are databound – there’s nothing to hook up! </li></ul></ul><ul><ul><ul><li>Fields, labels and entire panels! </li></ul></ul></ul>
    36. 38. Formula Language <ul><li>Game Changing Enhancement to Version7.0 </li></ul><ul><ul><li>Excel-like </li></ul></ul><ul><ul><li>Most common tasks can be done – 75 to 80% </li></ul></ul><ul><ul><li>No need to know VB.NET or C# </li></ul></ul><ul><ul><li>Easy to extend – local and global extensions </li></ul></ul>
    37. 39. Key Questions <ul><li>What is your time worth? </li></ul><ul><li>What is the ROI? </li></ul>
    38. 40. Questions?