OOP and Web Applications OOP and Web Applications
Michael JuMichael JuřekřekArchitect EvangelistArchitect EvangelistMicrosoft CorporationMicrosoft Corporation
AgendaAgenda
Web development – overall contextWeb development – overall context OOP Principles and OOP Principles and ASP.NETASP.NET ASP.NETASP.NET object internals object internals
Lifetime issuesLifetime issues Maintaing stateMaintaing state Handling eventsHandling events
Desktop DevelopmentDesktop Development OOPOOP oriented for years oriented for years Great toolsGreat tools (Visual Basic, Delphi, …) (Visual Basic, Delphi, …) Visual composition of application UIVisual composition of application UI Developer wires the objects through Developer wires the objects through
interaction via eventsinteraction via events Lots of components availableLots of components available::
Packaged with a toolPackaged with a tool For freeFor free For moneyFor money Grow your ownGrow your own
Only drawbackOnly drawback = COST OF DEPLOYMENT= COST OF DEPLOYMENT
ASPASP / PHP – fast facts / PHP – fast facts Revolutionary at their timeRevolutionary at their time (1996) (1996) Most popular web development Most popular web development
environmentsenvironments LineLinear programming modelar programming model ( (up-down)up-down) Hard to maintain code, mixing of logical Hard to maintain code, mixing of logical
layers, “spaghetti code”layers, “spaghetti code” Impossible to provide real development Impossible to provide real development
environment (-> expensive development)environment (-> expensive development) Other technologies suffer from similar Other technologies suffer from similar
painspains (JSP, PHP, Cold Fusion) (JSP, PHP, Cold Fusion)
ASP.NET ASP.NET – goals – goals Be object orientedBe object oriented Mimic desktop paradigmMimic desktop paradigm - - “Visual Basic “Visual Basic
for Web”for Web” Much less lines of codeMuch less lines of code ( (reduced costsreduced costs)) Keep application layers distinctKeep application layers distinct
((presentation, logicpresentation, logic)) Native, no scriptingNative, no scripting ( (performance !!!performance !!!)) „„FullFull“ “ languageslanguages - Visual Basic, C#, - Visual Basic, C#,
JScriptJScript®®
Abitilities:Abitilities: Reliable, Maintainable, Scalabled, DeployableReliable, Maintainable, Scalabled, Deployable
AgendaAgenda
Web development – overall contextWeb development – overall context OOP Principles and OOP Principles and ASP.NETASP.NET ASP.NETASP.NET object internals object internals
Lifetime issuesLifetime issues Maintaing stateMaintaing state Handling eventsHandling events
ASP.NET pages:ASP.NET pages: PrinciplesPrinciples
Server-side controlsServer-side controls Encapsulate visual elements, interact with userEncapsulate visual elements, interact with user Generate eventsGenerate events ( (value has changed, button value has changed, button
was pressed, mouse clickwas pressed, mouse click, …), …)
Execution is not linearExecution is not linear !!!!!! Event fired in every execution phaseEvent fired in every execution phase Init, Load, Render, Unload, etcInit, Load, Render, Unload, etc..
CodeCode hookedhooked on eventson events Executed during processing or by user actionExecuted during processing or by user action May be part of ASP.NET page, in separate file May be part of ASP.NET page, in separate file
or separate dll libraryor separate dll library
Server-side controlsServer-side controls PagePage class represents web pageclass represents web page
Hierarchy of controlsHierarchy of controls Page Page is the root of hierarchical treeis the root of hierarchical tree Even static text is Even static text is LiteralControlLiteralControl object behind the object behind the
scenesscenes
At the end of processingAt the end of processing Page.Page.Render()Render() is being is being calledcalled Propagated down the tree of controlsPropagated down the tree of controls Each control generates its HTML representationEach control generates its HTML representation Multiple client technologies may be supportedMultiple client technologies may be supported
DHTML, HTML 3.2, WML, DHTML, HTML 3.2, WML, atdatd..
Fully customizable look of controlFully customizable look of control Total control with the help ofTotal control with the help of TeTemplatemplate classclass
OOP: OOP: InheritanceInheritance All controls inherit from abstractAll controls inherit from abstract
WebControlWebControl and and Control Control classesclasses Page Page itself inherits from itself inherits from ControlControl, too, too Inheritance is everywhere, e.g.Inheritance is everywhere, e.g.::
ControlControl ( (genericgeneric, Children, Parent, …), Children, Parent, …) WebControlWebControl (web style specific, width, … (web style specific, width, …))
ListControlListControl ( (working with lists, Items, …working with lists, Items, …)) DropDownListDropDownList ListBoxListBox
OOP: OOP: EncapsulationEncapsulation Every control is fully responsible for Every control is fully responsible for
behaving as expectedbehaving as expected E.gE.g. . it must render HTML that works in common it must render HTML that works in common
web browsersweb browsers
How to change control’s behavior?How to change control’s behavior? By setting propertiesBy setting properties By calling methodsBy calling methods By setting external objectsBy setting external objects (context) – (context) – but it’s a but it’s a
control’s sole responsibility how it reflects this control’s sole responsibility how it reflects this informationinformation
OOP: PolymorOOP: Polymorphismphism ControlControl defines basic behavioral defines basic behavioral
characteristics by defining methods as characteristics by defining methods as virtual (overridable)virtual (overridable)
ExamplesExamples:: Render()Render() LoadViewState(), SaveViewState()LoadViewState(), SaveViewState() CreateChildControls()CreateChildControls() OnInit, OnLoad, OnUnloadOnInit, OnLoad, OnUnload ……
AgendaAgenda
Web development – overall contextWeb development – overall context OOP Principles and OOP Principles and ASP.NETASP.NET ASP.NETASP.NET object internals object internals
Lifetime issuesLifetime issues Maintaing stateMaintaing state Handling eventsHandling events
Object lifetimeObject lifetime Objects cannot live in client’s browserObjects cannot live in client’s browser::
No Java AppletsNo Java Applets No ActiveX controlsNo ActiveX controls These were unsuccessful technologiesThese were unsuccessful technologies
Objects live on the serverObjects live on the server:: No persistent connection between client and No persistent connection between client and
serverserver ( (further – think about web farmfurther – think about web farm)) Live only during request processing Live only during request processing ( (~ ms)~ ms) ConsequencesConsequences::
You have to keep state somewherYou have to keep state somewher You have to relay events from client to serverYou have to relay events from client to server
Maintaining object stateMaintaining object state DatabaseDatabase??
Correct from architectural perspectiveCorrect from architectural perspective Problematic performance, often “overkill”Problematic performance, often “overkill”
Web server processWeb server process?? Memory consumption, open door for DOS Memory consumption, open door for DOS
attacksattacks What about a web farm?What about a web farm?
ClientClient?? Need of encryption and digital signaturesNeed of encryption and digital signatures Increased client/server traffic (delays, money)Increased client/server traffic (delays, money) Least evil, this is the defaultLeast evil, this is the default ““ViewStateViewState” as a hidden HTML form field” as a hidden HTML form field
Web Forms Web Forms Web page lifecycleWeb page lifecycle
Page_LoadPage_Load
Page_Page_Render, Render, UnloadUnload
Textbox1_ChangedTextbox1_Changed
Button1_ClickButton1_Click
EventsEvents
1. 1. „Change“ „Change“ eventsevents
2. 2. „„ActionAction“ “ eventsevents
Page created and initializedPage created and initialized
Page resources releasedPage resources released
Handling eventHandling event (PostBack) (PostBack)
ServerBrowser
Info processedEvent
Eventhandled
Eventinfo
Updatedpage
Calls eventhandling routine
SummarySummary
Object oriented programming Object oriented programming makes development much more makes development much more efficientefficient
Web development is no exceptionWeb development is no exception ASP.NET is the only fully OOP-ASP.NET is the only fully OOP-
oriented web development oriented web development technology available todaytechnology available today