Mastering the .NET Code Model

40
Copyright © 2006-2008. Iron Speed Inc. All rights reserved Mastering the .NET Code Model Visit us: www.ironspeed.com Download the Free Edition: www.ironspeed.com/download

description

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.

Transcript of Mastering the .NET Code Model

Page 1: Mastering the .NET Code Model

Copyright © 2006-2008. Iron Speed Inc. All rights reserved

Mastering the .NET Code Model

Visit us: www.ironspeed.com Download the Free Edition: www.ironspeed.com/download

Page 2: Mastering the .NET Code Model

2

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

ASP.NET Code Model

Single-File Page Model

Layout and Code in one ASPX file

Used only for the simplest applications

Page 3: Mastering the .NET Code Model

3

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

ASP.NET Code Model

Code-Behind Page Model

Most common in practice

Separate Layout and Code

Layout in ASPX

Code-behind in .VB or .CS

No performance difference

Allows re-use of common code

Designers can design layout, while developers can write code

Best practice for an N-tier application

Page 4: Mastering the .NET Code Model

4

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Code-Behind

Code-Behind is specified in the ASPX

Page 5: Mastering the .NET Code Model

5

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Code-Behind

Code-Behind is derived from the Page class (or another subclass of Page class)

Page 6: Mastering the .NET Code Model

6

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Best Practices - .NET Code Model

Use Code-Behind Page Model for extensibility

Page 7: Mastering the .NET Code Model

Copyright © 2006-2008. Iron Speed Inc. All rights reserved

Page Lifecycle

Page 8: Mastering the .NET Code Model

8

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Page Lifecycle

Sequence of steps a page goes through

Initialize

Load the user interface controls

Read data from the database

Display the page

And later…

Validate data

Save data into database

Redirect back to calling page

etc.

Page 9: Mastering the .NET Code Model

9

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Page Lifecycle

.NET handles

Loading of the page

Converting ASP controls to HTML controls

Rendering controls on the page

But at key points during the page lifecycle, .NET raises events for you to do your work

Your job: Handle various events to load data from database, initialize content in controls

Page 10: Mastering the .NET Code Model

10

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

.NET’s Event-based Model

Program by responding to events

Write an event handler

Initial page display has three events

User actions on page (e.g., button click) has one additional event

Page 11: Mastering the .NET Code Model

11

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Page 12: Mastering the .NET Code Model

12

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

After Displaying a Page

Server releases the page instance

Nothing about the page or the data on the page is kept on the server

Any state information must be maintained by you in “ViewState”

Page 13: Mastering the .NET Code Model

13

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

What is a Postback?

User’s action causes a Postback

An event is raised

Page instance is re-created

UI Control data is repopulated from ViewState

Server-side event handler is called

Examples

Button click

Selected item in dropdown

Typed text in a textbox

Must register Event Handler to request a postback

Page 14: Mastering the .NET Code Model

14

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

User Actions cause “Postback”

Four events raised during Postback

Registered Event Handler called between Load and PreRender

Page 15: Mastering the .NET Code Model

15

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Page 16: Mastering the .NET Code Model

16

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

During Postback

Check IsPostback to determine whether in Postback state

Performance Tip: Only load data during initial display (Not IsPostback)

Page 17: Mastering the .NET Code Model

17

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

State Management

ViewState is a standard feature of .NET

By default, state information maintained in HTML sent to browser

Encoded, but not encrypted

Page 18: Mastering the .NET Code Model

18

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

State Management

ViewState can be optionally saved in Session, Cache, File or Database - but:

Additional work required

Session or Cache can get timed out

File or Database requires regular cleanup

Bloats the page content, but best to leave it as Page

Page 19: Mastering the .NET Code Model

19

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Best Practices – Page Lifecycle

Do most of the work by handling Load event

Init should be used only for initializing event handlers

PreRender should only be used to handle dependent controls

Security for a page should be checked in Load

Use IsPostback extensively to improve performance and minimize loading data from database

Use “Page” ViewState – even though it sends lots of data to the browser

Page 20: Mastering the .NET Code Model

Copyright © 2006-2008. Iron Speed Inc. All rights reserved

Ajax

Page 21: Mastering the .NET Code Model

21

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Using Ajax

ASP.NET Ajax creates fast and flicker-free updates

Extremely simple to implement –

Just surround ASP Controls with UpdatePanel

Only sends back content that has changed

Browser only paints changed content

Can have multiple and/or nested UpdatePanels

Code-behind does not change when using Ajax

Page 22: Mastering the .NET Code Model

22

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Using Ajax

Can show progress using UpdateProgress

Use animated GIF image

Page 23: Mastering the .NET Code Model

23

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Best Practices – Ajax

Best to use a single UpdatePanel

Using multiple UpdatePanels may require special handling when updating dependent information

Page 24: Mastering the .NET Code Model

Copyright © 2006-2008. Iron Speed Inc. All rights reserved

Master Pages

Page 25: Mastering the .NET Code Model

25

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Master Pages

Creates a consistent layout

Centralizes common functionality

Easier to maintain

Typical use:

Master Page contains:

Styles

Header, Menu and Footer

Individual page contains:

Content

Page 26: Mastering the .NET Code Model

26

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Master Pages

Page 27: Mastering the .NET Code Model

27

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Master Pages

.NET merges Master pages with Page Content

Master pages can inherit from other master pages

Page 28: Mastering the .NET Code Model

28

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Best Practices – Master Pages

Create multiple Master Pages for different needs

Nest Master Pages if needed

Include all CSS Styles, Javascript, etc. in Master Pages

Keep Page Content focused on actual data

Page 29: Mastering the .NET Code Model

Copyright © 2006-2008. Iron Speed Inc. All rights reserved

N-Tier Applications

Page 30: Mastering the .NET Code Model

30

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

N-Tier Application

Divide application into N-Tiers

Layout in ASPX pages

UI Code in Code-Behind code files

Business Layer

Data Access Layer

Stored Procedures

Page 31: Mastering the .NET Code Model

31

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

N-Tier Application

Page 32: Mastering the .NET Code Model

32

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Best Practices – N-Tier Application

Changes to layout should not impact Code-Behind

Changes to Database Schema should have minimal impact on UI Layer

Data Access Layer must use Strongly Typed objects

Paging must be done in the database, not in memory

ASP GridView, ListView etc. do paging in memory

Concurrency handling is important in highly asynchronous applications

Page 33: Mastering the .NET Code Model

Copyright © 2006-2008. Iron Speed Inc. All rights reserved

Iron Speed Designer

Page 34: Mastering the .NET Code Model

34

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Iron Speed Designer

Implements Best Practices for

.NET Code Model

Page Lifecycle

Ajax

Master Pages

N-Tier Applications

Lets you focus on what is most important

Your business logic

Use the .NET Code Model knowledge to extend

Page 35: Mastering the .NET Code Model

35

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Three Recent Game Changing Enhancements

Layout Customizations

Use Spreadsheet Grid

Toolbox – Drag and drop

Easily add pre-configured panels and controls

Code Customizations

Use Excel-like Formulas

Page 36: Mastering the .NET Code Model

36

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Page Layout Customization

Spreadsheet shows a ‘logical representation’ of page section

Drag and drop to arrange controls

Designer converts page layout to HTML (ASPX)

(You don’t have to know any HTML or ASPX!)

Cell Editor allows fine-grained tuning and formatting

- Add any HTML or ASPX

- Change font, bold, italics, alignment, cell borders, cell width

Page 37: Mastering the .NET Code Model

37

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Using Toolbox Controls

Drag and drop controls onto your Web page

Toolbox controls are databound – there’s nothing to hook up!

- Fields, labels and entire panels!

Page 38: Mastering the .NET Code Model

38

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Formula Language

Game Changing Enhancement to Version7.0

Excel-like

Most common tasks can be done – 75 to 80%

No need to know VB.NET or C#

Easy to extend – local and global extensions

Page 39: Mastering the .NET Code Model

39

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Key Questions

What is your time worth?

What is the ROI?

Page 40: Mastering the .NET Code Model

40

Copyright © 2006-2008. Iron Speed® Inc. All rights reserved

Questions?