.NET e-Business Architecture 9780672322198, 0672322196

This book is written for architects and developers preparing to design and build enterprise-scale e-business application

223 69 6MB

English Pages 463 Year 2001

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

.NET e-Business Architecture
 9780672322198, 0672322196

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

AM FL Y TE .NET e-Business Architecture David Burgett, Matthew Baute, John Pickett, Eric Brown, G. A. Sullivan

Copyright © 2002 by Sams Publishing 1st Edition, November 27, 2001

Team-Fly®

Copyright .NET e-Business Architecture Copyright (c) 2002 by Sams Publishing

All right s reserved. No part of t his book shall be reproduced, st ored in a ret rieval syst em , or t ransm it t ed by any m eans, elect ronic, m echanical, phot ocopying, recording, or ot herwise, wit hout writ t en perm ission from t he publisher. No pat ent liabilit y is assum ed wit h respect t o t he use of t he inform at ion cont ained herein. Alt hough every precaut ion has been t aken in t he preparat ion of t his book, t he publisher and aut hor assum e no responsibilit y for errors or om issions. Nor is any liabilit y assum ed for dam ages result ing from t he use of t he inform at ion cont ained herein. I nt ernat ional St andard Book Num ber: 0672322196 Print ed in t he Unit ed St at es of Am erica First Print ing: Mont h Year 04 03 02 01

4

3

2

1

Trademarks All t erm s m ent ioned in t his book t hat are known t o be t radem arks or service m arks have been appropriat ely capit alized. Sam s Publishing cannot at t est t o t he accuracy of t his inform at ion. Use of a t erm in t his book should not be regarded as affect ing t he validit y of any t radem ark or service m ark.

Warning and Disclaimer Every effort has been m ade t o m ake t his book as com plet e and as accurat e as possible, but no warrant y or fit ness is im plied. The inform at ion provided is on an "as is" basis. The aut hors and t he publisher shall have neit her liabilit y nor responsibilit y t o any person or ent it y wit h respect t o any loss or dam ages arising from t he inform at ion cont ained in t his book or from t he use of t he CD or program s accom panying it .

Credits Associate Publisher Linda Engelman

Acquisitions Editor Karen Wachs

Development Editor Grant Munroe

Managing Editor Charlotte Clapp

Project Editor Leah Kirkpatrick

Copy Editor Ned Snell

Technical Editor Beth Breidenbach, Jawahar Puvvala

Team Coordinator Lynne Williams

Media Developer Dan Scherf

Contents Introduction Who Should Use This Book? How This Book I s Organized Part I : Prelim inaries and Planning Part I I : Designing .NET e- Business Solut ions Part I I I : Building t he .NET e- Business Part I V: Test ing and Deploying .NET Sit es Convent ions Used I n This Book

Part I: Preliminaries and Planning Chapter 1 .NET--The Big Picture The I nt ernet Rules! A Level Playing Field St andards- - So Many t o Choose From Ent erprise- Level At t ribut es The Evolut ion of t he Program m ing Model Client s and Servers Applicat ions Becom e Sit es Scale Up versus Scale Out Balancing t he Load A Mat t er of St at e I ncorporat ing Services Services The Passport Service The Role of SOAP SOAP on a ROPE ( The SDK for VS) DI SCO and UDDI BizTalk: All Three The Organizat ion The I nit iat ive The Server Com plet e t he Pict ure wit h BizTalk Orchest rat ion Cast of Charact ers CLR: The Com m on Language Runt im e CLR Securit y Windows Microsoft Message Queue I nt ernet I nform at ion Server The Microsoft .NET Ent erprise Servers From Here

Chapter 2 gasTix: A Sample .NET e-Business I nt roducing gasTix: The Sam ple Client Background Problem St at em ent Vision St at em ent Solut ion Concept Business Goals Design Crit eria Archit ect ing t he Syst em The .NET Archit ect ural Foundat ion gasTix Concept ual Archit ect ure gasTix Use Cases Web Sit e Flow Hom e Page Art ist s/ Team s by Nam e Subcat egories by Cat egory Art ist s by Subcat egory St at e List / US Map Venue Search Page Cit ies wit h Venues List Venues by Cit y/ St at e Venues by Nam e Event s by Venue Event s by Art ist / Team Seat ing Chart Event Det ails Event Pricing Det ails Ticket Purchase Purchase Confirm at ion Updat e Profile Shipm ent St at us Proj ect Design and Approach: The Microsoft Solut ions Fram ework Developm ent Process Overviews A Not e on t he Sam ple Applicat ion From Here Chapter 3 Enabling Inter-Application Communications Challenges of I nt er-Applicat ion Com m unicat ions XML- - Ext ensible Markup Language XML and HTML XSD Schem a SOAP and Web Services WSDL, DI SCO, and UDDI

Microsoft BizTalk Server Business Docum ent Exchange BizTalk Orchest rat ion BizTalk Messaging From Here

Part II: Designing .NET e-Business Solutions Chapter 4 Tools of the Trade .NET Archit ect ural Tools .NET Fram ework Classes Com m on Language Runt im e ( CLR) Com m on Type Syst em ( CTS) / Com m on Language Specificat ion ( CLS) Met adat a Server I nt egrat ion and Device I ndependence Securit y and Aut hent icat ion Perform ance and Scalabilit y .NET Archit ect ural Tools Sum m ary .NET Language Tools Visual C# .NET Visual Basic .NET Language Choice Ot her Languages .NET Language Tools Sum m ary .NET Developer Product ivit y Tools Visual St udio .NET I LDasm Com m and Line Com pilers .NET Developer Product ivit y Tools Sum m ary From Here Chapter 5 Implementing .NET Presentation Services Why Should Developers Swit ch t o ASP.Net ? The Page Obj ect What are Web Cont rols? Event- Driven Program m ing Using Web Cont rols in Visual St udio.NET Advanced Feat ures of Web Cont rols User Cont rols Separat ion of UI and Code From Here

Chapter 6 Implementing .NET Business Services Archit ect ural Goals I nt eroperabilit y Perform ance Availabilit y Scalabilit y Securit y gasTI X Business Services- -A closer look Business Façade Business Dat a Dat a Access Syst em Fram ework Web Services What 's New for Com ponent Developm ent ? Rem ot ing versus DCOM ADO.NET From Here Chapter 7 Implementing .NET Data Services Dat a Modeling Relat ional versus Dim ensional Models Physical Modeling I ndexing for Perform ance Num ber of I ndexes Clust ered versus Non- Clust ered I ndexes Long or Short I ndex Keys? I ndex Tuning Wizard Transact- SQL Variables in Transact SQL Param et ers in Transact SQL Transact SQL Cursors Funct ions in Transact SQL Views Advant ages of Using Views Disadvant ages of Using Views Part it ioned Views I ndexed Views St ored Procedures Creat ing St ored Procedures St ored Procedure Perform ance I ssues Triggers AFTER Triggers I NSTEAD OF Triggers

User- Defined Funct ions I nline Table-Valued Funct ions Mult ist at em ent Table-Valued Funct ions XML Support in SQL Server Transact- SQL XML ext ensions OPENXML I nt egrat ion of SQL Server and I I S From Here Chapter 8 Handling security Securit y Concept s Aut hent icat ion and Aut horizat ion Aut hent icat ing Users Aut horizat ion - working wit h Role- Based Securit y Working wit h Code Access Securit y Signing Assem blies Code perm issions and associat ed t ools From Here Chapter 9 COM+ Integration A Legacy from t he Past The Need for Legacy Com ponent s To Migrat e or Not t o Migrat e A Sam ple Archit ect ure Technology Overview Managed versus Unm anaged Code Calling t he Operat ing Syst em Wrapping it All Up. Approaches t o Part ial Port s Advanced Topics Typed Transit ions and t he St ack Cont rolling t he Marshalling Mechanism Det ailed Walkt hrough What We Need Creat ing a Sim ple Visual Basic DLL Calling t he DLL from a Web Service Calling t he DLL from a Visual Basic.NET Class Library Calling t he Class Library from a Visual Basic Execut able From Here

Part III: Building the .NET e-Business

Chapter 10 gasTIX Event Searching Building Present at ion Services wit h ASP.NET Present at ion Service Goals ASP.NET Web Cont rols Building t he ASP.NET Proj ect The gasTI X Search Proj ect Separat ion of Code and Design in gasTI X Using ASP.NET Web Cont rols Using Repeat er Cont rols User Cont rols A Sim ple User Cont rol Building a Nest ed User Cont rol From Here Chapter 11 Purchasing a Ticket Set t ing Up t he Exam ples Web.Config Global.ASAX Middle Tier Obj ect s Versus XML Web Services Business Obj ect s Using XML Web Services via Soap Building t he gasTI X Purchasing User I nt erface Organizat ion and Flow of t he User I nt erface Event Det ails Paym ent I nform at ion Confirm ing t he Purchase Displaying a Receipt Viewing t he Seat ing Chart The UI for Purchasing a Ticket Tools Visual St udio .NET Com m and- Line t ools From Here Chapter 12 Fulfillment Com m unicat ion Overview Not ificat ion of New Orders Message Form at and St ruct ure Message Transport Message Transport Alt ernat ives Sending t he Order BTS Messaging Definit ions Creat ing t he Organizat ions and Applicat ions Creat ing t he Port

Creat ing t he Channel Form at t ing t he Order Funct oids Alt ernat ives Processing t he Order Receiving t he Order Act ivat ing Orchest rat ion from BTS Messaging TPA Orchest rat ion Overview Creat ing t he Business Process Receiving Messages in t he Schedule Creat ing t he Com ponent s Creat ing t he Com ponent Port s Mapping t he Dat a Delivery Receipt s Transport Acknowledgem ent and Ret ry BTS Queues Receipt s BTS Fram ework Reliable Delivery Lim it at ions Order St at us Ret urn and Enhanced Receipt s Transact ions in Orchest rat ion Schedules Correlat ion I m plem ent at ion Choices Updat ing Order St at us From Here Chapter 13 .NET Remoting Applicat ion Dom ains Rem ot able versus Non- Rem ot able Obj ect s Marshal- By-Value Marshal- by- Reference Channels Serializing Messages Rem ot ing Configurat ion Opt ions Host ing a Rem ot e Obj ect Host ing in a Managed .EXE or Managed Service I nt ernet I nform at ion Server ( I I S) .NET Com ponent Services Fram ework Consum ing a Rem ot e Obj ect Act ivat ion and Lifet im e Cont rol Server Act ivat ion Client Act ivat ion Obj ect Lifet im es From Here

Part IV: Testing and Deploying .NET Sites Chapter 14 Debugging a .NET Application .NET Debugging- -The Basics Debugging gasTI X At t aching t o gasTI X Web Just- I n-Tim e Debugging Trace and Debug Classes Debugging an ASP.NET Web Applicat ion ASP.NET Tracing Debugging and Test ing a Web Service Except ion Handling The Except ion Obj ect Fundam ent als Best Pract ices From Here Chapter 15 Performance Tuning in .NET

TE

AM FL Y

Perform ance Tuning Concept s Why Perform ance Tune? How t o Perform ance Tune? When To Perform ance Tune? .NET Perform ance Mechanism s ASP.NET Caching Technologies .NET Applicat ion Profiling Asynchronous Processing in .NET I nst all-Tim e Code Generat ion Ot her .NET Perform ance Considerat ions .NET Perform ance Tuning Tools Tuning t he .NET Present at ion Tier wit h Applicat ion Cent er Test ( ACT) Tuning t he .NET Business Tier wit h Visual St udio Analyzer Tuning t he .NET Dat a Tier From Here Chapter 16 Deploying the Application Put t ing t he Applicat ion Toget her The Present at ion Tier The Business Tier The Dat a Tier I nfrast ruct ure Considerat ions Technical Requirem ent s for Deploym ent Applicat ion- I m posed Requirem ent s Perform ance Considerat ions

Team-Fly®

Scaling Up Versus Scaling Out Deploym ent Plat form for gasTI X Sit es Execut ing Deploym ent s wit h Visual St udio .NET Visual St udio .NET Deploym ent Proj ect s Dissect ing t he Web Set up Proj ect Deploying a .NET Applicat ion t o t he World File Syst em Regist ry User I nt erface Cust om Act ions Creat ing Web Clust ers wit h Applicat ion Cent er About Applicat ion Cent er Preparing a Web Clust er for Use wit h .NET and ACK Deploying .NET Applicat ions on Applicat ion Cent erClust ers From Here

Appendix

Design Document

gasTI X Requirem ent s I m plem ent ed Funct ionalit y Fut ure Funct ionalit y gasTI X Use Cases Use Case - - Select Event by Cat egory Use Case - - Select Event by Venue Use Case - - Select Event by St at e Use Case - - Select Event by Art ist / Team Nam e Use Case - - Pricing/ Availabilit y Check Use Case - -View Seat ing Chart Use Case - - Purchase Ticket s gasTI X Sit e Map gasTI X Obj ect Models gasTI X Dat a Model

Foreword G. A. Sullivan sprang from hum ble beginnings as t he I BM PC was being launched- - j ust one ot her person ( who st ill works here! ) and m e writ ing code, working at t he kit chen t able. Our organizat ion has experienced dram at ic growt h in t he past 19 years ( visit our websit e for det ails) , but our m ot ivat ions, decisions, and invest m ent s are st ill colored by m y beginnings as a soft ware developer, and I st ill get excit ed when I see great new t echnology. The Microsoft .NET plat form and t oolset is excit ing, com prehensive, and powerful. We have been using Microsoft t ools and t echnologies since t he release of t heir first C com piler for t he DOS plat form . We’ve grown up wit h Microsoft , and are proud of t he st rong part nership we enj oy wit h Microsoft t oday. I n reviewing t he com prehensive scope and power of t he .NET plat form , I ’m rem inded of t hose early days, and feel t he sam e sense of excit em ent and im pending possibilit y. The .NET plat form represent s an incredible opport unit y for our indust ry t o creat e new syst em s t hat work well on t he I nt ernet and allow unprecedent ed levels of int egrat ion wit h business part ners and cust om ers, all using st andard I nt ernet prot ocols. Web services, which are described in det ail in t his book, play a key role in t he .NET st ory. Microsoft is once again “ bet t ing t he ranch” on a new init iat ive, and challenging our t hinking on what it m eans t o writ e an applicat ion or creat e a websit e. Wit h t heir own web services init iat ive, t hey have st aked out a t rue leadership posit ion in t his em erging m arket . I believe t he world will follow t heir lead. I t is im port ant t o realize, however, t hat .NET does NOT represent t echnology creat ed for it s own sake. Leveraging t echnologies such as XML and SOAP, and product s like t he collect ion of .NET Ent erprise Servers, Microsoft has provided a plat form t hat delivers real business value. This value is exem plified by im proved qualit y, rapid t im e t o m arket , and t ight er int egrat ion bet ween a com pany, it s cust om ers, and it s business part ners. So here is our sevent h book. We have t ried t o describe how t he .NET plat form can be used t o creat e pract ical, real- w orld applicat ions. I hope you enj oy it ! Greg Sullivan CEO

About the Authors G. A. Sullivan G. A. Sullivan is a global .NET applicat ion developm ent com pany focused on delivering Microsoft .NET solut ions t o m iddle m arket and Fort une 1000 com panies. G. A. Sullivan was am ong t he first in t he world t o becom e a Microsoft Gold Cert ified Part ner for E- Com m erce Solut ions and current ly has five locat ions t hat have earned t his designat ion. Since 1982, t he com pany has part nered wit h client s t o st ream line business processes and im plem ent leading- edge t echnologies t hrough com plet e st rat egic, t echnical and creat ive expert ise. G. A. Sullivan is am ong t he fast est growing privat e com panies in Am erica wit h nearly 300 soft ware developm ent professionals in six U.S. and t wo European locat ions. The com pany has ranked on t he Deloit t e & Touche Technology FAST 500 for four consecut ive years and has appeared for t hree years on I nc. m agazine’s I nc. 500. G. A. Sullivan is a recognized leader in t he t echnology indust ry, known for building com plex solut ions t hat are flexible, scalable and highly reliable. Vert ical indust ry specializat ion includes Financial Services ( banking, insurance and securit ies) , Healt hcare, Manufact uring, Governm ent and Ret ail. Technical specializat ion includes t he Microsoft Windows DNA plat form , and t he new Microsoft .NET plat form and t oolset . The com pany has high- level expert ise building E- Com m erce, Business I nt elligence, Knowledge Managem ent , Securit y and Mobile solut ions. G. A. Sullivan has built a solid t eam of indust ry leaders com m it t ed t o sharing t heir t echnical expert ise. Mem bers of t he G. A. Sullivan t eam frequent ly speak at nat ional and int ernat ional indust ry conferences. I n addit ion t o writ ing t echnical books, t he com pany publishes an ongoing whit e paper series, writ es art icles for t rade publicat ions and organizes num erous com m unit y developm ent sem inars and inform at ive roundt able discussions across t he Unit ed St at es and Europe. For addit ional inform at ion about G. A. Sullivan visit www.gasullivan.com . G. A. Sullivan 55 West Port Plaza, Suit e 100 St . Louis, Missouri 63146 Em ail: corporat [email protected]

Robert Amar As a Consult ant for G. A. Sullivan, Robert Amar has worked wit h a wide variet y of client s and indust ries t o im plem ent Microsoft plat form - based solut ions. Robert has experience in developing t radit ional obj ect- orient ed and web- based user int erfaces using a diverse set of languages and developm ent t ools. Recent ly Robert has worked on cut t ing- edge applicat ions for t he life insurance indust ry which im plem ent t he ACORD XML t ransact ion st andards for t he life insurance indust ry and use BizTalk Server as a m eans of com m unicat ing bet ween underwrit ers, agent s, and insurance com panies. Robert is an MCP in Visual Basic. Robert would like t o t hank m y fiancée Nicha Chowpaknam for all her encouragem ent and support .

As a Senior Technical Archit ect in G. A. Sullivan's e- Business Group, Matthew Baute helps client s design, im plem ent , and t une leading- edge t echnology solut ions for t he Microsoft plat form . I n addit ion t o ext ensive Windows DNA experience, Mat t hew recent ly com plet ed t he early phase of a large, dist ribut ed C# and ASP .NET proj ect involving OLAP and Digit al Dashboard t echnology. Mat t hew is an MCSD in Visual Basic and SQL Server whose aut horing credit s include a whit e paper ent it led Mult i- Tier Perform ance Tuning, and a chapt er on Visual Basic and ADO in G. A. Sullivan's previous t it le, Building Ent erprise Solut ions wit h Visual St udio 6. Mat t hew would like t o express special t hanks t o Helena, and t o Mom and Dad. As G. A. Sullivan’s Chief Technology Officer ( CTO) , Don Benage leads t he daily operat ions of t he com pany’s Technology Cent er. Specialt ies include applicat ion developm ent , e- business and I nt ernet t echnologies, and XML- based, loosely- coupled, highly scalable syst em s. Prior t o j oining G. A. Sullivan in 1995, he spent five years at Microsoft Corporat ion. Don has also direct ed several of G. A. Sullivan’s aut horing t eam s, serving as t he lead aut hor of t echnical books for Pearson’s Que and SAMS labels. These book t it les include: Building Ent erprise Solut ions wit h Visual St udio 6, and Special Edit ion Using Microsoft BackOffice ( Edit ions 1 & 2) . Don would like t o t hank Diane and Andy.

Eric Brown Eric Brown spends m ost of his t im e helping G. A. Sullivan's cust om ers and developers archit ect , develop, and design large scale e- Business syst em s. Eric is an accom plished developer wit h over eight years of experience building dist ribut ed syst em s using various languages. Eric spends t im e as a present er and aut hor educat ing bot h cust om ers and developers on .NET t echnologies . He has cont ribut ed t o t hree previous publishing effort s, including G. A. Sullivan's previous t it le, Building Ent erprise Solut ions wit h Visual St udio 6. Eric t hanks Rebecca and Em ily, as well as Larry, Curley, Moe, Shem p, The Pirat es Who Don't Do Anyt hing, and The Yodeling Vet erinarian of t he Alps.

David Burgett As a Senior Consult ant for G. A. Sullivan, David Burgett helps det erm ine t he t echnical direct ion of client s by ident ifying business challenges and educat ing and assist ing t he client wit h t he im plem ent at ion of t he t echnology best suit ed t o m eet t he challenge. David has ext ensive web developm ent experience, creat ing solut ions on bot h t he server- and client- side, wit h a variet y of languages and plat form s. David also evangelizes t echnology wit hin t he G. A. Sullivan com m unit y, wit h t opics ranging from Dynam ic HTML t o .NET developm ent . David is an act ive aut hor and t echnical present er; he has cont ribut ed t o t hree books and t wo t echnical whit e papers and has recent ly present ed at t he TechEd conference and t he Office XP launch. David is an MCSD and an MCDBA. David would like t o t hank Susan for encouraging him t o cont ribut e t o t his book, for put t ing up wit h him while he worked on it , and for st icking by him as he finished it . Also t o Sable, Sashka, and Chloe for always m aking him sm ile.

As a Managing Consult ant in G. A. Sullivan's Line of Business Financial Services, Remco Jorna helps financial inst it ut ions ident ify and design t echnology solut ions based on t he Microsoft .NET Plat form . David has ext ensive syst em s and web developm ent experience, creat ing solut ions on bot h t he server- and client- side, using different dat abase m anagem ent syst em s, languages and plat form s. Rem co is and MCSD and has recent ly com plet ed t he logical and t echnical design of t wo .NET Fram ework applicat ions for Em ployee Benefit and Knowledge Managem ent solut ions which use ASP .NET and Visual Basic .NET. As a Senior Consult ant for G. A. Sullivan, Chris Koenig works wit h client s t o help t hem discover and solve t heir business problem s using t he lat est t echnologies on t he Microsoft plat form . Chris has ext ensive soft ware developm ent experience including large scale Client- Server and Mult i- t ier Web applicat ions. Chris is also an act ive t echnical evangelist for G. A. Sullivan and helps t o educat e t heir client s and consult ant s about .NET and Web Developm ent . Chris is a Chart er Mem ber of t he MCSD com m unit y and an MCDBA. Chris would like t o t hank Laura, David, Christ ine, St ephen, and Brian for t heir pat ience and underst anding, and t o Shawn for t he opport unit y...

Cor Kroon As a Technical Lead for G. A. Sullivan's Dut ch office, Cor Kroon helps t ranslat e client s’ needs int o a t echnical design and im plem ent at ion t hat best suit s t heir business goals. Cor has ext ensive web developm ent experience, covering all layers of t he Windows DNA m odel. Cor is an MCSD in Visual Basic and web developm ent , but his experience also includes ot her plat form s and languages like UNI X and C. His recent cont ribut ions t o t he G. A. Sullivan com m unit y include t he design and im plem ent at ion of an XML- based m ult i- lingual version of t he com pany web- sit e, t arget ed t owards t he European m arket . Cor would like t o t hank his wife Jolanda and his sons Niels and Lars for t heir support and encouragem ent during his writ ing effort s. As a Senior Consult ant in G. A. Sullivan's Nashville office, Richard Lassan is focusing on learning and evangelizing about t he benefit s of t he .NET plat form . Richard’s aut horing credit s include “ASP+ Validat or Cont rols” for ASPToday, he also aut hored “ Working wit h Dat aGrids” and “ Validat ing User I nput ” in a fort hcom ing book on ASP.NET. Richard is also current ly cont ribut ing t o a fort hcom ing book on t he C# language. Richard can be reached at [email protected] Richard would like t o t hank William & Eleanor Lassan, Billy, Ed and St eve, and his wife Karen, for her pat ience and encouragem ent while cont ribut ing t o t his book.

Roger Miller As a Senior Consult ant for G. A. Sullivan, Roger Miller helps archit ect , im plem ent , and deploy ent erprise- wide solut ions for client s. Roger m anages and m ent ors large t eam s of consult ant s t o produce high qualit y, on- t im e solut ions. Roger recent ly present ed a Microsoft Solut ion Series on BizTalk across t he Midwest . Roger has worked wit h a variet y of languages and plat form s, ranging from hand held devices t o web sit es t o F- 15 fight er j et s. Roger holds bot h a B.S. and a M.S. in Com put er Science.

Roger would like t o t hank his wife Melissa and his children Hayden and Gwendolyn for t heir support and pat ience during t he developm ent / aut horing process and says, “ I prom ise I won't lock m yself in t he basem ent ever again.”

Brian Mueller Brian Mueller is a senior developer and archit ect for G. A. Sullivan who specializes in developing Web- based n- t ier business syst em s ut ilizing a variet y of t echnologies, and is present ly leading t he developm ent of a large VB.Net and ASP.NET port al solut ion. He also has ext ensive experience m igrat ing exist ing applicat ions t o new t echnologies and archit ect ures. Brian is an MCSD, MCDBA, and MCSE.

Baya Pavliashvili As a soft ware consult ant , Baya Pavliashvili has worked on a num ber of proj ect s specializing in dat abase developm ent and adm inist rat ion. Baya has aut hored art icles about developing in Transact- SQL, configuring replicat ion, building and m aint aining dat a warehouses wit h SQL Server. He has assist ed wit h edit ing and reviewing several t echnical t it les. Baya is t he president of t he Nashville SQL Server User s Group, and holds t he MCSE, MCSD, and MCDBA cert ificat ions. Baya would like t o t hank Jerry Post and Thom as Baldwin for t heir t ireless effort s, and for developing world- class professionals.

Bry Pickett Bry Pickett is a soft ware engineer wit h G. A. Sullivan and has over six years experience designing and developing dist ribut ed applicat ions using Microsoft t ools and t echnologies. He cont ribut es developm ent skills t o a wide variet y of proj ect s using various languages and plat form s including t he Microsoft .NET Ent erprise Solut ions Plat form . Bry is an MCSD, MCSE, and MCDBA focusing his work on m iddle t ier business logic and dat a access com ponent s as well as user int erface logic com ponent s.When not working, Bry enj oys spending t im e wit h his wife, Layne, t heir t wo cat s, Hana and Daisy, and reading books about t he hist ory of World War I I . Bry would like t o t hank Layne for being here, and Bet t y and John for get t ing him here.

John Pickett John Pickett has spent m ost of t he last t en years applying new t echnologies t o a wide range of indust ry solut ions. While serving as a Senior Archit ect in G. A. Sullivan's e- Business group, his prim ary role is t o provide t echnical leadership t o G. A. Sullivan's cust om er s, proj ect t eam s, and sales st aff. John's leadership part icipat ion ext ends t o all phases of t he soft ware developm ent process, from envisioning t o developm ent t hrough st abilizat ion. John has been involved wit h several previous publishing effort s, and recent ly present ed t he gasTI X applicat ion at TechEd 2001 Europe in Barcelona, Spain. John would like t o t hank his parent s, John and Bet t y, as well as Kat hy, Jennaka, and Giovanni.

Jody Socha As a Senior Consult ant for G. A. Sullivan, Jody Socha helps client s wit h all facet s of soft ware developm ent from requirem ent s t o proj ect m anagem ent t o design and im plem ent at ion. He has over five years in developing applicat ions for t he financial services indust ry wit h a focus on ent erprise- class applicat ions and business int egrat ion st rat egies. Jody’s aut horing credit s include a whit e paper ent it led Using Visual Com ponent Manager and t he Microsoft Reposit ory, and chapt ers in bot h of t he last t wo books from G. A. Sullivan on Microsoft Visual St udio.

Acknowledgments From G. A. Sullivan John Alexander Mark Benatar Chris Brauss Gary Dzurny Bill Edwards Stefanie Gavornik Richard Gower Shelley Hawkins Erik Howard Tom Klein Mike Lichtenberg Gary Loberg Tom Luther Steve Miller Mark Munie Dan Norton Brian Pursley Greg Sullivan Debb Wiedner Kim Williamson Deana Woldanski

From Microsoft Bryan Alsup Craig Jaris Judy Kolde Dave Mendlen Cathy Menees Piet Obermeyer Erik Olson Shannon S. Pahl Brad

Rhodes

Marius Rochon Merrill Schebaum Kent Sharkey

From SAMS Leah Kirkpatrick Grant Munroe Karen Wachs

Others Eddie Gulley Kenny Hall Mark Sundt

Introduction Who Should Use This Book? .NET e- Business Archit ect ure is m eant t o be read by t echnical archit ect s and soft ware developers responsible for designing and building I nt ernet - based applicat ions for t he .NET plat form . I t provides det ailed inform at ion on im plem ent ing a .NET syst em design, and present s t he best pract ices and lessons learned along t he way. Coding t echniques using t he Visual St udio .NET t ool suit e are dem onst rat ed t hroughout t he book. The sam ple code is based on t he act ual im plem ent at ion of an ent erprise- scale sam ple applicat ion, gasTI X. .NET e- Business Archit ect ure is also appropriat e for inform at ion syst em m anagers faced wit h planning issues. This book explains t he key concept s behind t he m ost im port ant t echnologies in t he .NET plat form . Visual C# .NET, Visual Basic .NET, ASP.NET, ADO.NET, SOAP, and XML- based Web Services are all covered. More im port ant ly, t his book provides pract ical exam ples of how t o effect ively im plem ent t hese t echnologies, as well as when and why t hey should be considered.

How This Book Is Organized

AM FL Y

This book is organized t o follow an applicat ion developm ent cycle, beginning wit h a look at t he basics of t he .NET plat form . An int roduct ion t o t he sam ple gasTI X applicat ion is given, along wit h an overview of t he business and t echnical goals of t his fict ional com pany. Subsequent sect ions cover design and im plem ent at ion det ails, and t he final sect ion explores .NET t est ing and deploym ent issues.

TE

The chapters in Part I, " Prelim inaries and Planning," int roduce t he concept s behind t he .NET Fram ework. They discuss how t he I nt ernet and t he pervasiveness of XML enable us t o begin offering a new generat ion of soft ware applicat ions. The sam ple applicat ion referenced t hroughout t he book is int roduced, along wit h a chapt er covering t he com m unicat ion m echanism s bet ween t he m ult iple layers of a .NET e- Business applicat ion. The chapters in Part II, " Designing .NET e- Business Solut ions," cover .NET applicat ion design considerat ions from each of t he t hree logical t iers: present at ion, business, and dat a. Securit y t opics are explored, which are an area of prim ary concern for archit ect s, m anagers, and adm inist rat ors alike. I ssues relat ing t o int egrat ion wit h COM+ services are also covered in t his sect ion. The chapters in Part III, " Building t he .NET e- Business," consider t he im plem ent at ion of t he sam ple gasTI X applicat ion from a funct ional point of view. A chapt er is devot ed t o each of t he m ain sit e funct ions ( event searching, t icket purchasing, fulfillm ent / order processing) , from t he t op t ier t o t he bot t om t ier, dem onst rat ing how dat a is passed bet ween applicat ion layers. An abundance of code sam ples provides you wit h ideas and t echniques t hat you can include in your own .NET applicat ions. The chapters in Part IV, " Test ing and Deploying .NET Sit es," explore t he necessit y of debugging and perform ance t uning a .NET e- Business applicat ion prior t o release. An ent ire chapt er is dedicat ed t o t he issues surrounding deploying a .NET applicat ion. This sect ion rounds out a com plet e discussion of t he soft ware developm ent life cycle for a .NET e- Business applicat ion.

Team-Fly®

Part I: Preliminaries and Planning Chapter 1, " .NET—The Big Pict ure," provides a high- level overview of t he m aj or feat ures of t he .NET Fram ework and t he Visual St udio .NET program m ing t ools. An int roduct ory chapt er is necessary because of t he t rue evolut ionary nat ure of t he .NET release. The cent ral role of t he I nt ernet and st andard prot ocols such as XML and SOAP are discussed, along wit h t he im port ance of designing loosely coupled syst em s. This chapt er prepares t he reader t o drill down t o t he m ore solut ion- focused chapt ers t hat follow. Chapter 2, " gasTI X: A Sam ple .NET e- Business," int roduces t he sam ple applicat ion referenced t hroughout t he rem ainder of t he book. The sam ple applicat ion is an online t icket ing web sit e t hat sells t icket s t o m aj or sport ing and concert event s across t he count ry. The chapt er provides an overview of t he requirem ent s and business drivers behind t he t echnical approach chosen. Thought s on t eam com posit ion and effect ive proj ect m anagem ent for a .NET e- Business im plem ent at ion are also included. Chapter 3, " Enabling I nt er- Applicat ion Com m unicat ions," discusses how independent applicat ions can work t oget her in a .NET collaborat ive environm ent . Concept s such as XML, Web Services, SOAP, and Orchest rat ion in BizTalk Server 2000 are covered. Part II: Designing .NET e-Business Solutions Chapter 4, " Tools of t he Trade," discusses t he various archit ect ural, language, and product ivit y t ools at t he disposal of designers and developers program m ing for t he .NET plat form . The .NET Fram ework is covered in m ore dept h, along wit h t he t wo m aj or .NET languages, Visual C# .NET and Visual Basic .NET. New product ivit y feat ures in t he Visual St udio .NET int egrat ed developm ent environm ent ( I DE) are explored. Chapter 5, " I m plem ent ing .NET Present at ion Services," covers t he design considerat ions for t he Web user int erface in .NET. ASP .NET Web Form s are int roduced, along wit h t he new st at e m anagem ent opt ions. Approaches for how HTML can be separat ed from ASP .NET code are given, and server- side vs. client - side rendering issues are also covered. Chapter 6, " I m plem ent ing .NET Business Services," discusses approaches t o designing t he m iddle t ier in a .NET e- Business applicat ion. The design of t he gasTI X business services is discussed, as well as new issues for com ponent developers, including ADO .NET and .NET Rem ot ing. A discussion of t he gasTI X Web Services is anot her im port ant t opic covered in t his chapt er. Chapter 7, " I m plem ent ing .NET Dat a Services," looks at design considerat ions for t he dat a t ier in a .NET applicat ion. This chapt er explores som e of t he new feat ures of SQL Server 2000 and how t hey int egrat e int o t he gasTI X websit e. I ssues relat ed t o t he use of st ored procedures, XML, and indexes in SQL Server 2000 are explored t hrough evaluat ing t he im plem ent at ion choices m ade in t he sam ple applicat ion. Chapter 8, " Handling Securit y," discusses applicat ion securit y issues and how .NET handles t hem . Role based securit y is explored, including aut hent icat ion and aut horizat ion using cookies and cert ificat es. Code access securit y t hrough assem bly signing is also covered, along wit h t he different t ools used in t his regard. Chapter 9, " COM+ I nt egrat ion," shows how int eract ion wit h COM+ legacy com ponent s can be accom plished. This chapt er provides a discussion of t he issues encount ered when we consider port ing a COM+ applicat ion t o .NET m anaged code. I t also present s an overview of how t o best leverage your current invest m ent in exist ing COM+ com ponent s.

Part III: Building the .NET e-Business Chapter 10, " Event Searching," discusses how a user can browse t he gasTI X sit e t o find a specific event . One of t he search m et hods is discussed in det ail by focusing on ASP .NET im plem ent at ion t echniques using several of t he Web Cont rols, along wit h a cust om User Cont rol. An advanced User Cont rol is also built for use on t he hom e page of gasTI X. Chapter 11, " Purchasing a Ticket ," present s t he area of t he sit e where a user act ually purchases a t icket . The focus of t he chapt er is a walkt hrough of purchasing a t icket on gasTI X, and it s im plem ent at ion as a web service. The gasTI X purchasing web services, consum ed by t he gasBAGS websit e ( a fict ional band who wishes t o offer it s fans event t icket purchasing funct ionalit y) , are discussed in t his chapt er. Chapter 12, " Fulfillm ent ," dem onst rat es how com m unicat ion is handled bet ween t he m ain gasTI X sit e and a sit e run by a t hird- part y responsible for print ing and shipping t icket s. BizTalk issues are discussed, including m essage queuing, rout ing, and workflow orchest rat ion. Chapter 13, " .NET Rem ot ing," discusses t he im port ant concept s for developing and host ing .NET obj ect s rem ot ely. The use of rem ot ing in t he gasTI X applicat ion is exam ined, along wit h t he differences bet ween providing applicat ion funct ionalit y via rem ot ing and via SOAP web services. Part IV: Testing and Deploying .NET Sites Chapter 14, " Debugging a .NET Applicat ion," discusses t ools and t echniques developers can use t o efficient ly debug a .NET applicat ion. The m aj or debugging enhancem ent s in Visual St udio .NET are also explored. Chapter 15, " Perform ance Tuning in .NET," covers general perform ance t est ing concept s, along wit h t he various perform ance m echanism s in .NET, such as ASP .NET out put caching. The m ain .NET t uning t ools, Applicat ion Cent er Test ( ACT) , Visual St udio Analyzer ( VSA) , and SQL Profiler, are all discussed. These t ools help you find and elim inat e syst em bot t lenecks. Chapter 16, " Deploying t he Applicat ion," walks t he reader t hrough t he st eps required t o deploy a .NET applicat ion t o a product ion environm ent . I m port ant issues t hat should be considered for proper applicat ion configurat ion are addressed. Specific considerat ion is given t o configuring and m anaging t he sam ple gasTI X applicat ion in Applicat ion Cent er 2000.

Tell Us What You Think! As t he reader of t his book, you are our m ost im port ant crit ic and com m ent at or. We value your opinion and want t o know what we're doing right , what we could do bet t er, what areas you'd like t o see us publish in, and any ot her words of wisdom you're willing t o pass our way. As an Associat e Publisher for Sam s Publishing, I welcom e your com m ent s. You can fax, em ail, or writ e m e direct ly t o let m e know what you did or didn't like about t his book- - as well as what we can do t o m ake our books st ronger.

Please not e t hat I cannot help you wit h t echnical problem s relat ed t o t he t opic of t his book, and t hat due t o t he high volum e of m ail I receive, I m ight not be able t o reply t o every m essage. When you writ e, please be sure t o include t his book’s t it le and aut hor as well as your nam e and phone or fax num ber. I will carefully review your com m ent s and share t hem wit h t he aut hor and edit ors who worked on t he book. Fax: 317- 581- 4770 Em ail: feedback@sam spublishing.com Mail: Linda Engelman Sams Publishing 201 West 103rd Street Indianapolis, IN 462

1 .NET--The Big Picture Change is const ant in our lives. I n every area of hum an endeavor t he ongoing flow of progress cont inues, and com put er t echnology is no except ion. Just as we begin t o feel com fort able wit h a part icular plat form and t oolset , anot her is int roduced and t he learning process begins anew. Though we m ay occasionally lam ent t he effort required t o keep up wit h t he lat est advancem ent s in t echnology, t he const ant pace of innovat ion is also very excit ing. The t erm “ .NET” carries m ult iple m eanings. I t is first and forem ost an um brella m arket ing t erm used t o describe t he ent ire Microsoft plat form , m uch as Windows DNA described t he previous plat form . But t he ast ut e observer will not ice t hat .NET is also used t o refer t o one or m ore elem ent s of t he overall plat for m in addit ion t o t he ent ire plat form as a whole, which can lead t o confusion. The exact m eaning of t he t erm depends on t he cont ext , and what is m ost im port ant t o t he speaker. Here is a short list of different elem ent s t hat are described by t he t erm .NET, which are described in det ail in t his book: z

the collection of Microsoft’s Enterprise Servers

z

the Common Language Runtime (CLR)

z

the .NET Framework

z

an approach to software development emphasizing the role of “web services” and the use of standard Internet protocols

z

the collection of Microsoft’s “core” web services codenamed “Microsoft My Services”

The Microsoft .NET plat form is a com prehensive overhaul in t he evolut ion of t heir soft ware developm ent t oolset . Som e elem ent s of t he plat form have been under developm ent for four years, and alm ost every facet has been changed is som e way. This book is designed as a t horough int r oduct ion t o .NET, and how t o use it t o creat e e- Business applicat ions and web sit es. The aut horing t eam also act ed as a soft ware developm ent t eam , creat ing a com prehensive sam ple com prising t wo web sit es, and t hen describing t he t echniques t hat were used in t he following chapt ers. Welcom e t o t he world of .NET.

The Internet Rules! As organizat ions of all sizes and descript ions capt ure and analyze inform at ion, form ulat e st rat egies, and ponder t heir fut ures, it is clear t hat t he I nt ernet has changed everyt hing. The im pact t hat t his m ult ifacet ed ent it y has on our businesses, our schools, our governm ent s, and our lives is profound. This has been st at ed so oft en it has already becom e a cliché, barely wort h m ent ioning. We are not here t o prognost icat e on which way t he wind is blowing, or t he fat e of " t he dot com s." But t o underst and t he significance and t he im pet us of .NET you m ust begin here: The I nt ernet is t he t hing. I t has becom e a m aj or fact or in t he way t hat businesses int eract wit h suppliers, wit h cust om ers, and wit h business part ners. I t has dram at ically alt ered t he availabilit y of inform at ion, accelerat ing t he pace of inform at ion flow, and increasing cust om er expect at ions. I t ’s im pact is som et im es obvious, but it has also caused pervasive and subt le changes in t he way we t hink about t he world which are only beginning t o be fully underst ood.

A Level Playing Field What m akes t he I nt ernet so im port ant ? One reason is t hat - t he I nt ernet offers a reasonably level playing field for a large num ber of part icipant s. The I nt ernet operat es wit h no single organizing force, no single group or individual holding ult im at e power. English is not a st andard on t he worldwide web, but Hypert ext Transfer Prot ocol ( HTTP) and Hypert ext Markup Language ( HTML) are, as well as Ext ensible Markup Language ( XML) . Large corporat e ent it ies from leading indust rialized nat ions cert ainly exert a great deal of influence, but no conglom erat e owns t he I nt ernet , and it is not governed by t he laws of any single count ry. Of course t here are polit ical m aneuverings, and people from all over t he world engaging in debat es and m achinat ions aim ed at a favorable out com e for t heir const it uencies and t hem selves. But by and large t he I nt ernet is a place wit h very low barriers t o ent ry, very lit t le regulat ion, and lot s of opport unit y.

Standards--So Many to Choose From A wise m an once said, " The nice t hing about st andards is t here are so m any t o choose from ! " This is t rue even if we lim it our invest igat ion t o web- based prot ocols, or even t hose prim arily associat ed wit h applicat ion developm ent and deploym ent . There are m any st andards. We like t o t hink of t his in a posit ive way, looking at t hese st andards as m any t echnologies in our t oolkit , wait ing for t he appropriat e sit uat ion t o be used. Why are st andards so im port ant ? Wit h so m any people part icipat ing in t his shared endeavor, we m ust agree on som e rudim ent ary approaches t o conduct ing our conversat ions, our t ransact ions, our business. Absolut e anarchy serves only t he nihilist . I f we wish t o accom plish som et hing const ruct ive, we need building blocks wit h which t o work. St andard I nt ernet prot ocols serve as t hese building blocks. As a guiding principle, we'd prefer t o " be one of t he m any, not one of t he few." I n ot her words, we'd generally like t o select m at ure t echnologies t hat are used by m any ot her organizat ions. And, in general, we'd like st andards t hat are cont rolled by an independent organizat ion t hat is not prim arily a profit - m aking ent it y. This chapt er won't delve deeply int o specific prot ocols, but it will m ake a few key point s about XML and Sim ple Obj ect Access Prot ocol ( SOAP) because of t he cent ral role t hey play in .NET. XML Am ong it s m any benefit s, XML is a prim ary enabling t echnology when we wish t o build loosely coupled syst em s. To appreciat e t his benefit , som e hist ory is in order. Am ong t he lessons we've learned over t he past t en years is t he value of building our syst em s t o be resilient in t he face of change. Things always change, usually before we can deploy Version 1.0 of a new syst em ! As we have st ruggled t o deal wit h t his const ant need t o adapt we have learned t hat if t he int erface bet ween t wo syst em s is highly const rained, any changes m ust be m ade at t he sam e t im e t o bot h syst em s. This is bad enough, but if we have a single syst em t hat connect s t o m any ot her syst em s, t he result is part icularly pernicious. Changing all t he relat ed syst em s in synchronizat ion wit h one anot her is nearly im possible. Things becom e so difficult t o change we m ay decide inst ead t o inst it ut ionalize t he current pract ice. XML provides a flexibilit y t hat can solve t his problem .

I n t he past , it was com m on pract ice t o define a highly st ruct ured dat a t ransm ission prot ocol. I f one applicat ion needed t o t ransm it inform at ion t o anot her applicat ion, t he inform at ion was oft en represent ed in a com pressed, binary form at . This was done in an effort t o reduce t he size of t he t ransm ission t o m axim ize t he use of relat ively scarce bandwidt h. An unwelcom e offshoot of t his approach is a t ight coupling bet ween t he t wo applicat ions com m unicat ing wit h one anot her. I f we need t o change t he cont ent of t he t ransm ission, we m ust change bot h applicat ions at t he sam e t im e. Soft ware designed, for exam ple, t o int erpret t he first six charact ers of a dat a t ransm ission as an invent ory num ber is, by it s design, going t o yield a t ight ly coupled syst em . But suppose we don't build a preconceived not ion of t he exact posit ioning of t he invoice num ber int o our soft ware. Suppose, inst ead, t hat we look for t he invoice num ber bet ween t wo t ags, < invno> and < / invno> , t hat m ust appear som ewhere wit hin t he dat a st ream . Now t hat 's being a bit m ore reasonable. We m ight wish t o define a schem a t hat represent s, for us, t he proper synt ax for a valid invoice. We m ight be willing t o consider a subset of t he inform at ion opt ional. This would allow us t o m ake changes t o one syst em wit hout t he ot her being affect ed- - loose coupling! So, we've seen how t he design of XML allows us t rem endous flexibilit y and power in t he exchange of business docum ent s. The sam e approach can obviously be used wit h inform at ion of all kinds. This is one of t he prim ary reasons t hat t he use of XML has at t ract ed so m uch at t ent ion. I s t here anot her way t o leverage t his sim ple but elegant prot ocol? Yes, and t hat way is SOAP. SOAP Note: Simple Object Access Protocol (SOAP) is an XML-based protocol that allows applications to exchange structured data over the web. SOAP is the glue that ties together disparate services through industry standard protocols. SOAP allows developers to build XML Web services, which expose programmatic functionality over the Internet.

Let 's assum e we have a dist ribut ed syst em m ade up of t wo or m ore com put ers, each running it s own operat ing syst em , t hat are connect ed by various net working prot ocols ( predom inant ly TCP/ I P) . For som e years t here have been passionat e debat es about obj ect m odels, applicat ion developm ent m odels, soft ware archit ect ures, and relat ed t echnologies. As dist ribut ed com put ing and com ponent - based approaches grew in popularit y, t he com pet it ion grew for t he heart s and m inds of developers. The m aj or vendors involved in providing t ools for building ent erprise- class applicat ions are oft en bit t er enem ies, and com pet e vigorously against one anot her. The playing field is replet e wit h acronym s represent ing various approaches: CORBA versus COM/ DCOM, Ent erprise Java Beans ( EJB) versus Windows DNA, J2EE versus .NET. Of course, under t he surface t here are m any sim ilarit ies in all of t hese approaches. I n part icular, all of t hem offer a way for a process execut ing on a part icular com put er t o call a rem ot e procedure execut ing in anot her process on t he sam e m achine, or on an ent irely different com put er. We need a way t o specify t he procedure, send one or m ore argum ent s, and receive result s in t he form of ret urn argum ent s. And every Rem ot e Procedure Call ( RPC) should be uniquely resolveable in an unam biguous way, leading us t o t he correct execut able m odule on t he correct com put er. Every plat form finds a way t o do t his, and t he differences in im plem ent at ion are perhaps less int erest ing t han t he sim ilarit ies. And yet t hey are different . Syst em s built wit h one plat form are incom pat ible wit h ot hers. This gave rise t o such t hings as COM/ CORBA gat eways t hat provide int eroperabilit y bet ween t wo different plat form s. But such gat eways, living wit h one foot in each world, suffer from t he problem s of bot h,

and are nearly always an undesirable com prom ise. There are frequent inconsist encies in how eit her side of t he gat eway deals wit h dat a t ypes and param et ers, as well as t he inevit able perform ance hit incurred by t he t ranslat ion process. Ent er t he web, and expect at ions have changed. The m essage being sent by t he m arket place is t his: what ever we build, it m ust work on t he web. And it m ust int eroperat e wit h ot her syst em s. I f difficult and capricious requirem ent s exist for int eract ing wit h a given syst em , t he t im e and expense of doing so will becom e t oo great . All syst em s m ust adhere t o a reasonably st raight forward way of int eroperat ing wit h ext ernal syst em s. This is t he way organizat ions wish t o conduct business t oday. And so we see som et hing happening t hat was unant icipat ed by m ost , professionals and pundit s alike. An elegant prot ocol has been devised for calling a rem ot e procedure t hat runs over st andard I nt ernet prot ocols, is firewall- friendly, and is not " owned" by any single vendor. Take t he cont ent s of a rem ot e procedure call, form at t hem wit h XML, and send t his payload over HTTP prot ocols. What could be sim pler ? Forget your COM/ CORBA gat eways, your façade classes wrapping rem ot e, dissim ilar syst em s and archit ect ures. Let 's all speak SOAP!

Enterprise-Level Attributes Offering a high degree of int eroperabilit y t hrough t he use of st andard web- based prot ocols is clearly a good t hing. I t is not enough, however. I n t his age of e- Business, and t he rise of t he " dot com s," sit es m ust be prepared t o deliver m ore. What if we decide t o invit e t he whole world t o visit our sit e following t he NFL's Super Bowl or t he World Cup soccer finals? What if we want t o service a worldwide client ele, in m ult iple t im e zones? The charact erist ics we need are som et im es described as t he " ilit ies" , or m ore eloquent ly as ent erprise class at t ribut es. We've already discussed int eroperabilit y. What are t he ot her " ilit ies" ? They include: z

Scalability

z

Reliability

z

Manageability

z

Flexibility

When we say a sit e should be scalable, we are referring t o t he abilit y t o handle a large " load" - t o perfor m lot s of work. This usually m eans a large num ber of concurrent client s, but m ay also m ean relat ively fewer client s each engaged in t asks t hat require t he syst em t o perform a lot of work on t heir behalf. We need reasonable perform ance for t he first user t o visit our sit e, and as t he am ount of work increases, t hat perform ance should not degrade below a well- defined m inim um accept able level. The sit e should also be reliable. Depending on t he nat ure of t he sit e we m ay even wish t o approach t he elusive " five nines" , 99.999% upt im e. Since m any of t he elem ent s we use t o build sit es do not t ypically offer t his level of reliabilit y, we m ust usually incorporat e redundant elem ent s. By elim inat ing any single point of failure, an individual elem ent whose failure will bring down t he ent ire sit e, we can dram at ically im prove reliabilit y. This goal also im pact s our abilit y t o perform scheduled m aint enance. No longer can we use lat e night hours aft er t he close of business for operat ions t hat will have a m aj or effect on perform ance. Our business m ay not " close" !

Large organizat ions are subj ect t o m any out side influences. There are regulat ory and st at ut ory requirem ent s im posed by governm ent s, and com pet itive pressures exert ed by ot her organizat ions. The life cycle of a t ypical aut om at ed syst em has shrunk st eadily over t he past few decades. Everyone is in a hurry. I n order t o respond quickly t o change, we m ust design our syst em s wit h from t he very beginning t o support change. We m ust select an archit ect ure t hat enables change. Earlier in t he chapt er we discussed loose coupling as a t echnique t o enable change. Building a syst em out of discret e com ponent s t hat encapsulat e t heir funct ionalit y is anot her approach t hat support s t his goal. I f we don't address t his need, our syst em is doom ed t o a very brief useful lifespan. Perhaps flexibilit y isn't enough. I n m any sit uat ions, our syst em s need t o go beyond being flexible- t hey m ust be agile. As our sit e operat es day- by- day, support ing t he needs of our organizat ion and navigat ing t he com pet it ive landscape, we m ay need agilit y t o be successful. This com bines t he idea of flexibilit y wit h an elem ent of t im eliness, and also connot es t he abilit y t o be nim ble, t o be sure- foot ed. We can m ake changes wit h confidence and land on our feet when faced wit h t he need t o change. As syst em archit ect s and developers have pursued t hese loft y goals, t he t ools and t echniques t hey use have undergone a const ant evolut ion. The Microsoft .NET plat form is seen in t his light as t he current incarnat ion of an evolving process, one t hat cont inues t o t his day and beyond. An underst anding of how t his process has unfolded and it s m aj or m ilest ones can be helpful in developing a com plet e underst anding of our current st at us. Therefore, a brief sum m ary of t his process is provided in t he next sect ion.

The Evolution of the Programming Model Developers m ust learn m any t hings during t he course of t heir career. The first of t hese is, obviously, at least one program m ing language. I n addit ion t o t his, t here are broader issues t hat m ust be m ast ered. What are t he charact erist ics of t he t arget plat form ? What operat ing syst em will be running on t he com put ers m aking up t he syst em ? What are it s capabilit ies, and how should t hey be leveraged t o deliver t he desired applicat ion specific feat ures? Also of int erest is t he developm ent plat form . What edit or or environm ent will be used? What Applicat ion Program m ing I nt erface( s) ( API ) or obj ect m odel( s) are relevant ? I n ot her words, what is t he program m ing m odel? The .NET Fram ework has t he benefit s of Microsoft ’s m any years of experience building bot h low- level and rapid applicat ion developm ent ( RAD) languages and environm ent s. The .NET Fram ework has been specifically designed t o handle t he challenges faced by t oday’s developers.

Clients and Servers Not so long ago, developers who wrot e applicat ions for PCs were t arget ing individual users sit t ing at disconnect ed com put ers. I f t hey were connect ed t o a net work, t hey m ight load and save files from a shared server, or print on a shared print er, but very lit t le was required of t he developer t o ut ilize t hese services. Net working syst em soft ware int ercept ed calls direct ed at t he local operat ing syst em and redirect ed t hem t o shared devices. Microsoft Windows and ot her sim ilar operat ing environm ent s offered a Graphical User I nt erface ( GUI ) t hat gave developers a whole new program m ing m odel- event - driven program m ing. The applicat ion was essent ially a loop t hat wat ched for m essages sent by t he operat ing syst em t o not ify t he applicat ion of event s as t hey occurred: keyboard act ivit y, m ouse m ovem ent , m ouse clicks, et c.

Over t im e, net worked PCs were increasingly used t o deliver applicat ions for groups of users, perhaps a depart m ent wit hin an organizat ion. The need for large am ount s of st ruct ured dat a grew, and Dat abase Managem ent Syst em s ( DBMS) becam e popular. This led t o t he creat ion of client / server com put ing, a different way of building applicat ions ( see Figure 1.1) . For t he first t im e execut able m odules m aking up t he applicat ion execut ed on m ore t han one com put er- t he birt h of dist ribut ed com put ing. Client / server com put ing offered im port ant advant ages. Server- based dat abases were capable of enforcing cent rally defined business rules ( for exam ple, " Don't allow anyone t o delet e a cust om er t hat st ill owes us m oney! " ) , and allowed m ore com put ing power t o be applied t o a problem .

Server Laser printer

Ethernet

Client

Client

Client

Figure 1.1 The client / server program m ing m odel was a significant depart ur e from m onolit hic, single- m odule applicat ions.

As t he use of t his program m ing m odel increased, new problem s arose. Am ong t he m ost serious was t he cost of deploying t he client port ion of t he applicat ion. I n fact , in som e cases, t he cost of deploym ent was great er t han t he cost of creat ing or acquiring t he applicat ion. I t was very expensive t o inst all a collect ion of execut able m odules and configurat ion set t ings on dozens or hundreds of com put ers. I n addit ion, t he prot ocols t hat were used for connect ing client and server were not designed t o work well in a highly dist ribut ed environm ent like t he I nt ernet . The growing use of firewalls present ed significant challenges. For t hese and ot her reasons, applicat ion developers began exploring t he use of web browsers such as Net scape Navigat or and Microsoft 's I nt ernet Explorer. At first , using a browser as a " cont ainer" for t he client - side applicat ion m eant serious com prom ises in t he feat ure set available on t he client . Far fewer bells and whist les were possible for client s host ed in a browser. Com pat ibilit y differences am ong rapidly changing browsers from different vendors were also a challenge.

Perhaps m ore im port ant ly, t he lim it at ions of using St ruct ured Query Language ( SQL) t o im plem ent business rules were increasingly apparent . SQL is a non- procedural, set t heory- based language ideal for specifying dat a- cent ric operat ions on a dat abase. I t is not designed t o provide t he services of a general purpose program m ing language. As t hese developm ent s were t aking place, anot her paradigm shift was unfolding- t he growt h of Obj ect Orient ed Program m ing ( OOP) . OOP offered a program m ing m odel wit h im port ant advant ages such as encapsulat ion and inherit ance. Many problem s facing developers were easier t o solve when viewed from t his perspect ive. And OOP also offered a great er opport unit y t o achieve one of t he m ost elusive goals of large- scale soft ware developm ent - reuse.

Applications Become Sites

TE

AM FL Y

Com bining elem ent s of client / server and OOP, yet anot her program m ing m odel em erged. Com ponent - based dev elopm ent viewed t he applicat ion as a collect ion of m odules t hat could be deployed on net worked com put er syst em s. But inst ead of a 2- t ier client / server approach, applicat ions were separat ed int o t hree logical t iers ( see Figure 1.2) . These were usually referred t o as present at ion services, m id- t ier business logic, and dat a services. Called 3- t ier or, in m ore com plex arrangem ent s, N- t ier or m ult i- t ier, t his program m ing m odel is ideally suit ed for web- based environm ent s, as well as st andard applicat ion developm ent s. I t is arguably t he predom inant program m ing m odel for non- scient ific applicat ions at t he t im e t his book is being writ t en.

Figure 1.2 A logical t hree- t ier archit ect ure provided a separat ion of t he business rules em bedded in a syst em .

Team-Fly®

Present at ion services are delivered by t he com binat ion of web browsers and web servers working in concert wit h one anot her. Microsoft 's Act ive Server Pages ( ASP) and sim ilar t echnologies allow applicat ion logic on t he web server t o execut e, ult im at ely producing HTML for delivery t o t he browser where it is rendered. Opt ionally, client - side logic can also be creat ed for execut ion direct ly on t he client com put er. Present at ion services are prim arily concerned wit h allowing t he user t o navigat e t hrough t he various screens and dialogs t hat m ake up t he applicat ion, accept ing input from t he user, and present ing result s on t he client com put er's display. I n t he m iddle t ier, t he rules t hat define how an organizat ion operat es are im plem ent ed. We m ight , for exam ple, creat e com ponent s t hat calculat e t axes and apply t hose t axes only when appropriat e. Mid- t ier business logic is m ost oft en im plem ent ed using com ponent - based t echniques, which rely on syst em soft ware t o provide services for creat ing, m anaging, and dest roying com ponent s on a server. Microsoft 's first offering in t his area, t he Microsoft Transact ion Server ( MTS) , has evolved int o a set of services provided by COM+ for m anaging com ponent s and t ransact ional behavior. This allows t he use of obj ect orient ed approaches and leaves t he dat abase t o do t he t hings for which it is best suit ed. Dat a services are concerned wit h t he st orage and ret rieval of inform at ion in a persist ent st ore, usually a relat ional dat abase. Dat a service com ponent s oft en are designed t o encapsulat e t he exact det ails of physical dat a st orage and present a " friendly" façade t o m id- t ier services. This allows t he developers of business logic t o sim ply request cust om er inform at ion, for exam ple, and not worry about what t ables t he inform at ion is st ored in, t he relat ionships bet ween various t ables, and how t hey are indexed. I t is im port ant t o not e t hat t here is a dist inct ion bet ween logical t iers and t he physical im plem ent at ion of t hese elem ent s. I t is t em pt ing t o t hink of present at ion services occurring on a client com put er, m id- t ier services execut ing on a web server and/ or applicat ion server, and dat a services on a dat abase server. I n realit y, it is a bit m ore subt le and com plicat ed t han t his sim plified pict ure, as shown if Figure 1.3. W e b F a rm

D a ta b a s e C lu s te r

D a ta W e b S e rv e r

W e b S e rv e r

W e b S e rv e r

D a ta b a s e S e rv e r

D a ta b a s e S e rv e r

E th e rn e t

In te rn e t

C lie n t

C lie n t

Figure 1.3 The physical archit ect ure of a large web sit e m ay be m ade up of m any servers working t oget her t o funct ion as a single syst em .

Scale Up versus Scale Out A cent ral and ongoing debat e focuses on t he issue of how t o deliver t he m axim um com put ing power t o a part icular applicat ion. I s it bet t er t o build very large m ult i- processor syst em s wit h m assive am ount s of m em ory, or should collect ions of relat ively norm al sized, com m odit y- priced servers be used? Bot h t echniques are now possible, and t hey are referred t o respect ively as scaling up and scaling out . Scaling up is t he m ost com m on approach t o providing m axim um power wit h t he dat abase. This is prim arily due t o t he com plexit y associat ed wit h part it ioning a large dat abase m ade up of m any t ables, indexes, views, t riggers, and ot her elem ent s int o a collect ion of m ore or less uniform servers. I t is possible for any reasonably t alent ed dat abase adm inist rat or t o do t his j ob m anually, but when t he part it ioning schem e needs t o be adj ust ed ( and sooner or lat er it always will) you are faced wit h a difficult and t im e- consum ing j ob alm ost cert ain t o require scheduled downt im e. SQL 2000 offers new support for part it ioning a dat abase, and for som e applicat ions t his m akes sense, but as a general rule it is easier t o scale t he dat abase up, not out . Scale up approaches also m at ch up well wit h failover clust ering for reliabilit y, part icularly im port ant for t he t ransact ional nat ure of m ost dat abases. A scale out approach is m ost oft en used for web servers, grouping t hem int o a load- balanced web farm . The elem ent s t hat m ake up t he web server's port ion of a dist ribut ed, com ponent - based applicat ion ( for exam ple, script ed web pages, graphics, and configurat ion files and set t ings) are deployed in a uniform fashion t o each server in t he farm . Once t his is accom plished, incom ing HTTP request s are dist ribut ed t o one of t he servers. I deally, t he am ount of work perform ed by any given server is very close t o t he am ount of work perform ed by each of t he ot her servers- an equal load.

Balancing the Load There are a num ber of approaches t o load balancing, som e hardware- based and som e soft ware- based. One of t he first approaches was a sim ple " round robin" dist ribut ion t hat can be provided by t he sam e Dom ain Nam e Syst em ( DNS) t hat direct s t raffic for t he I nt ernet . I nst ead of configuring only one ent ry in t he DNS t able for a part icular dom ain nam e, a group of I P addresses could all be associat ed wit h a sit e nam e ( for exam ple, www.gasullivan.com ) . This worked, but didn't offer m uch resilience when one of t he servers went down. DNS would cont inue t o send an equal share of incom ing request s t o t he down ( and unavailable) server, causing t he client t o experience a long wait and event ual t im eout of t he applicat ion or worse. Net working and rout er vendors such as Cisco and F5 have offered product s for several years t hat address t his problem . I n addit ion t o dist ribut ing incom ing request s, t hese product s will t ypically sense when a server has gone down and rem ove it ( t em porarily) from t he web farm . Microsoft acquired and refined sim ilar soft ware- based t echnology. The Windows Load Balancing Service ( WLBS) was first offered as a free download from Microsoft 's web sit e, t hen was subsequent ly renam ed and incorporat ed int o Windows 2000 as Net work Load Balancing ( NLB) . A num ber of indust ry pundit s have predict ed t hat load balancing a web farm is rapidly on it s way t o becom ing a com m odit y service, and t he pricing for t his capabilit y cont inues t o drop as addit ional feat ures are added and perform ance im proves. Load balancing is not necessarily a t rivial t ask, however. For exam ple, cert ain applicat ions m ay perform a great deal of work using m id- t ier com ponent s. Such an applicat ion m ight warrant a m ult i- t ier approach t hat im plem ent s a physically dist inct collect ion of m id- t ier servers ( in ot her words, t he m id- t ier

com ponent s are not run on t he web servers) . Now we are faced wit h a m ore difficult t ask t han equally dist ribut ing incom ing request s across t he server farm . I deally, we would equally dist ribut e t he act ual work, not j ust t he com ponent request s. The algorit hm s for providing t his capabilit y are non- t rivial. Am ong t he problem s t hat m ust be faced is t he need t o m easure and charact erize t he load being sust ained by a given server. What exact ly should be m easured? Clearly you want t o m easure t he use of one or m ore of a server's resources such as t he Cent ral Processing Unit ( CPU) , m em ory, or disk access, but which and in what com binat ion? Also, it is clearly im port ant not t o j ust look at resource ut ilizat ion at som e part icular inst ant , but t o m easure load over t im e. But we need t o be careful or we will wast e precious resources in order t o accom plish t he m easuring. Finally, t here is t he quest ion of how t o dist ribut e t he load. Assum ing we know at any given inst ant which server should receive t he next request , what is t he act ual m echanism t hat accom plishes t his t ask? Do we send a m ult i- cast request and depend on t he least busy server t o answer first ? Or do we provide a t raffic m anager ( pot ent ially a single point of failure) t o dole out work? Microsoft has grappled wit h t hese issues, and offers a Com ponent Load Balancing ( CLB) capabilit y in addit ion t o NLB.

A Matter of State The m ult i- t ier program m ing m odel helped solve m any problem s faced by developers and syst em archit ect s, but also brought som e new challenges. The use of a web farm t o handle very large num bers of users is a great benefit , but as t hese syst em s were deployed, t est ed, and t uned it becam e obvious t hat st at e m anagem ent was an issue. Most web server plat form s offer an obj ect m odel wit h som e t ype of Session const ruct for m anaging int eract ion wit h a part icular user. I t is possible t o use session variables t o st ore inform at ion about a part icular session- choices m ade by t he user, user preferences, and ot her inform at ion collect ively referred t o as " t he current st at e." Doing so, however, can put a considerable burden on t he resources of t he server. An even m ore difficult problem arises when we allow each request for a new page t o be load balanced. I f we force a part icular client session t o " st ick" t o a part icular server, creat ing what is called server affinit y, we m ust do ext ra work t o provide t his session capabilit y over HTTP, which is essent ially a session- less prot ocol. Furt herm ore, t his will alm ost cert ainly cause a less even dist ribut ion of t he load. I t would be easier in som e respect s t o allow each request for a new page t o be serviced by any server in t he web farm . This would likely provide a m ore uniform dist ribut ion of load, and sim plify our session m anagem ent chores. But , what about st at e? I f t here is any current st at e inform at ion t hat should be m aint ained, how is it t o be m anaged when t he user is redirect ed t o an ent irely different server? As we will explore in som e det ail in a lat er chapt er, t he .NET plat form offers several alt ernat ives t o address t his dilem m a: z

Use session variables as in the past. Though this limits scalability, it is the fastest alternative with a single web server.

z

Configure a "state server" running as a Windows NT service. A single state server can provide state management for an entire web farm, providing a balance between performance and scalability. This potentially introduces a single point of failure.

z

Configure the web farm to store state information in SQL Server. Though this adds the most overhead, and hence compromises performance somewhat, it is the most reliable. The admittedly slower performance will continue to be a problem as the number of users grows very large.

I n addit ion t o m aint aining st at e inform at ion for a user session, it is also clearly im port ant t o be able t o ident ify a part icular session. This can be done in a variet y of ways, each wit h it s own st rengt hs and weaknesses. The m ost com m on m et hod is t o use HTTP " cookies" , st rings of specially coded inform at ion t hat is st ored direct ly on t he user's com put er and passed t o t he server wit h each request . Cookies are seen as invasive, and a privacy concern by som e users, and m ost browsers can be configured t o disallow t he use of cookies. Therefore an alt ernat e m et hod m ust be provided. The m ost widely used alt ernat ive t o cookies is t o im bed t he sam e inform at ion direct ly int o t he Uniform Resource Locat or ( URL) t hat is passed from client t o server. Alt hough t his circum vent s t he problem wit h cookies, it is not as reliable since all session inform at ion is lost if a server goes down.

Incorporating Services The m et am orphosis from applicat ions t o sit es becom es even m ore int erest ing when we int roduce t he concept of XML Web services, as described in t he previous sect ion, “ SOAP.” XML Web services blur t he definit ion of t he t erm "sit e." We will explore t he concept of soft ware as a service in det ail in t he next sect ion, but for now we will int roduce t he idea wit h a sim ple exam ple. To avoid confusion, it is perhaps best t o not e im m ediat ely t hat t his use of t he word " service" is sim ilar t o, but dist inct from , it s norm al English definit ion- t he act of helping or doing work for anot her or for a com m unit y. I t is also dist inct from a Windows NT- st yle service, which refers t o a specific t ype of process running on a Windows NT or Windows 2000 com put er ( sim ilar t o a Unix daem on) . Suppose we are creat ing a web sit e whose purpose is t o sell an invent ory of it em s t hat vary in size and weight . We wish t o charge our cust om ers for t he it em s t hey order and offer a range of shipping alt ernat ives including overnight delivery as well as norm al delivery t hat m ay t ake several days. How do we calculat e t he charge for shipping? We could, of course, review t he process a person would use t o m anually calculat e shipping charges for using a part icular carrier. We could creat e a dat abase t able represent ing t he current rat es for various sizes, weight s, and dist ances. Then we could creat e one or m ore business rule com ponent s t hat calculat e shipping charges according t o t he current rules for t hat carrier. We could t hen repeat t his process for each carrier we wished t o use. The inform at ion in t hese t ables, and t he rules expressed in t he com ponent s, will need t o be changed periodically, perhaps frequent ly. A m ult i- t ier approach facilit at es t his process, but we don't really wish t o becom e quit e so int im at e wit h t he calculat ion of shipping charges. Perhaps t he m ain concern is t hat t his effort does not represent our core com pet ency. Get t ing t his right is not our prim ary concern. We want t o sell our invent ory; we are not in t he shipping business. I t would be very nice if we could som ehow out source t his calculat ion t o t hose for whom it is a prim ary concern, nam ely t he carriers t hem selves. What if we could send an elect ronic request for t he calculat ion of a shipping charge t o send an it em of specified size and weight from a part icular locat ion t o anot her locat ion, and quickly receive a response cont aining t he cost ? This is t he cent ral idea behind a web service.

Services Our shipping calculat ion exam ple leaves m any im port ant det ails of web services undefined. Which shipping com panies offer such a web- based service, if any? Exact ly what do I send in t he request ? Do I specify t he t o and from locat ions using a st reet address, Global Posit ioning Syst em ( GPS) coordinat es, or lat it ude and longit ude? I n what form will t he result s be ret urned? Can I ask for opt ional inform at ion or m ust I m ake m ult iple request s? And what prot ocol do I use t o send t he request ? We could use a variet y of prot ocols t o im plem ent XML Web services, but a st andard way of doing t he j ob would m ake sense in t his cooperat ive endeavor. SOAP seem s like a nat ural fit for m uch of t he j ob we need t o accom plish. Our request for a shipping calculat ion is essent ially a rem ot e procedure call. Wit h t he addit ion of a few addit ional elem ent s, we have t he foundat ion for a powerful new way t o build web- based applicat ions. A direct ory of XML Web services would be useful, one we could consult t o see which shipping carriers offered such a service, for exam ple. I t is beyond t he scope of t his chapt er t o provide a com prehensive descript ion of t he work in t his area, but one effort leading t o such a direct ory is known as Universal Descript ion, Discovery, and I nt egrat ion ( UDDI ) . UDDI was creat ed t hrough t he collaborat ive effort s of Microsoft , Ariba, and I BM. I t encom passes a process for t he definit ion and creat ion of t he UDDI Business Regist ry ( UBR) and relat ed art ifact s, t he operat ion of t he UBR it self, and t he event ual licensing of t he int ellect ual propert y and cont rol of t he UBR and fut ure direct ions t o an independent t hird part y. The benefit s of UDDI include: z

all XML Web services are described in a similar fashion

z

UDDI only describes XML Web services, so it does it well

z

uses standard HTTP, so it is available anywhere on the Internet, including through firewalls.

The Passport Service Back- end " m egaservices" are a key com ponent t o m aking web services an at t ract ive way t o build applicat ions. Megaser vices are sim ply services designed t o handle a huge num ber of services calls every day. Microsoft has already delivered one such m egaservice in t he form of Passport . Already, several hundred part ners have rid t hem selves of t he problem s of building and m aint aining t heir own aut hent icat ion syst em s. This enables t hem t o reduce adm inist rat ive overhead. They can cut t heir t im e t o m arket significant ly and in a fast m oving age, t his is im port ant . They have out sourced t he building and debugging of t he soft ware. I t also m eans t hat t he users have a single sign- on t o any sit e t hat uses Passpor t aut hent icat ion. They don't have t o worry about rem em bering t he sign- on t o your web sit e. Passport consist s of t hree m ain services: z

Passport Single Sign-in service. Site developers can map sign-in names to information in their databases, which will allow them to offer Passport members a personal web experience through targeted ads, promotions, and content. Using Passport in this way can help increase customer loyalty, sales, and advertising revenues.

z

Kids Passport Service. This service is an optional part of Sign-in service and helps parents protect their children's online privacy by allowing them to determine whether their child can use participating web sites' services that collect personally identifiable information. Kids Passport is the first turnkey solution available to web sites for managing parental consent and helping sites comply with the Children's Online Privacy Protection Act (COPPA), which requires web sites to obtain parental consent before collecting and/or disclosing a child's personal information.

z

Passport Express Purchase Service This allows Passport members to store credit card and shipping information in one secured electronic wallet. The purchase process is streamlined because members do not have to retype this information. To make purchases, members click an express purchase button or text link next to the products they want to purchase. Then, they simply click to select the credit card and shipping address to use for the purchase, and the information is sent to the supplier's site to complete the transaction.

Businesses can im plem ent t he single sign- in service, t he express purchase service, or bot h. On our sam ple sit e we will be im plem ent ing Passport services.

The Role of SOAP Earlier in t his chapt er, we t alked about what SOAP is. I t is also im port ant t o underst and what SOAP is not . z

It does not perform distributed garbage collection.

z

It has no type safety, or versioning, or boxcarring or pipelining of messages.

z

It does not use objects-by-reference.

z

It has no activation (which requires objects-by-reference).

I t builds on HTTP securit y, bot h HTTPS and X.509 cert ificat es. We can choose which m et hods t o expose explicit ly. There is no applicat ion code included in SOAP m essages. I t is also firewall- friendly.

SOAP on a ROPE (The SDK for VS6) I f you have an exist ing COM com ponent , t he web services SDK m akes it easy t o t urn it int o a web service and t o consum e it in VB. I t includes a t ool t hat will ext ract t he t ype library from an exist ing COM com ponent and t urn it int o an XML " cont ract " t hat represent s t hat com ponent 's capabilit ies. ( You can also generat e t his cont ract by hand.) Once you have a cont ract , it includes an ext ension for VB t hat will aut om at ically t urn t he cont ract int o a proxy t hat you can program against j ust like a local COM com ponent ( com plet e wit h VB feat ures such as I nt elliSense) . The SDK also cont ains a " list ener" t hat will receive SOAP calls t hat com e in and direct t hem t o t he appropriat e service. This Toolkit enables you t o: z

Expose a service. If the service you want to expose is a COM component, you can just run the Toolkit's "dehydrator," which extracts an XML service description. Otherwise, you can hand code your service description according to the rules published in the SDL specification.

z

Consume a service. Once the service description is generated, the Toolkit's automatic Remote Object Proxy Engine (ROPE) pulls it down into Visual Basic and instantiates it as a VB proxy that the programmer can treat as though it were a local COM object.

z

Run against a service. When you then run the client application, the proxy makes calls across the network using SOAP to the Toolkit's Listener application (either the ASP or ISAPI version). The Listener then handles starting up the XML Web serviceand interacting with the actual application.

Tip: Remember, you can expose XML Web services using any programming language, object model, or operating system Your XML Web Services can also communicate with any other programming language, object model or operating system.

DISCO and UDDI The Discovery of web services ( DI SCO) and UDDI are bot h concerned wit h finding what services a web sit e can offer. Before you can access a web service, we need t o know where it is, what it s capabilit ies are, and how t his is form at t ed. Web service discovery is t he process of locat ing and int errogat ing web service descript ions. We can use a program t o carry out t he discovery when a web service publishes a discovery ( .disco) file, which is an XML docum ent t hat cont ains links t o ot her resources t hat describe t he web service. The following shows an exam ple of t he st ruct ure of a discovery docum ent :

is t he t op- level node, all elem ent s and at t ribut es used t o aut hor a schem a m ust appear bet ween t he begin and end < schem a> t ags. The specificat ion also describes t he exact elem ent s and at t ribut es aut hors m ay ut ilize t o develop XSD schem as. Let 's work by exam ple. List ing 3.2 shows t he XSD schem a t hat defines t he household pet XML docum ent t hat was st udied earlier in t he chapt er. List in g 3 .2 H ou se h old Pe t s XSD Sch e m a

Team-Fly®

























As we can see, t he schem a is m ore com plex t han it s inst ance. Our household pet schem a, like all schem a, begins wit h t he < schem a> elem ent . Looking t hrough t he list ing we find < elem ent > declarat ions for our household, pet s, pet , breed, nam e, color, and weight elem ent s as well as an < at t ribut e> elem ent t hat defines t he t ype at t ribut e of t he < pet > elem ent . Propert ies such as MinOccurs and MaxOccurs define t he m inim um num ber of t im es a t ag can occur. For inst ance, t he < pet > t ag has a MinOccurs value of 0 and an unbounded MaxOccurs value, indicat ing t hat for any household, t here can be 0 or m ore pet s.

Note: The XSD schema in Listing 3.2 was generated using the Microsoft .NET Frameworks XML schemas support utility (xsd.exe). This program allows solution developers to efficiently interoperate between schemas and .NET classes and is available in the Microsoft .NET Frameworks SDK.

Up t o t his point we've discussed XML and st udied a sim ple docum ent inst ance. We've learned how t he separat ion of cont ent and present at ion is im port ant in bringing t oget her dat a from different sources. We t alked briefly about XSD Schem a and looked at an exam ple declarat ion for our household pet exam ple. I n t he next sect ion, we will discuss t echnologies t hat ext end XML t o creat e a new set of t ools t hat facilit at e int er- applicat ion com m unicat ions.

SOAP and Web Services The Sim ple Obj ect Access Prot ocol ( SOAP) and Web Services play a m aj or role in t he Microsoft .NET Ent erprise Solut ions plat form . A Web Service is a piece of applicat ion code m ade available over t he Web via st andard I nt ernet prot ocols such as HTTP and TCP/ I P. SOAP is a light weight prot ocol t hat t ravels on t op of HTTP. I t provides t he inform at ion t ransport layer for applicat ion com m unicat ion via Web Services. Soft ware engineers developing solut ions t hat t arget t he Microsoft .NET Ent erprise Solut ions Plat form build applicat ions t hat exchange dat a bet ween various Web Services producing a single, cohesive dist ribut ed soft ware applicat ion. Sim plicit y is t he fundam ent al principle behind t he developm ent of SOAP. SOAP does not int roduce new t echnological innovat ions but inst ead builds upon reliable, well known I nt ernet st andards like XML and HTTP. As a result , soft ware engineers can leverage exist ing knowledge of t he I nt ernet t o develop SOAP- based Web Services t arget ing t he Microsoft .NET Ent erprise Solut ions Plat form . That said, we can form ulat e t hree im port ant point s about what SOAP is: z

SOAP is formatted using XML and transported through HTTP requests.

A client sends a SOAP message to

a server via HTTP and the server performs application logic based on the client request. z

SOAP needs HTTP, nothing else. SOAP is not concerned with operating systems, development languages, or application object models.

z

SOAP works with existing firewalls, routers, and proxy servers. A network administrator does not have to go out and buy new hardware to support SOAP-based software systems.

Now t hat we have covered what SOAP is, we will discuss t he t wo dist inct t ypes of SOAP m essages: 1) Client s sending SOAP m essages t o a server do so using a Call m essage, and 2) Server responses sent back using a Response m essage. List ing 3.3 illust rat es a Call SOAP m essage.

List in g 3 .3 SOAP Ca ll M e ssa ge

POST /RegisterPets HTTP/1.1 Host: www.gasullivan.com Content-Type: text/xml Content-Length: xxxx SOAPMethodName: http://www.gasullivan.com/PetService





The first four lines in List ing 3.3 are com m on HTTP synt ax. POST is t he request t ype. Host nam e, Cont ent - Type, and Cont ent - Lengt h are required for all HTTP m essages. Not ice t he Cont ent - Type of t ext / xm l. XML keeps popping up everywhere! The t ext / xm l value indicat es t hat t he payload of t his HTTP request is XML. I n fact , t he SOAP m essage is a well- form ed XML docum ent com prised of a required < SOAP: Envelope> elem ent , a required < SOAP: Body> elem ent , and an opt ional < SOAP: Header> elem ent . The < SOAP: Header> is not shown in t he list ing but would exist bet ween t he Envelope and Body elem ent s. A server receiving t his m essage sees t he SOAPMet hodNam e in t he HTTP Header and execut es an AddPet sToRegist ry procedure call t o add our household pet s t o a fict it ious pet reposit ory at G.A. Sullivan. List ing 3.4 below shows a response t o t he SOAP Call m essage above. List in g 3 .4 SOAP Re spon se M e ssa ge HTTP/1.1 200 OK Content-Type: text/xml Content-Length: xxxx

elem ent cont aining a < ret urn> elem ent . The act ual payload of t he response would be an XML docum ent t raveling as a child of t he < ret urn> elem ent .

We previously discussed how SOAP t ransport s XML docum ent s bet ween Web Services. By underst anding t his sim ple call/ response m odel, we are now st art ing t o realize why Web Services, t oget her wit h XML and SOAP, are so fundam ent ally im port ant t o t he Microsoft .NET Ent erprise Solut ions Fram ework. Web Services allow com m unicat ion bet ween disparat e applicat ions, anywhere on t he I nt ernet . Since t hey ut ilize t he indust ry st andard XML prot ocol, Web Services even enable com m unicat ion bet ween different web servers and plat form s. For m ore inform at ion on building and consum ing web services, see Chapt er 11, " Purchasing a Ticket " .

WSDL, DISCO, and UDDI A Web Service accept s m essages from a client , perform s work based on inform at ion cont ained in t he m essage, and sends a response back t o t he client . How does a client , or Web Service consum er, learn about t he m et hods a Web Service exposes? This process is enabled t hrough WSDL, t he Web Service Descript ion Language. This sect ion explains WSDL and how it fit s int o t he Web Services pict ure. WSDL is an XML specificat ion t hat developers use t o det ail which m et hods, param et ers, and prot ocols a Web Service exposes. WSDL can be loosely com pared t o a t ext - based COM t ype library t hat is not specific t o any part icular operat ing syst em plat form and act s as a cont ract bet ween a Web Service consum er and t he Web Service it self. I m agine a set of cool web services t hat exist at a sit e called gasTI X.com . The gasTI X Web Services allow cust om ers t o search for t icket s t o upcom ing concert s and sport ing event s. As a developers, all we know about is gasTI X.com and not specific endpoint s at t he sit e t hat expose WSDL docum ent s for you t o develop against . The Discovery Prot ocol ( DI SCO) defines an XML- based discovery docum ent form at , along wit h a prot ocol, giving you t he abilit y t o scour gasTI X.com for services of int erest . The discovery page list s all Web Services and t he m et hods available for t hose services. I t also det ails t he expect ed param et ers for each m et hod and a descript ion of t he ret urn value. Taking t his one st ep furt her, pret end we do not know t he specific URL for t he Web Services we are int erest ed in. The Universal Descript ion, Discovery, and I nt egrat ion ( UDDI ) specificat ion allows Web Service aut hors t o advert ise t he locat ion of t heir services and gives Web Service consum ers t he abilit y t o find t hese services. Note: For more information on SOAP and Web Services, see the following web links: z

Microsoft Web Services Developer Center: msdn.microsoft.com/webservices/

z

SOAP Specification Version 1.1: msdn.microsoft.com/xml/general/soapspec.asp

Web Services Description Language 1.1: msdn.microsoft.com/xml/general/wsdl.asp

Microsoft BizTalk Server 2000 Microsoft BizTalk Server 2000 is a .NET Ent erprise Class server product wit h powerful developm ent and execut ion feat ures capable of orchest rat ing business processes wit hin one organizat ion, as well as am ong m any organizat ions. BizTalk Server 2000 provides us wit h t he abilit y t o define XML- based business docum ent specificat ions, and t o configure what t ransform at ions m ust t ake place on t hese docum ent s as t hey t ravel bet ween soft ware applicat ions. BizTalk facilit at es applicat ion developm ent by allowing bot h

soft ware developers and business analyst s t o part icipat e in t he developm ent of business process soft w are applicat ions. BizTalk is designed t o provide us wit h a com prehensive int erface for m anaging t he t ypes of applicat ion int eroperabilit y issues t hat we have been discussing, which can oft en becom e quit e com plex. BizTalk is a t echnology t hat helps enable t he efficient im plem ent at ion of business processes and workflow wit hin an organizat ion. I t can im port XML and XSD schem as aft er init ial configurat ion cust om ized for a com pany's e- Business requirem ent s has been com plet ed. As a m em ber of t he .NET Ent erprise Server fam ily, BizTalk can com m unicat e wit h SOAP web services. The sect ions below are an int roduct ion t o t he prim ary applicat ion- level feat ures of BizTalk Server and are int ended as a foundat ion for lat er chapt ers about gasTI X.NET and Microsoft BizTalk Server 2000. For m ore inform at ion on t he im plem ent at ion of BizTalk in t he gasTI X applicat ion, refer t o Chapt er 12, " Fulfillm ent ."

Business Document Exchange Microsoft BizTalk Server 2000 provides a powerful execut ion engine t hat enables business docum ent exchange bot h wit hin and bet ween ent erprises. A business docum ent is a t ext docum ent , or m essage, t hat drives a business process.Exam ples include purchase orders, receivables st at em ent s, product specificat ions, or any ot her crit ical pieces of inform at ion on which businesses depend for t heir operat ions. Different soft ware applicat ions ut ilize different docum ent form at s. BizTalk Server 2000 helps int egrat e different applicat ions by support ing bot h XML and com m on flat file form at s. These m essage form at s are described below: z

XML documents. Any XML document that conforms to the XML 1.0 specification can be exchanged using Microsoft BizTalk Server 2000.

z

Delimited flatfiles can also be used with BizTalk. The data fields of delimited flatfiles are separated by a specified character delimiter. Common delimiters are commas, colons, pipes, and tab characters.

z

Fixed-length data fields comprise positional flatfiles. A collection of data fields on one row in the flatfile is called a record. Flatfile records can either be fixed or variable length depending the structure of the data.

I n order for BizTalk t o com prehend t he cont ent of t hese different dat a form at s, we m ust creat e m essage specificat ions. BizTalk m essage specificat ions are sim ilar t o t he XSD Schem a we learned about earlier in t his chapt er. They are blueprint s for what t ype of inform at ion a m essage can cont ain, and t hus enable BizTalk t o underst and specific rules for business docum ent exchange.

BizTalk Orchestration The highest layer of abst ract ion in BizTalk Server 2000 is Orchest rat ion. BizTalk Orchest rat ion is used t o design business processes t hat m anage overall applicat ion business logic. I n t he past , t he design and im plem ent at ion of a business process have occurred in t wo separat e pieces. First , a business analyst would gat her business requirem ent s and const ruct som e t ype of diagram , like a flowchart or int eract ion diagram , describing t he business process in a graphical m anner. Next , a soft war e developer would review t he business process diagram and m ap requirem ent s t o an im plem ent at ion m odel and perform t he coding and t est ing t o produce t he required soft ware solut ion.

The BizTalk Orchest rat ion Designer gives bot h t he business analyst and soft ware developer a com m on t ool for using when designing and im plem ent ing business processes. Business process design using BizTalk Orchest rat ion generally follows four dist inct st eps. These are: z

Business analysts create an XLANG Schedule drawing using a set of well known flowchart-like shapes. Each shape represents a logical step in the business process.

z

Software developers provide an implementation for the XLANG Schedule by attaching each shape in the drawing to software which implements that shape.

z

Orchestration Designer provides a way to visually define the flow of data between different shapes in the diagram and this work can either be accomplished by a business analyst or software developers.

z

The XLANG Schedule drawing is then compiled into an XLANG Schedule. XLANG Schedules are executable business process files used by the BizTalk runtime to complete the business process.

A neat feat ure of Orchest rat ion Designer is it s abilit y t o creat e long- running business processes. When designing an XLANG Schedule, we can configure it t o st ay alive for as long as it t akes t o com plet e t he business process. For exam ple, a book ordered over t he I nt ernet m ay have t o be ordered direct ly from t he publisher which could t ake weeks t o com plet e. BizTalk Orchest rat ion t reat s t his as a loosely- coupled process and is able t o com pensat e for added work required t o com plet e t he book order.

BizTalk Messaging BizTalk Messaging Services provide us wit h t he abilit y t o receive m essages and rout e t hem int o a business process, or t o send out m essages as a result of a process. Messaging enables business part ner int egrat ion, as well as int egrat ion of exist ing applicat ions by act ing as t he low- level t ransport and support layer for BizTalk Orchest rat ion Services. Messaging and Orchest rat ion are designed t o be used closely t oget her, wit h Messaging providing t he receipt and delivery capabilit y t hat Orchest rat ion relies on. The m aj or feat ures of BizTalk Messaging include t he following: z

Transport services

z

Data parsing

z

Data validation

z

Reliable document delivery

z

Security

Transport services represent t he different prot ocols and applicat ions t hat t ransm it business docum ent s t o t heir dest inat ions. BizTalk Messaging support s st andard I nt ernet prot ocols like HTTP, SMTP, and HTTPS, as well as file- based docum ent delivery. Cust om soft ware, t erm ed Applicat ion I nt egrat ion Com ponent s, can be also be used by BizTalk Messaging, as well as Message Queuing t echnology. All docum ent s t hat pass t hrough BizTalk Messaging are XML- based. We all know t hat a business process m ay consum e dat a t hat conform s t o a variet y of indust ry st andard form at s like ASNI X12, EDI FACT, or even use flat - file form at s. BizTalk Server provides a set of dat a parsers ( convert ers) t hat t ranslat e t hese

non- XML form at s t o XML. BizTalk Messaging even allows you t o cust om ize it s services and develop your own parsers for use wit h propriet ary dat a form at s. BizTalk enforces dat a int egrit y rules by validat ing each docum ent inst ance against a specificat ion. I f a docum ent inst ance does not conform t o specificat ions, it is t ransferred t o a t em porary holding queue for addit ional inspect ion. Reliable docum ent delivery ensures a docum ent reaches it s int ended dest inat ion in a reasonable am ount of t im e. BizTalk Messaging st ores each docum ent in a cent ral reposit ory. I n t he unlikely event of server failure, BizTalk allows ot her servers t o t ake cont rol and process orphaned docum ent s from t he cent ral reposit ory. BizTalk Server support s public key encrypt ion for docum ent s t ransm it t ed using BizTalk Messaging Services. On t he receive side of m essaging operat ions, BizTalk support s decrypt ion as well as signat ure verificat ion.

From Here I n t his chapt er we explored XML, SOAP and Web Services, and Microsoft BizTalk Server 2000. We discovered t he ways XML is t he underlying dat a form at for t echnologies like SOAP, Web Services, and BizTalk Server and how XSD Schem a can be used t o creat e blueprint s for XML docum ent s. We st udied how SOAP and Web Services enable applicat ion t o applicat ion com m unicat ion using st andard I nt ernet prot ocols like TCP/ I P and HTTP. We int roduced t he basic feat ures of BizTalk Server 2000. We discussed t he flow of event s for creat ing business process drawings using BizTalk Orchest rat ion. Also, we t alked about t he feat ures of Business Docum ent Exchange and BizTalk Messaging Services. To learn m ore about building Web Services and t he gasTI X exam ple, see Chapt er 11, " Purchasing a Ticket ." For m ore inform at ion on how BizTalk Server is used t o int egrat e t he gasTI X websit e wit h a t hird- part y fulfillm ent com pany, see Chapt er 12, " Fulfillm ent ." For a m ore det ailed discussion of t he XML, SOAP, or UDDI st andards, please visit t he websit es not ed in t his chapt er.

4 Tools of the Trade Microsoft 's release of t he .NET plat form m arks an im port ant st ep forward in soft ware developm ent . Several years in t he m aking, t he fram ework includes a com prehensive set of class libraries t hat provides a robust t oolset for developers, built from t he ground up wit h t he I nt ernet , XML, and dist ribut ed processing in m ind. St rong language int eroperabilit y, easier applicat ion deploym ent and versioning, aut om at ic m em ory m anagem ent , and SOAP Web Services support were all key design goals for t he .NET Fram ework. .NET delivers on t hese goals, providing an im proved developm ent environm ent t hat will m ake developers m uch m ore product ive for years t o com e. Alt hough JScript and Visual C+ + have been updat ed for .NET, t he prim ary languages t arget ed for t he .NET Fram ework by Microsoft are Visual C# .NET and Visual Basic .NET. These languages bot h fully m eet t he st at ed design goals of .NET. C# is a new language from Microsoft , m arket ed as providing t he Rapid Applicat ion Developm ent ( RAD) feat ures of VB, com bined wit h t he power of C+ + . The C# language synt ax will feel quit e fam iliar t o C+ + , Java, and even JavaScript developers. C# was designed and built wit h t he .NET Fram ework in m ind, and is t hus t he de fact o .NET language of choice. This new language is already garnering a great am ount of int erest in t he developm ent com m unit y, and is also at t ract ing Java developers who want t o t arget applicat ions for t he .NET plat form . Visual Basic .NET is a significant overhaul of t he popular Visual Basic 6.0 program m ing language. Many m aj or im provem ent s have been m ade t o t he language, great ly enhancing t he robust ness of t he t ool. This chapt er explores t he m ost im port ant feat ures of t he .NET plat form from an archit ect ural perspect ive. I t exam ines how .NET solves t he challenges part icular t o t he dist ribut ed I nt ernet - based syst em s being developed t oday. I t t hen provides an overview of t he t wo prem ier .NET languages, Visual C# .NET and Visual Basic .NET, along wit h t hought s about which language t o select for your own developm ent . Visual St udio .NET, t he lat est and m uch im proved release of Microsoft 's int egrat ed developm ent environm ent ( I DE) , will be reviewed in dept h.

.NET Architectural Tools Wit h t he int roduct ion of XML, SOAP, and Web Services, applicat ions can now be easily int egrat ed wit hout regard t o geographical locat ion or t he underlying operat ing syst em . This should bring about a bet t er online experience for end- users because applicat ions t hat we build can now com prehensively gat her and process dat a t hat m ight , in part , live on servers t hat som eone else owns and m aint ains. Wit hout XML Web Services, users are oft en forced t o m anually work wit h dat a from our sit e, and t hen go t o several ot her sit es t o com plet e a high- level t ask. I ndeed, t his idea of syst em int egrat ion and dat a sharing via XML is cent ral t o Microsoft 's .NET vision. Microsoft believes t hat vast int egrat ion will drive applicat ion developm ent over t he next several years, t ransform ing t he com m on user experience and bringing about t he " t hird generat ion" of I nt ernet applicat ions. Note: The Internet is entering its third generation of applications. First generation applications were static, read-only HTML pages, with little interactivity. The second generation, of which we are nearing the end, offered more interactivity. Users could make purchases online, submit product information requests, and participate in newsgroup discussions. Second generation Microsoft Internet applications were architected using Windows Distributed Internet Architecture (DNA) solutions, consisting of Active Server Pages (ASP), VB COM components, and SQL Server. Third generation applications will be driven largely by the acceptance of

SOAP, XML, and Web Services. Microsoft is offering.NET as a premier development environment and platform for these next-generation applications.

Myriad challenges face t he applicat ion designers archit ect ing and im plem ent ing t he dist ribut ed, I nt ernet - based syst em s t hat have exploded ont o t he scene in t he last few years. The sam e archit ect ural design goals t hat drove Windows DNA developm ent ( scalabilit y, m aint ainabilit y, reliabilit y, int eroperabilit y, ext ensibilit y) rem ain relevant in t his new landscape. By design, Microsoft cont inues t o provide m ore robust t ools for archit ect s and im plem ent at ion t eam s in order t o help m eet t hese goals. .NET represent s a m aj or st ep forward in t he evolut ion of I nt ernet - based applicat ion developm ent , and t his sect ion explores t he enabling foundat ional archit ect ure of t his new plat form .

.NET Framework Classes Alt hough t he .NET plat form includes a great num ber of different pieces, a large collect ion of classes called t he .NET Fram ework is one of it s m ost im port ant feat ures. Developers can t ake advant age of a t rem endous variet y of funct ionalit y t hrough t his fram ework, ranging from file I O and dat a access, t o t hreading and web UI const ruct ion. The .NET Fram ework classes are t he single API used t o program in .NET. Anyt hing and everyt hing we m ight want t o accom plish program m at ically in a .NET applicat ion, we will do t hrough som e class in t his fram ework. Because t he .NET Fram ework cont ains hundreds of classes, it can be overwhelm ing at first . However, it is vit al t hat we gain a solid underst anding of t he fram ework in order t o develop applicat ions effect ively for .NET. To help organize t he fram ework int o logical groupings, t he designers of .NET have cat egorized t he classes int o nam espaces. This m akes t he scope of t he .NET classes m ore approachable when we are first learning about t hem . Table 4.1 shows t he nam espaces t hat are t he m ost widely used in const ruct ing e- Business applicat ions wit h .NET. Many different funct ions occur in a t ypical e- Business applicat ion: dynam ically const ruct ing and serving Web pages, accessing dat abases, wrapping a t ransact ion around a set of funct ions, and calling SOAP m et hods on ext ernal servers. Such funct ionalit y is im plem ent ed t hrough t he following im port ant .NET nam espaces: Table 4.1 The m ain .NET nam espaces for e- Business applicat ions

Class Name

Functional Description

System.Configuration

Supports a variety of application install and uninstall classes; compatible with Windows Installer.

System.Data

Use this namespace to access relational databases such as SQL Server 2000 through ADO .NET classes.

An improved

construct called a DataSet provides the equivalent of an in-memory database, allowing operations on hierarchical representations of tables. System.Diagnostics

When something goes wrong in our application, we usually want to write an exception to the event log.

We may also

want to examine system performance counters, or create our own. Both types of functionality are found in this namespace. System.DirectoryServices

Features support for interacting with Active Directory Services (ADSI).

System.EnterpriseServices

This namespace is used when we need to enlist COM+ services such as transactions and object pooling. (In .NET, Microsoft still relies on the COM+ infrastructure built into Windows 2000.)

System.IO

This namespace includes file I/O and StreamReader/StreamWriter classes, used for reading and writing data to disk.

System.Messaging

This namespace is used when our application requires asynchronous processing based on Microsoft Messaging technology (formerly MSMQ).

System.Net

WebRequest and WebResponse classes are included in this networking namespace, along with support for other protocols such as TCP/IP and Sockets. This functionality is

AM FL Y

used to open networking ports and perform specific tasks over them. System.Runtime

Serialization methods such as the SoapFormatter and the BinaryFormatter are implemented in this namespace. Interoperability with unmanaged COM components is also

System.Security

TE

facilitated by this namespace. This namespace includes Cryptography and Policy classes for helping to lock down a .NET application.

System.Text

Classes in this namespace provide functionality for dealing with text-based data. StringBuilder can be used for efficient manipulation of strings, and RegularExpressions provide powerful string validation functionality.

System.Threading

This namespace includes classes for building multi-threading support into a .NET application.

System.Web

This large namespace includes support for the creation of Web Services, Web Forms, and for mechanisms such as output caching to improve the performance of a Web application.

System.XML

XML and XSL functionality, increasingly prevalent in Web applications, is found in this namespace. also included.

Team-Fly®

Support for XPath is

The following diagram present s a high- level view of pot ent ial e- Business applicat ion funct ionalit y m apped t o t he corresponding .NET Fram ework base class. We can use Figure 4.1 t o locat e t he t op- level nam espaces t hat we need t o program against when we have a specific e- Business requirem ent t o m eet :

Figure 4.1 A graphical v iew of t he m ain .NET e- Business nam espaces.

One approach t o proficiency wit h t he .NET Fram ework classes is focusing on fluency wit h t he nam espaces list ed above. We will probably use funct ionalit y provided by t hese m aj or nam espaces in nearly every .NET e- Business web applicat ion t hat we build. Specialized funct ionalit y required by a specific applicat ion can t hen drive furt her explorat ion int o t he less frequent ly used nam espaces. I f we concent rat e on t hese few nam espaces and learn t hem inside and out , t hey should provide us wit h alm ost everyt hing we need for building business applicat ions on t he Web.

Offering a single API for developers of all .NET languages is a big st ep forward. Previously, Visual C+ + developers eit her program m ed direct ly against t he Win32 API , against t he ODBC API , and against MFC or ATL. Visual Basic developers had t heir own language- specific class library ( VBRun) , RDO and ADO for dat a access, and only occasionally program m ed directly against t he Win32 API , wit h som e caveat s. I n t he .NET world, no m at t er whet her we program in t he Managed Ext ensions for C+ + , Visual Basic .NET, Visual C# .NET, or even a t hird- part y language such as COBOL .NET, we now only need t o learn a single API . This is a significant and welcom e shift in developing on t he Microsoft plat form . Program m ing against a single API will help facilit at e bet t er com m unicat ion bet ween t eam s working in different languages. Alt hough synt act ical im plem ent at ion m ay differ across languages, t he .NET Fram ework class library will now serve as a com m on point of reference. When developers creat e t he funct ionalit y cust om t o t he applicat ion t hey are im plem ent ing, t hey cat egorize t he various com ponent s in classes wit hin an applicat ion- specific nam espace. This allows client s t o declare and use t he applicat ion code in t he sam e m anner as t he .NET Fram ework base classes are used. For exam ple, in t he gasTI X sam ple applicat ion, all classes providing business funct ionalit y specific t o our applicat ion are im plem ent ed in a nam espace we have called " gasTI X" : namespace gasTIX.Data

{

using System;

using System.Data;

using System.Data.SQL;

public sealed class Category : DataAccess

{

C+ + and Visual Basic developers will now have m uch m ore in com m on t han ever before, increasing t he pot ent ial for knowledge sharing and for helping each ot her and t he proj ect s t hey are working on succeed. The cont ent ion bet ween t hese t wo cam ps has oft en been acrim onious, and has som et im es underm ined t he success of proj ect s because of language bias. I t has long been t im e for t hese groups t o work product ively t oget her, and .NET facilit at es t his im port ant t ransit ion t o m ut ual respect and co- product ivit y.

Common Language Runtime (CLR) The Com m on Language Runt im e ( CLR) is at t he heart of t he .NET plat form . The CLR is a run- t im e engine t hat com piles I L code int o nat ive m achine- language code when a Web page or m iddle- t ier m et hod is invoked. I t cat ches syst em except ions, m anages m em or y, and loads and unloads applicat ion code.

A m aj or feat ure of t he CLR is it s " writ e once, run anywhere" execut ion m odel. Prior t o being run by t he CLR, t he code we writ e in C# or VB.NET is com piled int o Microsoft I nt erm ediat e Language, as shown in Figure 4.2. Bet t er known as I L, t his low- level language is roughly sim ilar t o assem bly language.

Figure 4.2 .NET com pilat ion st ep 1, convert ing source code t o I L.

When I L code is first execut ed on a t arget m achine, it is com piled int o nat ive m achine language code for t hat plat form by an im port ant com ponent of t he CLR, t he .NET Just - I n- Tim e ( JI T) com piler. Figure 4.3 shows how code is JI T- com piled t o nat ive m achine language for each m et hod t hat is called, and is t hen cached for quick execut ion. Microsoft m ay choose t o release JI T com pilers for plat form s ot her t han Windows. This will allow .NET code t hat we writ e in C# on a Windows 2000 m achine t o not only run on Windows 2000 Servers, but also on Linux boxes if a JI T com piler is available for t hat plat form . There is no longer a separat e runt im e for VB, C+ + , and C# . No m at t er in which language we choose t o code, all source get s com piled t o I L. Theoret ically, t he I L produced by t he VB.NET com piler should be t he sam e as what is produced by t he C# com piler. ( Microsoft com es close, but t here st ill rem ain differences in t he I L generat ed by t he t wo com pilers.) A single runt im e is needed t o JI T- com pile and t hen execut e t he m anaged code, regardless of t he language in which t he code was init ially writ t en. Again, t his should help facilit at e com m unicat ion by developers across languages, since t hey are all t arget ing t he sam e runt im e, t he .NET CLR.

Figure 4.3 .NET com pilat ion st ep 2, convert ing I L t o nat ive code. Note: Unlike Java, there is no .NET virtual machine interpreting byte-code line-by-line.

.NET IL code is always

JIT-compiled by the CLR to the native machine language of the platform. Although Java JIT compilers exist for some platforms, Java code is normally executed as interpreted code by a virtual machine.

Generally speaking,

this should make the .NET execution environment faster than Java because .NET code runs in the platform’s native machine language.

However, the only .NET CLR currently available is for Windows, while a Java Virtual

Machine exists for several platforms.

The CLR feat ures aut om at ic m em ory m anagem ent , called Garbage Collect ion. The .NET runt im e decides when t o release obj ect references based on usage pat t erns. No longer are developers explicit ly required t o release obj ect references or free m em ory . This should m ean im proved applicat ion reliabilit y because fewer m em ory leaks can occur. Tracking down m em ory leaks can be a long, arduous t ask, and having t he CLR aut om at ically m anage t his m arks a big im provem ent in applicat ion reliabilit y. Caution: Remember that only memory resources are automatically managed by the CLR. We must still explicitly release other resources such as file handles and database connections.

Common Type System (CTS) / Common Language Specification (CLS) Anot her powerful archit ect ural feat ure of t he .NET plat form is t he Com m on Type Syst em ( CTS) t hat all languages share. We are no longer forced t o perform a special cast on a VB m em ber when calling a m et hod im port ed from a C+ + class. All languages now share t he sam e t ypes, shown in Table 4.2 ( alt hough aliases t o t hese base t ypes m ay differ per language) . The CTS enables t he cross- language int eroperabilit y t hat is a m aj or feat ure in .NET. As long as a language com plies wit h t he Com m on Language Specificat ion ( CLS) , base t ypes can be passed t o and from obj ect s writ t en in t hat language wit hout incom pat ibilit ies. Table 4.2 The .NET base t ypes, wit h VB.NET and C# aliases

Type Name

Description

Visual Basic .NET Alias

Byte

8-bit unsigned integer

Byte

byte

SByte

8-bit signed integer

N ot su ppor t e d

sbyte

Int16

16-bit signed integer

Short

short

Int32

32-bit signed integer

Integer

int

Int64

64-bit signed integer

Long

long

UInt16

16-bit unsigned integer

N ot su ppor t e d

ushort

UInt32

32-bit unsigned integer

N ot su ppor t e d

uint

UInt64

64-bit unsigned integer

N ot su ppor t e d

ulong

Single

single-precision (32-bit) floating-point

Single

float

Double

double

Visual C# .NET Alias

number Double

double-precision (64-bit) floating-point number

Char

Unicode character (a 16-bit character)

Char

char

String

immutable, fixed length string of Unicode

String

string

characters Decimal

12-byte decimal value

Decimal

decimal

Boolean

Boolean value (true or false)

Boolean

bool

DateTime

8-byte date and time value

Date

n o a lia s (use DateTime)

Obj ect s can also be inherit ed across languages because of t he CTS and t he CLS. This offers developers t he powerful abilit y t o im plem ent in C# an inherit ed abst ract base class writ t en in VB.NET. This is because all t ypes, even prim it ive t ypes such as Boolean and I nt 32, inherit from t he abst ract base class Syst em .Obj ect . At a m inim um , a cert ain am ount of funct ionalit y, including Equals( ) , Get HashCode( ) , and ToSt ring( ) , is guarant eed t o be support ed by each and every t ype, whet her built - in or cust om - im plem ent ed. Addit ionally, all variables are st rongly- t yped, m eaning we define variables wit h an explicit t ype such as I nt eger, rat her t han as a Variant . I n fact , t he concept of Variant does not exist in .NET. Alt hough we can st ill perform lat e binding when needed, t he m aj orit y of t im e we will declare variables wit h an explicit t ype and reap t he benefit s of a st rongly- t yped syst em . This should help cat ch m ore errors at com pile t im e, rat her t han having t he user see t hem at run t im e. Unsafe t ype conversions and uninit ialized variable assignm ent s will not be allowed t o com pile. ASP program m ers accust om ed t o declaring variables wit h only a Dim st at em ent will now enj oy declaring variables specifically as I nt eger or St ring, cat ching any errors at com pile t im e if an inappropriat e value is assigned. .NET languages adhering t o t he CLS share m any addit ional feat ures dealing wit h t ypes, propert ies, and ot her class m em bers. The CLS has been developed t o ensure m axim um language int eroperabilit y in t he .NET Fram ework, a feat ure t hat will provide new opt ions for program m ers current ly working wit h Microsoft t echnologies, and for program m ers working in languages t hat previously could not execut e on t he Windows plat form . COBOL program m ers, for exam ple, will now be able t o ext end t heir reach int o new sect ors and t ake advant age of t he I nt ernet when vendors for t his language develop .NET CLS- com pliant com pilers. Caution: Because .NET languages are required to meet only the minimum specifications set forth in the CLS, we should not expect non-CLS language features to be inheritable across language boundaries. A language that might inherit our class may not support the language-specific feature we implement (e.g. unsigned integers and operator overloading are valid in Visual C# .NET, but are not supported in Visual Basic .NET).

Metadata Met adat a, inform at ion t hat describes various charact erist ics of a piece of code, is widespread t hroughout t he .NET Fram ework. Met adat a is used by t he CLR t o locat e and load classes, t o allocat e m em ory for t ype inst ances, t o check securit y set t ings, and t o com pile I L code t o nat ive code. Met adat a enables t he richness of t he .NET design, debugging, and profiling t ools. Visual St udio .NET provides I nt elliSense support , and t he I nt erm ediat e Language Disassem bler ( I LDasm ) offers graphical inspect ion capabilit ies via m et adat a, helping developers opt im ize product ivit y. Met adat a is com piled direct ly int o an assem bly and cannot becom e separat ed from t he com ponent t hat it describes. This is a big im provem ent over t he COM m et adat a m odel, where t ype libraries ( TLB files) , I nt erface Definit ion Language ( I DL) files, or a variet y of ot her files were separat e from t he DLL com ponent s t hat t hey described. I f a TLB file was delet ed inadvert ent ly from a developer's syst em , t he

developer would no longer get I nt elliSense support for t he com ponent in Visual St udio. cannot occur in .NET.

This sit uat ion

Met adat a also enables a sim pler developm ent m odel called at t ribut e- based program m ing. .NET classes and m et hods can be m arked wit h various propert ies ( at t ribut es) which are st ored as m et adat a. These at t ribut es t ell t he CLR how t o handle cert ain aspect s of execut ion. Exam ples include SOAP- enabling a m et hod wit h t he [ WebMet hod( ) ] at t ribut e, and m arking t he COM+ 1.0 t ransact ion requirem ent s of a class wit h an at t ribut e such as [ Transact ion( Transact ionOpt ion.Required) ] . The following code list ing, from t he Updat eOrder class in t he Business nam espace, shows a few of t he at t ribut es used in gasTI X: namespace gasTIX.Business

{

using System;

using System.Data;

using System.Runtime.InteropServices;

using gasTIX;

using gasTIX.Data;

using System.EnterpriseServices;

[

ComVisible(true),

Transaction(TransactionOption.RequiresNew),

Guid("13DF3124-0995-464d-A0AE-1A965A51FF70")

]

public class UpdateOrder : ServicedComponent

{

public UpdateOrder()

{

}

[ AutoComplete ]

public void Add(OrderData order)

{

At t ribut e- based program m ing is anot her big im provem ent over t he COM/ DNA m odel, because at t ribut es are com piled int o t he code. I n DNA, even if a class was m arked wit h " Transact ion= Required," t his could be overwrit t en by an adm inist rat or wit hin t he MTS Explorer. Fault y program execut ion could t hen occur because cert ain condit ions m ight be encount ered t hat t he developer did not account for or int end. Cust om at t ribut es can also be im plem ent ed in t he .NET Fram ework. These special at t ribut es can be what ever we want t hem t o be, such as m et hod and propert y docum ent at ion. Ot her developers t hat use t he com ponent s int o which we com pile cust om at t ribut es discover t hem at runt im e using t he Syst em .Reflect ion classes. The .NET plat form sim plifies com ponent versioning because of m et adat a. Problem s wit h versioning were a cont inual source of frust rat ion in t he DNA environm ent , because alt hough we weren't supposed t o ever break an int erface, it happened all t he t im e. Versioning is facilit at ed in .NET t hrough a m aj or.m inor.build.revision num bering schem e at t he assem bly level. Built - in t o t he .NET CLR is a version- checking algorit hm t hat will look t o see if a com pat ible com ponent exist s. //

// Version information for an assembly consists of the following four values:

//

//

Major Version

//

Minor Version

//

Revision

//

Build Number

//

// You can specify all the value or you can default the Revision and Build Numbers

// by using the '*' as shown below:

[assembly: AssemblyVersion("2.1.4.*")]

I f t he m aj or or m inor num ber has changed, t he com ponent will be considered incom pat ible and will not be used. However, if t he build or revision num ber has changed, but t he m aj or or m inor num bers rem ain t he sam e, t he source is considered t o be basically t he sam e, and will be used. Windows DNA had no such version- checking schem e built - in. When a shared assem bly is inst alled int o a m achine's Global Assem bly Cache ( GAC) , it will creat e a subdirect ory for it wit h a unique nam e. This allows t wo versions of t he sam e com ponent , ident ically nam ed, t o be inst alled on a m achine. Client s t hat are dependent on t he first version can st ill use t hat version, while newer client s can t ake advant age of t he new funct ionalit y in t he second version. Tip: Method overloading also helps improve versioning in .NET. new parameter to after it had been in use for a time? further defined during a normal project lifecycle.

Ever had a method that you wanted to add a

This occurs often, since requirements change and are

In Windows DNA this meant breaking the interface, causing

potential incompatibilities and errors. In .NET, simply add another method with a different parameter list. The runtime will determine which method to call based on the number and type of parameters.

Old clients will

not break, even though a new method signature has been added.

Deploym ent becom es sim pler in .NET due t o t he prevalence of m et adat a. Since t ypes are self- descr ibing t hrough t heir com piled- in m et adat a, t here is no longer a need t o regist er t ypes or classes in t he Regist ry . File locat ions are specified in t he assem bly m anifest , and t his m et adat a cannot becom e separat ed from t he I L code it describes ( w hereas wit h COM, regist ry ent ries could be inadvert ent ly delet ed) . See Chapt er 16, " Deploying t he Applicat ion," for m ore inform at ion on deploym ent in .NET. Tip: Deploy assemblies as private whenever possible. Private assemblies live in the application folder and are not registered in the GAC.

Assemblies should only be deployed as shared when they contain functionality that

several applications will use.

The CLR has to go through more work when resolving types within shared

assemblies.

Rem ot ing in t he .NET Fram ework is also enabled t hrough m et adat a. Rem ot ing is t he concept of inst ant iat ing a m et hod on som e obj ect on a rem ot e m achine. The CLR uses t he m et adat a of t he t ype and m et hod signat ure t o allocat e t he proper am ount of m em ory for t he m et hod call before it is serialized. For a det ailed discussion of rem ot ing concept s, see Chapt er 13, " .NET Rem ot ing."

Server Integration and Device Independence Microsoft believes t hat XML and Web Services, st andardized in t he SOAP prot ocol, will t ransform t he way t he I nt ernet is used over t he next few years. The abilit y t o call funct ions on a server som ewhere on t he I nt ernet is a powerful m odel ( see Figure 4.4) . I nst ead of designing, writ ing, and debugging a t horny t ax calculat ion algorit hm , we can now search at a sit e such as www.uddi.org for a t rust ed aut horit y t hat already provides such a calculat ion, callable t hrough a SOAP Web Service. This let s us focus on what we

Figure 4.4

AM FL Y

do best ( im plem ent t he business funct ionalit y cust om t o our applicat ion's needs) , and let s ot hers focus on what t hey do best ( worry about t hings like t horny, ever- changing t ax calculat ions) .

.NET facilit at es server int egrat ion and device independence.

TE

Because Microsoft firm ly believes in t his evolving m odel, deep support for SOAP has been incorporat ed in t he .NET plat form . XML, XSD, XPat h, XSL, and ot her st andard prot ocols are all built int o t he plat form and fram ework in various ways. A visual designer for XML and XSD schem as is built - in t o Visual St udio .NET ( see t he Visual St udio .NET sect ion lat er in t his chapt er) . I nt egrat ion wit h BizTalk Server 2000- generat ed schem as is sim ple, as BizTalk export s t ransform ed XML docum ent s. See Chapt er 12, " Fulfillm ent / Order Hist ory," for det ailed inform at ion on how BizTalk is int egrat ed int o t he gasTI X sit e. Providing support for t he variet y of client devices t hat m ight access our web applicat ion is j ust as im port ant as providing back- end int egrat ion wit h ot her servers. Ever spent long hours t rying t o get an HTML page t o look and behave t he sam e in bot h I nt ernet Explorer and Net scape Navigat or? Cross- browser com pat ibilit y has been a m aj or headache ever since DHTML support in t he browsers diverged wit h version 4. The designers of .NET have underst ood t he significant cost t o proj ect budget s relat ed t o t his issue, and have addressed it in t he lat est release of Act ive Server Pages ( ASP) t echnology, known as ASP .NET. When const ruct ing a page, a Web Form will read t he HTTP_USER_AGENT header and det erm ine what t ype of device is request ing a page. I t will render JavaScript / DHTML t o m ore int elligent browsers, and HTML 3.2 t o " down- level" browsers ( for m ore inform at ion, see Chapt er 5, " I m plem ent ing.NET Present at ion Services" ) . Because t his t ype of cross- browser program m ing is now built - in t o t he fram ework, t he life of t he everyday web developer will be m ade easier. Part of t he long- t erm vision for .NET is t o provide t he inform at ion a user m ight want on any device. Wireless device usage cont inues t o grow rapidly, and Microsoft has released support for t hese devices and

Team-Fly®

for t he Wireless Markup Language ( WML) in t he .NET Mobile SDK. This t echnology will det ect t he t ype of m obile device request ing t he page and render accordingly, sim ilar t o t he int elligent rendering capabilit y of ASP .NET. This enables our .NET applicat ions t o be accessible from " any device, any t im e," m eet ing part of t he prim ary .NET vision. During t he design phase for a t ypical e- Business applicat ion, we should t hink about t he funct ionalit y we want t o offer t o our end users in t wo very different ways. First , consider t he norm al Web browser client t hat will be accessing our applicat ion, probably at a resolut ion of 800x600 or higher. This im plem ent at ion should be very rich and full- feat ured, offering t he m ost powerful online experience. Next , consider how t o present t he applicat ion t o m obile device users. We m ight port a lot of funct ionalit y t o t he m obile space, or perhaps a scaled- down subset of t he applicat ion would be m ore appropriat e. Because we need t o design for sm all- screen wireless devices, we will likely end up providing users wit h a significant ly different experience. More and m ore people will soon be browsing our sit es wit h Pocket I E or som e ot her m obile browser, so any new applicat ion we develop should rem em ber t hese users. .NET helps us t arget t hese users by providing a t oolset t hat m akes t his aspect of Web developm ent easier.

Security and Authentication All plat form s m ust incorporat e st rong securit y m easures t o obt ain widespread adopt ion. Microsoft has learned t his lesson, som et im es t he hard way, over t he course of t he last few it erat ions of it s Windows operat ing syst em and I I S web server. .NET includes robust securit y feat ures such as public key signing of .NET com ponent s, role- based securit y support , and run- t im e m anaged code verificat ion checking. Passport , Microsoft 's ident it y t echnology t hat enables users t o login t o a variet y of part ner sit es wit h a single I D and password, is fully int egrat ed int o t he .NET Fram ework. Passport is a key st rat egy for Microsoft in offering soft ware as a service ( t he Windows XP operat ing syst em even support s Passport as a login opt ion) . Because of it s cent ral role and im port ance t o fort hcom ing t echnologies, we should st rongly consider im plem ent ing Passport Single Sign- I n ( SSI ) t echnology int o our e- Business applicat ions. This will set t he st age for t aking advant age of t he services collect ively known as "HailSt orm ." HailSt orm services will be host ed by Microsoft and will be accessed via Passport . Basic services such as MyCalendar, MyI nbox, MyLocat ions, MyNot ificat ions, and ot hers will be included. Microsoft is com m it t ing large resources t o st abilizing it s dat a cent ers and securit y, hoping t o convince a large num ber of users t o allow t heir dat a t o be host ed, furt hering t he concept ual shift t o a service m odel from a m odel in which soft ware is largely host ed locally. Many com put er users m ove from com put er t o com put er several t im es during t he day. Having dat a residing in t he I nt ernet " cloud," accessible from any m achine ( or even any wireless device) , is a big usabilit y win. No longer will we be forced t o m anually synchronize or updat e cont act s and inboxes. These services will creat e new business opport unit ies for com panies, and a m ore powerful experience for users. Passport is t he key t o t his world, so invest igat e it furt her if you plan on int egrat ing wit h t hese services in t he fut ure. Note: Passport version 3.0 is the official .NET release that brings full integration with the .NET Framework base classes.

HailStorm-enabled web services should begin to emerge after the official HailStorm release

sometime in calendar year 2002.

Performance and Scalability Microsoft has cont inued t o m ake great st rides in im proving t he scalabilit y and perform ance of it s product s. Many new feat ures have been included in t he ASP .NET release specifically aim ed at im proving web applicat ion perform ance and scalabilit y. I I S session st at e provides developers wit h t he abilit y t o m aint ain inform at ion about a Web sit e user bet ween page request s. I t can now be ut ilized wit hout scalabilit y concerns, as session inform at ion for web farm s can be st ored in a SQL Server or on a separat e m achine running an inst ance of t he Session St at e service. ASP .NET is now a com piled environm ent , unlike it s predecessor, which was an int erpret ed script ing environm ent . Any code parsed line- by- line will im plicit ly be slower t han t he sam e code com piled, all ot her t hings being equal. ASP .NET also includes out put caching, a t echnology t hat allows frequent ly viewed pages t hat are m ore st at ic in nat ure t o be cached on t he web server for subsequent request s. This way, only t he first user who request s a page will experience t he full wait for page processing. Subsequent users will receive t he page in t he t im e it t akes t o push it over t he wire. This m echanism is configurable, so param et ers can be set on a page- by- page basis. Because all .NET languages can be m ult i- t hreaded, any com ponent s writ t en in .NET can t ake advant age of COM+ 1.0 obj ect pooling. This is especially helpful for com ponent s t hat t ake a long t im e t o init ially load. Mult i- t hreading it self great ly im proves t he perform ance of com ponent s. Aut om at ic error and overload det ect ion t o rest art and m anage applicat ions and com ponent s is support ed for ASP .NET applicat ions. This is a powerful feat ure t hat will prevent m any applicat ions from locking up t he server. Every 20 m inut es or every 5000 page request s, a " prevent ive rest art " is perform ed for each ASP .NET worker process. The cache is cleared and m em ory is released, prevent ing problem s t hat can arise when applicat ions run unchecked for hours on end.

.NET Architectural Tools Summary The .NET Fram ework brings t rem endous im provem ent s in t he abilit y t o develop robust , ent erprise- class solut ions. A host of archit ect ural feat ures sim plifies com m on I nt ernet developm ent t asks, ensures cross- language int eroperabilit y, and im proves overall program execut ion. As a result , well- designed applicat ions t arget ed for t he .NET plat form will be highly scalable, m aint ainable, reliable, and ext ensible. They will also int eroperat e well wit h ot her I nt ernet applicat ions, a feat ure designed int o t he plat form from t he beginning. Now t hat t he m ost im port ant archit ect ural charact erist ics of .NET have been exam ined, let 's look at how t he t wo prim ary .NET languages, Visual C# .NET and Visual Basic .NET, im plem ent t hese concept s.

.NET Language Tools

Microsoft has int roduced a new language called Visual C# .NET t hat has been designed specifically wit h t he .NET Fram ework in m ind. The popular Visual Basic 6.0 language has also been given a significant overhaul t o com ply wit h t he .NET Com m on Language Specificat ion. This sect ion explores t he m aj or feat ures of t hese t wo languages and provides som e insight s on which language t o choose when beginning a proj ect .

Visual C# .NET Visual C# .NET is a new language t hat has been in developm ent at Microsoft for several years. I t is gaining wide accept ance at Microsoft ; in fact , part s of ASP .NET and Visual St udio .NET are writ t en in C# . The Visual C# .NET language was designed specifically t o t ake full advant age of t he .NET Fram ework. I t is a com ponent - based language by design, wit h support for m et hods, propert ies, and event s. I t also feat ures full support for t he obj ect - orient ed program m ing ( OOP) concept s of inherit ance, polym orphism , and encapsulat ion. C# , as it s nam e im plies, m aint ains m any basic feat ures of t he C and C+ + fam ily of languages, but wit h a new beginning. I t was indeed t im e t o design a new language from t he ground- up wit h OOP, t he I nt ernet , and com ponent s in m ind. C# is t he product of t his new st art . I nt ernet feat ures are available in C+ + , but largely as add- on feat ures. Java is designed for t he I nt ernet , but XML and SOAP support is lacking. C# is now t he m ost m odern developm ent language available, fully support ing t he lat est t ypes of I nt ernet applicat ions. The overwhelm ing num ber of C+ + const ruct s and m acros proved t oo high a barrier of ent ry for m ost new program m ers. To rem edy t his, C# does away wit h t he m any anachronism s of t he C+ + language. I t provides newer program m ers wit h a m uch m ore concise, int egrat ed t oolset which st ill enables building robust , int eroperat ing applicat ions. The power of C# is provided by t he .NET Fram ework base classes, rat her t han t hrough a variet y of different API s as in C+ + . Novice developers will now be able t o dive m or e quickly int o a full- feat ured, m odern language t han ever before. Java program m ers will also be at t ract ed t o t he C# program m ing language. Because Java is a C- st yle language, t he synt ax of C# will feel very fam iliar t o Java developers. I nt erest ingly enough, m any web developers who have program m ed JavaScript for client - side validat ion in HTML pages will also feel at hom e in C# because JavaScript synt ax is sim ilar t o C# synt ax.

Visual Basic .NET The lat est incarnat ion of t he popular Visual Basic program m ing language is called Visual Basic .NET. This is t he m ost significant overhaul of VB t o dat e. Support for OOP concept s such as inherit ance, polym orphism , and encapsulat ion are included. This m arks a very different way t o t hink for VB program m ers, and m ay present one of t he biggest challenges for current VB developers in m oving t o t his new version. Mult i- t hreading support is also included in VB.NET. I n previous versions of Visual Basic, program m ers could only writ e single- t hreaded program s, great ly lim it ing applicat ion perform ance. Wit h m ult i- t hreading support , Visual Basic applicat ions can now perform on a par wit h lower- level languages such as C+ + . This is anot her new way t o t hink for VB developers- - new concept s such as t hreading synchronizat ion m ust be m ast ered.

St ruct ured except ion handling is anot her new feat ure of Visual Basic .NET. Clum sy VB On Error Got o synt ax is replaced wit h t he m ore logical t ry..cat ch..finally synt ax. Met hod overloading now enables developers t o call different im plem ent at ions of t he sam e m et hod nam e by passing different param et ers. Type safet y is a welcom e feat ure of Visual Basic .NET. I n VB 6.0, undesired t ype conversions would som et im es occur when t he program m er did not int end it. Wit h t he int roduct ion of t he Opt ion St rict direct ive in VB.NET, t hese t ypes of errors cannot occur, and will be caught at com pile t im e. Wit h all t hese new feat ures, Visual Basic .NET offers m ore power and flexibilit y t han ever before t o it s loyal legion of followers.

Language Choice A great debat e is current ly raging over which .NET language t o use on fut ure developm ent proj ect s: Visual C# .NET or Visual Basic .NET. This is an im portant quest ion, but it does not have easy answers. The decision of which language t o use should be t he result of carefully weighing developm ent st aff st rengt hs and weaknesses, along wit h current m arket t rends. I t is st ill t oo early in t he life cycle of t hese languages, and t he .NET plat form in general, t o reach a subst ant ive conclusion about which of t hese t wo languages will dom inat e in t he m arket place. However, t he following t hought s provide insight s which m ay help wit h t his decision. Alt hough t he languages are nam ed aft er C/ C+ + and Visual Basic, neit her Visual C# .NET nor Visual Basic .NET bears m uch resem blance t o t heir predecessors. C# bears m ore resem blance t o Java t han it does t o C or C+ + . VB.NET has changed so m uch ( prim arily in order t o support OOP concept s) t hat it requires a significant shift in m indset for current Visual Basic program m ers t o m ake t he t ransit ion. I f VB program m ers m ake t he assum pt ion t hat VB.NET program s should be im plem ent ed sim ilar t o Visual Basic 6.0 program s, t hey will m ake serious program m ing errors. Part of t he popularit y of Visual Basic 6.0 is it s sim plicit y. Business m anagers have been able t o put t oget her sim ple applicat ions t o m eet t heir needs wit h lit t le or no help from I T depart m ent s. Taking advant age of t he significant st rides m ade wit h t he release of VB.NET will require a fairly significant learning curve. I t is not cert ain t hat t he business m anagers who used VB 6.0 so successfully will be able t o use VB.NET in a sim ilar fashion. There is a subst ant ial base of VB program m ers t hat need an upgrade pat h int o t he .NET world. Conservat ive est im at es st at e t hat t here are over t hree m illion Visual Basic program m ers t oday. I f a significant part of t he VB world m oves t o VB.NET, t here will be a large base of support for VB.NET program m ing, regardless of it s st at ure as a language. Microsoft has used Visual C# .NET as t heir program m ing language of choice for const ruct ing part s of t he .NET infrast ruct ure. The C# com piler is likely bet t er opt im ized because of t he heavy focus on t his new language at Microsoft . However, consider t hat C and C+ + were used t o writ e m ost of t he exist ing Windows infrast ruct ure and program s, yet VB becam e t he language of choice for business applicat ions, not C or C+ + . Growing accept ance at Redm ond, however, m akes t he fut ure of t he C# language m ore cert ain.

There are a few advanced funct ions in C# t hat are not available in VB.NET. Operat or overloading, unsigned int egers, and XML docum ent at ion support are a few of t he m ore not able. Alt hough t hese feat ures m ay be released in a fut ure version of VB.NET, t hey are t hree exam ples of funct ionalit y t hat C# provides t oday t hat VB.NET does not . Tip: Regardless of the language in which we decide to implement our .NET applications, becoming a .N ET de ve lope r should be our goal.

A ".NET developer" will focus heavily on mastering the .NET Framework base

classes, and will thus be able to jump from Visual Basic .NET to Visual C# .NET and back with little difficulty. Much of the challenge in coming up to speed on .NET is learning how to effectively apply the wide variety of functionality found in the base classes.

Proficiency with the .NET platform as a whole is often times more

valuable to companies and projects than focusing deeply on a single .NET language.

Other Languages Alt hough t his chapt er has dealt only wit h Visual Basic .NET and Visual C# .NET, Microsoft has also released an updat e t o it s Visual C+ + and JScript languages. The Managed Ext ensions for C+ + enables C+ + developers t o t arget applicat ions for t he .NET Fram ework, and t o call int o unm anaged, " unsafe" C+ + code when point ers and ot her const ruct s not allowed by t he CLS are needed. JScript is now a com piled language, wit h support for OOP. Alt hough t hese t wo languages are not garnering a great am ount of int erest from t he developm ent com m unit y at large, t hey will cont inue t o have t heir loyal devot ees, and Microsoft is com m it t ed t o t hem in t he near fut ure. Com pilers for ot her languages adhering t o t he CLS will soon be released, enabling a large variet y of languages t o run on t he .NET plat form . Developers skilled in Ada, APL, COBOL, Fort ran, Pascal, Perl, Pyt hon, Sm allTalk, and several ot her languages will now be able t o m arket solut ions for t he .NET plat form . They will get a new lease on life by having t he power of t he .NET Fram ework at t heir fingert ips, as far as t heir respect ive com pilers include support for .NET plat form const ruct s. Microsoft 's upgrade pat h t o .NET for current Visual J+ + and Java developers is called t he Java Upgrade Migrat ion Plan ( JUMP) . The J+ + language will not be upgraded t o .NET largely because of t he lit igat ion over t he past few years bet ween Microsoft and Sun, and because t he com binat ion of C# and .NET offers a viable alt ernat ive. Microsoft will cont inue t o support J+ + 6.0, so any solut ions based on t his language will have Microsoft 's support for now. However, t he fut ure of t his language is uncert ain, and it is quit e likely t hat Microsoft will not release addit ional upgrades. Any J+ + applicat ions should probably be port ed t o C# and .NET if business requirem ent s warrant new funct ionalit y, or if t he applicat ion m ust live for several m ore years. ( See m sdn.m icrosoft .com / visualj / j um p/ default .asp for m ore det ails on t he JUMP init iat ive.)

.NET Language Tools Summary Bot h Visual C# .NET and Visual Basic .NET provide developers wit h t he m ost m odern program m ing const ruct s, including support for com ponent and obj ect - orient ed program m ing. These languages enable developers t o t ake advant age of t he vast am ount of funct ionalit y found in t he .NET Fram ework base classes.

Choosing a .NET language in which t o work can be difficult , and depends on m any fact ors. Mast ering t he .NET Fram ework base classes should be t he prim ary goal of any com put er professional desiring t o part icipat e in t he burgeoning .NET world. The language chosen t o im plem ent t he underlying const ruct s does not hold t he sam e im port ance as it once did because all languages now sit on t op of t he sam e API and CLR.

.NET Developer Productivity Tools The Visual St udio .NET developm ent suit e and ot her im port ant ut ilit ies such as t he .NET disassem bler I LDasm bring great product ivit y gains for developers. This sect ion will explore t he new feat ures of Visual St udio, along wit h several ot her im port ant .NET t ools.

Visual Studio .NET Microsoft has significant ly reworked t he Visual St udio developm ent environm ent . I t feat ures a single I DE for all languages, support for debugging across language boundaries, Web Services support , XML / XSD visual design t ools, and a host of ot her usabilit y enhancem ent s. Single IDE The .NET release of t he Visual St udio product suit e finally realizes t he vision of an all- encom passing I DE. Microsoft 's single I DE is enabled by t he cross- language feat ures of t he .NET Fram ework such as t he CLR and t he CLS. Whet her we program in Visual C# .NET, Visual Basic .NET, or Managed Ext ensions for C+ + , we will use Visual St udio .NET as our m ain developm ent environm ent . Having j ust one I DE t o work wit h is a great product ivit y gain for developers. I n past incarnat ions of Visual St udio, each language cam e wit h it s own I DE, wit h different funct ion keys, m enu bars, and various quirks. Swit ching back and fort h bet ween Visual Basic 6.0, Visual C+ + 6.0, and Visual I nt erDev was challenging, especially for program m ers new t o t he Microsoft world. I t was hard enough t rying t o m ast er a language wit hout having anot her I DE t o learn- - t his j ust added anot her layer of unneeded com plexit y. I n order t o m axim ize exist ing knowledge and skills, we can st ill choose t o use t he keyboard schem e or window layout based on prior language experience. I f you're a Visual Basic developer, t he I DE can be configured t o respond t o fam iliar VB funct ion keys and t o arrange windows sim ilar t o VB 6.0. As shown in Figure 4.5, t hese opt ions are configured t he first t im e Visual St udio .NET is st art ed, on t he My Profile sect ion of t he VS Hom e Page: The Visual St udio St art page cont ains m any ot her helpful sect ions, including a link t o Microsoft 's online developer support area, MSDN. Headlines are ret rieved across t he I nt ernet from MSDN, enabling t he developer t o keep current wit h t he lat est pat ches, t echniques, and t ips. This int egrat ion is an effect ive way t o encourage developers t o access t he t housands of t echnical docum ent s on MSDN, helping t hem grow t heir skills and add value t o proj ect s. Cross-Language Debugging Having all languages in a single I DE enables anot her im port ant feat ure of Visual St udio .NET: cross- language debugging. I t is now possible t o st ep from Visual C# .NET code int o Visual Basic .NET

code, or int o any ot her CLS- com pliant language. This powerful debugging capabilit y should help program m ers find and correct bugs in less t im e t han ever before. We also now have t he capabilit y t o st ep int o SQL Server st ored procedures from wit hin t he Visual St udio .NET I DE. This enhancem ent m akes t racking down t ricky st ored procedures bugs quicker. A breakpoint m ust be set wit hin t he st ored procedure t hat you want t o debug in order t o st op applicat ion execut ion. Note: According to the .NET documentation, the integrated stored procedure debugging feature only works with the ODBC adapter.

Figure 4.5 The Visual St udio .NET St art page enables cust om izat ion of t he I DE.

Improved Usability The Visual St udio .NET environm ent , while based on Visual I nt erDev 6.0, has been revam ped and enhanced wit h respect t o general usabilit y. Fly- in windows ( such as t he Toolbox and Server Explorer) help keep t he m aj orit y of screen real est at e available for t he code window. Once we drag and drop a t ool from t he Toolbox ont o our web form , t he Toolbox " flies" back t o t he left - hand side. Anot her user int erface enhancem ent is m ult i- t ab windows ( see Figure 4.6) . Rat her t han seeing j ust t he open window, as in Visual Basic 6.0, m ult iple t abs now appear across t he t op of t he Visual St udio .NET window, sim ilar t o worksheet s in Microsoft Excel. This is a convenient m et hod t o quickly be rem inded of

what windows are current ly open. I t also provides an easy way t o navigat e back and fort h am ong several code or form windows we're working wit h.

Figure 4.6 The Visual St udio .NET environm ent feat ures m ult i- t ab windows.

A feat ure t hat will especially benefit newer developers is Dynam ic Help. Alt hough t his feat ure causes som e slow - down in perform ance, it is wort h t he penalt y, especially when learning t he Visual St udio .NET I DE or t he .NET Fram ework. Not e how in Figure 4.7, t he keyword Prot ect ed has been highlight ed. Dynam ic Help searches it s dat abase for ent ries relat ed t o t his keyword and displays m at ches in t he Dynam ic Help window at t he right . The Visual St udio .NET Server Explorer, shown in Figure 4.8, cont ains any and all part s of a server against which code can be writ t en. Message queues, perform ance count ers, event logs, and m any ot her com ponent s all live wit hin t he Server Explorer.

Figure 4.7 Dynam ic Help in Visual St udio .NET especially helps program m er s new t o t he .NET Fram ework.

Figure 4.8 VS.NET Server Explorer enables " Rapid Applicat ion Developm ent ( RAD) for t he Server."

A com ponent in t he Server Explorer can be dragged and dropped ont o a web form , double- clicked, and program m ed against . This is a feat ure t hat Microsoft calls " RAD for t he Server." I t provides program m ers wit h a quick, visual m et hod for incorporat ing non- visual com ponent s int o applicat ions. The Task List is anot her helpful window t hat m akes t he overall developm ent process in Visual St udio .NET m ore robust . Not only does t his window display all com pile- t im e errors, but it will also display any TO DO it em s t hat we m ark in our code. This provides us wit h a quick way t o m ark sect ions of code t hat we want t o revisit lat er.

TE

AM FL Y

Wit h t he prevalence of XML in t he .NET plat form , Microsoft has int roduced new visual design t ools t o support XML. The XML and XSD visual designers, shown in Figure 4.9, allow program m ers who are not expert s in XML synt ax t o generat e robust schem as and files by using fam iliar visual t ools, sim ilar t o dat abase design t ools.

Figure 4.9 The VS.NET XSD schem a designer offers a way t o visually creat e schem as.

I nt elliSense, t he st at em ent aut o- com plet ion feat ure t hat we oft en wonder how we ever program m ed wit hout , is im proved in Visual St udio .NET. VS.NET gives aut o- com plet e funct ionalit y when writ ing XML files, generat ed by reading t he accom panying XSD schem a. I nt elliSense is also support ed for HTML st at em ent com plet ion. Web Services Support The SOAP prot ocol is st rongly support ed in t he Visual St udio .NET environm ent .

Visual St udio .NET

includes product ivit y feat ures t hat keep developers from having t o know t he int im at e det ails of t he

Team-Fly®

underlying infrast ruct ure of any SOAP calls. This is achieved by adding a web reference t o t he Solut ion Explorer, sim ilar t o how a reference t o a library or nam espace locat ed on t he local m achine is added. The difference is t hat t he SOAP call will likely be going over t he I nt ernet , but t he enabling infrast ruct ure is abst ract ed from t he developer. Aft er right - clicking on t he Solut ion and select ing Add Web Reference…, a dialog box such as t he one in Figure 4.10 is displayed. This screen allows t he developer t o browse t o t he Web Service on t he I nt ernet , or t o search for a t ype of service t hrough UDDI .

Figure 4.10 Adding a Web Reference in Visual St udio .NET is easy.

The developer can view a Web Service's SDL cont ract t o get m ore det ailed inform at ion about t he service. When t he correct Web Service has been locat ed, t he developer picks it , and t he Web Service is included wit hin t he Visual St udio .NET solut ion. The SOAP cont ract is downloaded t o t he local m achine for I nt elliSense support . This allows t he developer t o use I nt elliSense help even for m et hods called over t he I nt ernet . The new Web Services program m ing m odel is a significant shift in m indset for m any developers, but it holds great prom ise for driving t he creat ion of an ent irely new generat ion of applicat ions. Visual St udio .NET abst ract s enough of t he SOAP prot ocol plum bing t o m ake it easy for developers t o begin writ ing and consum ing Web Services. Microsoft believes t hat t ools like t hose found in VS.NET will begin driving widespread accept ance of Web Services in t he indust ry.

Application Lifecycle Tools Modeling, design, t est ing, and source- code cont rol t ools are all t ight ly int egrat ed int o t he Ent erprise edit ion of Visual St udio .NET. Having a t oolset at our disposal wit hin t he VS.NET environm ent t hat covers t he ent ire soft ware developm ent lifecycle from st art t o finish is a big win for our proj ect t eam s. No longer is t he Visual St udio developm ent environm ent sim ply a set of program m ing languages. I t has evolved t o becom e a t oolset t hat support s t he analysis, design, developm ent , t est ing, and deploym ent phases of t he soft ware developm ent lifecycle. Visual St udio .NET includes t ools for t he indust ry- st andard Unified Modeling Language ( UML) for applicat ion m odeling. Support is included for use cases, sequence diagram s, act ivit y diagram s, class diagram s, and m ore. The VS .NET m odeling t ool builds on t he previous version of Visual Modeler, which was it self a subset of t he popular Rat ional Rose product . UML m odeling is helpful for com m unicat ing applicat ion requirem ent s and archit ect ure across proj ect t eam s t hrough visual represent at ions of various syst em com ponent s. I n addit ion t o support for m odeling applicat ion code, dat abase m odeling t ools are also included in VS.NET. The Dat abase Designer provides t he developer wit h t he abilit y t o add new t ables, colum ns, relat ionships, and m ore, all wit hin t he VS .NET I DE. Query Designer provides an Access- like way of visually generat ing SQL queries wit h drag and drop. The Script Edit or provides keyword color- coding when working wit h t riggers and st ored procedures. When an applicat ion has reached t he appropriat e developm ent st age, it can be t est ed wit hin t he VS.NET I DE wit h Applicat ion Cent er Test ( ACT, form erly known as Web Applicat ion St ress Tool, or Hom er) . This t ool places a heavy load on t he various XML Web Services and HTML / ASPX Web pages t hat com prise t he applicat ion. Test pages for XML Web Services are generat ed aut om at ically by t he Visual St udio .NET environm ent , and t hese pages can be included in aut om at ed ACT st ress- t est s. This t ool helps t est ers t rack down pot ent ial bot t lenecks before t he applicat ion is ever deployed. Developers can t hen at t ack each bot t leneck unt il t est perform ance m et rics m eet t he specified business requirem ent s. Applicat ions can be t est ed at a lower level using Visual St udio Analyzer. This t ool t racks int eract ions bet ween obj ect s, t hreads, and t he various applicat ion t iers. Result s are present ed in an easy- t o- use graphical form at . Throughout t he life of a proj ect , applicat ion docum ent s and code m ust be prot ect ed from inadvert ent changes, and from m ult iple program m ers unknowingly working on t he sam e piece of code at t he sam e t im e. Visual SourceSafe ( VSS) is Microsoft 's t ool for solving t hese problem s. The .NET release of VSS brings a t ight er int egrat ion wit h t he Visual St udio .NET environm ent for ease of use. The Visual St udio .NET environm ent also includes support for deploying a .NET applicat ion aft er it has been developed and t est ed. See Chapt er 16, " Deploying t he Applicat ion," for an in- dept h look at t he deploym ent feat ures of VS.NET. Enterprise Features Microsoft cont inues t o st rive t o m eet t he needs of ent erprise- class developm ent proj ect s. Com panies wit h sizable developm ent proj ect s and st aff oft en wish t o st andardize applicat ion archit ect ure across m any different proj ect s. Wit h Visual St udio .NET, senior developers and t echnical archit ect s can now develop baseline applicat ion archit ect ures. Tem plat es est ablished by t hese m ore experienced t eam m em ber s can

t hen be used as st art ing point s by t eam m em bers. This is m uch m ore effect ive t han each developer st art ing wit h a blank slat e and com ing up wit h his own design. Senior developers and t echnical leads can also define Visual St udio .NET policies t hat dict at e whet her cert ain m enu it em s or com ponent s appear in a developer's VS .NET I DE. These policies are defined t hrough an XML m et a- language called t he Tem plat e Descript ion Language ( TDL) . For exam ple, seeing only t he relevant com ponent s on a t oolbar, rat her t han having t o choose from every com ponent available out - of- t he- box, helps developers im plem ent form s t hat are consist ent wit h t he policy laid out by t hose responsible for delivery. Support for Archit ect ural Tem plat es and Policy Definit ion wit hin Visual St udio .NET helps senior archit ect s share t heir knowledge m ore effect ively wit h t eam m em bers. This will enable bet t er applicat ion int egrit y and will help ensure consist ency across t he work of all t he developers on a proj ect or across an ent erprise.

ILDasm The I L Disassem bler, or I LDasm , is a ut ilit y used for looking at t he inner workings of assem blies. I t is locat ed in t he C: \ Program Files\ Microsoft .NET\ Fram eworkSDK\ Bin folder. Such a det ailed view of an assem bly is possible because of t he prevalence of m et adat a com piled int o every assem bly. The m et adat a describes everyt hing about an assem bly: it s t ypes, it s version num ber, it s cult ural locale, et c. We m ight want t o view t he inner workings of an assem bly t o det erm ine why a program is behaving like it is, or t o help us find out about all t he funct ionalit y t hat is im plem ent ed. As shown in Figure 4.11, t he part s of an assem bly t hat I LDasm will display include nam espaces, classes, int erfaces, m et hods, event s, propert ies, and m ore.

Figure 4.11 The I LDasm ut ilit y offers an inside look int o assem blies.

By double- clicking on a m em ber of a t ype, we can view t he I L code t hat defines t he inner workings of t he m em ber. A sam ple det ail view of a Dat aSet m em ber funct ion is shown in Figure 4.12.

Figure 4.12 Double- clicking a t ype m em ber display s t he support ing I L code.

I LDasm is useful for figuring out how t he underlying .NET Fram ework behaves, and provides a m eans of going " deep" int o t he det ailed im plem ent at ion when needed. Command Line Compilers I ncluded wit h bot h Visual C# .NET and Visual Basic .NET is a com m and- line com piler. This allows us t o writ e code out side of t he Visual St udio .NET environm ent wit h t he edit or of our choice. This is also helpful for adm inist rat ors who use build script s t o pull t he lat est code from VSS and com pile it on a regular basis. Caution: Work outside of the Visual Studio .NET IDE at your own peril. The productivity gains that VS.NET provides for us are exceptional.

Creating typed datasets, programming against web

references, and calling other functions that require plumbing work "behind the scenes" is made simple in the graphical IDE of VS.NET.

The Visual Basic .NET com piler is VBC.exe and t he Visual C# .NET com piler is CSC.exe. provide a variet y of swit ches t hat cont rol com pilat ion opt ions, as shown in Figure 4.13.

Bot h com pilers

Figure 4.13 The language com pilers offer a variet y of com pilat ion opt ions.

.NET Developer Productivity Tools Summary Visual St udio .NET finally realizes t he long- await ed vision of a single I DE for all Microsoft developm ent languages. This will bring m ore synergy t o program m ing t eam s and will allow developers t o m ove m ore easily bet ween languages. Visual St udio .NET brings a host of ot her product ivit y feat ures t hat m ake it t he prem ier developm ent environm ent for building dist ribut ed e- Business applicat ions on t he m arket t oday. I n addit ion t o using Visual St udio .NET, developers should also becom e fam iliar wit h t he I LDasm t ool. This ut ilit y can help drive a deeper underst anding of t he way t he .NET Fram ework is designed and im plem ent ed.

From Here The new .NET plat form is t he end result of a t rem endous effort by Microsoft . The com pany believes t hat t he I nt ernet is poised t o evolve int o a new generat ion of applicat ions and services, int erconnect ing dat a on levels previously not possible. .NET is designed from t he ground up wit h t his m odel of large- scale dat a int egrat ion and sharing in m ind. Because of it s powerful archit ect ural foundat ion, robust language im plem ent at ions, and developer product ivit y t ools, t he .NET plat form and t oolset is poised t o deliver t he ent erprise- class perform ance and ext ensibilit y t hat will enable Microsoft e- Business solut ions t o play in t he space once reserved for larger- scale Unix and AS/ 400 syst em s. This chapt er has int roduced t he .NET archit ect ure and t ools. Read on t o learn m ore about designing each of t he t hree logical t iers in .NET, present at ion, business, and dat a.

5 Implementing .NET Presentation Services The present at ion layer of t he gasTI X websit e is im plem ent ed using ASP.Net pages writ t en wit h Visual Basic.NET. The use of ASP.Net offers t he following benefit s over classic ASP during developm ent : m uch short er developm ent t im e, separat ion of design and code, bet t er encapsulat ion and reusabilit y, and m uch easier m aint enance. This chapt er exam ines t he m aj or feat ures of ASP.Net wit h part icular focus on how t hose feat ures were leveraged in t he gasTI X websit e. When Microsoft first int roduced Act ive Server Pages ( ASP) in 1997, developers were suddenly able t o dynam ically publish inform at ion t o t he web and cust om ize web pages very easily while using t heir exist ing Visual Basic skills. Visual Basic had already gained a large following, so t his advancem ent m ade dynam ic web developm ent inst ant ly available t o a very large group of developers. Act ive Server Pages boast ed very easy developm ent , an int erpret ed script language, and t he abilit y t o int eract cleanly wit h st andard HTML files. All of t hose feat ures were great in 1997 since only a sm all percent age of com panies had dynam ic websit es and fewer ones t rust ed t he I nt ernet t o handle t heir core business funct ionalit y. Wit h t he success of Act ive Server Pages, t his philosophy began t o change. Act ive Server Pages quickly becam e t he de fact o st andard for adding personalizat ion t o websit es, publishing dat a , and displaying cont inuously updat ed inform at ion across I nt ranet s. However, even as Microsoft was enj oying a great response t o t he first version of Act ive Server Pages, t heir developers had already recognized t hat t he vision of t he I nt ernet was changing and t hey were already hard at work on t he next evolut ion of t he t echnology, ASP.Net . I n t he last few years, we have seen t he I nt ernet explode from a public novelt y t o a fundam ent al business com ponent . As bandwidt h and securit y have increased, m ore and m ore com panies are placing t heir business processes on t he web while hundreds of new com panies have em erged whose ent ire business depends on t he web for it s exist ence. Transact ions writ t en in XML and t ransferred t hrough t he I nt ernet are rapidly becom ing t he core of business- t o- business com m unicat ion. becom ing t he . As com panies place m ore of t heir business on t he web, t hey need increasingly m ore robust , m ore st ream line solut ions t hat can handle t he increased volum e. ASP.Net m eet s t hese requirem ent s while offering m any ot her new feat ures. ASP.Net is a radical change from previous versions of ASP. I t is m uch m ore t han sim ply an upgrade. I n fact , som e previously developed ASP applicat ions and pages will not be easily upgraded t o ASP.Net . This m arks a significant depart ure from past ASP upgrades and will no doubt anger som e ASP developers. Microsoft did not , however, m ake t his decision light ly. I n designing ASP.Net , Microsoft realized t hat t he benefit s easily out weigh t he problem s associat ed wit h lim it ed backwards com pat ibilit y. I t was a sm all price t o pay but one which will be praised in t he long run. The rest of t his chapt er is devot ed t o dem onst rat ing t he overwhelm ing benefit s of ASP.Net and dem onst rat ing why developers will want t o m ake t he swit ch.

Why Should Developers Switch to ASP.Net? Classic ASP code suffers from all t he sym pt om s of non- obj ect - orient ed program m ing. They are: poor reusabilit y, increased difficult y when using a dist ribut ed developm ent t eam , and increased m aint enance challenges.. These problem s are act ually worse in ASP t han ot her non- oo languages, since ASP inherent ly m ixes t wo different developm ent languages int o a single code base. By m ixing HTML design

code wit h ASP script ing code ( whet her it be VBScript , JScript , or anot her language) , t he result ing page is m uch m ore difficult t o read and m aint ain. Addit ionally, classic ASP code becom es t ied t o t he design m aking it easier t o break and m ore difficult t o reuse in ot her pages. This problem is oft en referred t o as " spaghet t i code," in t hat t he lines of code are int ert wined am ongst one anot her like a m ound of spaghet t i, wit h no discernible beginning or end t o any st rand. While t here are t echniques for overcom ing m any of t he non- OO problem s associat ed wit h classic ASAP, none of t hem are part of t he t echnology it self. I t is possible t o use COM cont rols, for inst ance, t o im prove encapsulat ion and reusabilit y. However, t he COM cont rol cannot be writ t en in t he sam e VBScript t hat t he page is writ t en in, it is writ t en against a different obj ect m odel, and requires different developm ent skills t o produce. ASP .NET im proves upon t his by int egrat ing all of t he solut ions int o a single developm ent package. Classic ASP also suffers from m any of t he problem s inherent in im m at ure languages, such as lim it ed funct ionalit y and poor perform ance. Since t he default language for classic ASP is VBScript , which is a subset of t he Visual Basic language, developers have oft en com plained about t he lim it ed funct ionalit y available t o ASP pages. Developers fam iliar wit h t he full Visual Basic language or ot her applicat ion- level languages, such as C or C+ + , have found it difficult t o work wit hin t he lim it s of VBScript . The power of VBScript was enhanced wit h t he abilit y t o run COM cont rols, but t his power cam e at t he expense of having t o code and m aint ain m ult iple languages. I n addit ion t o t he lim it ed funct ionalit y, VBScript incurs serious perform ance penalt ies due t o it s int erpret ed nat ure. Every line of code m ust be parsed and int erpret ed each t im e a page is request ed. This has severely lim it ed t he abilit y of classic ASP pages t o scale t o t he level of an ent erprise applicat ion. ASP.Net addresses each of t hese issues t hrough a variet y of evolut ions of t he obj ect m odel, underlying languages, and deploym ent infrast ruct ure. First , t he ASP obj ect m odel has been com plet ely rewrit t en in order t o gain t he benefit s of obj ect orient at ion. This result s in a com plet e change in t he way t hat ASP pages are writ t en. ASP.Net pages now separat e t he script ing code from t he design code, allowing developers t o writ e code and designers t o design t he pages. This m inim izes t hat am ount of design work t hat developers need t o do and t he am ount of ASP knowledge t hat designers m ust have, allowing each group t o focus on what t hey do best . gasTI X is an excellent exam ple of t his separat ion of code and design. A t eam of designers creat ed t he HTML code using server- side ASP cont rols ( explained lat er in t his chapt er) using a pre- det erm ined nam ing schem e. The developers t hen wrot e t he code for t heir respect ive pages in a separat e file, referencing t he nam ed cont rols. This allowed t he designers and developers t o be locat ed in geographically different locat ions wit hout im pact ing eit her’s work. This separat ion result ed in t he developer never having t o see where t he cont rols were on t he page while t he designers never seeing t he developer's code. They were able t o working com plet ely independent of each ot her. To im prove on t he perform ance of classic ASP pages, ASP.Net is now fully com piled. All of t he code in an ASP.Net web page is com piled int o a single .DLL t hat , by default , resides in t he " bin" sub- direct ory in t he root web direct ory. By com piling code int o a .DLL and st oring it in m em ory, ASP.Net execut es pages very quickly and elim inat es m uch of t he overhead associated wit h t he execut ing m ult iple pages. This offers significant perform ance gains over classic ASP, which sim ply int erpret s each page upon dem and, which includes int erpret ing t he sam e page m any t im es.

One of t he benefit s of int erpret ed languages over com piled languages is t he abilit y t o j ust drop new code int o place and run it im m ediat ely. ASP.Net feat ures t his benefit despit e being a com piled language. ASP.Net allows t he developer t o define where t he proj ect .DLL is t o be placed and updat e t hat .DLL by sim ply dropping a new version on t op of t he old one. I t is no longer necessary t o m anually regist er a .DLL using regsvr32; t he syst em aut om at ically regist ers .DLL’s upon dem and. The first t im e an ASP.Net page uses t he new .DLL, t he syst em will regist er t he .DLL for fut ure use. This incurs a significant perform ance penalt y t he first t im e a page is run, but allows fut ure calls t o t he .DLL t o be run wit h t he best possible perform ance. Tip: Always test your ASP.Net application after each compilation.

Not only is this good development practice,

but it will also ensure that you experience the time-consuming .DLL registration period, saving your users the frustration of waiting for the page to run the first time.

The lim it ed funct ionalit y problem in classic ASP has been alleviat ed in ASP.Net t hrough t wo new feat ures. These are a new feat ure in t he general .NET plat form and a new feat ure in how t he ASP.Net uses .Net com plied code. The .NET plat form now allows all .NET languages t o be com piled int o a Com m on Language Runt im e ( CLR) . This m eans t hat whet her you writ e your code in VB.Net , C# , or any one of t he ot her t hirt y t hird- part y .NET languages, your code will be com piled int o t he sam e CLR. This feat ure, coupled wit h ASP.Net 's abilit y t o use any CLR com piled code, m eans t hat developers are now free t o writ e t heir ASP.Net pages in any .NET language t hey're com fort able wit h. Addit ionally, different pages wit hin a single ASP.Net pages can be writ t en in different .NET languages, allowing easier t eam developm ent . Since ASP.Net pages now fully support t he developer's favorit e language, what ever it m ight be, ASP applicat ions are now only lim it ed by t he funct ionalit y of t he chosen language. ASP applicat ions can now m ake Windows API calls, cont rol m ult iple syst em t hreads, and do m any ot her language feat ures t hat , in t he past , have been reserved for applicat ion developm ent . Since t he gasTI X present at ion layer was developed in Visual Basic.NET, all code exam ples in t his chapt er will use Visual Basic.NET. Since ASP.Net support s t he Com m on Language Runt im e ( CLR) , t hese exam ples can be easily port ed t o C# or any ot her .NET language. There are m any ot her feat ures of ASP.Net t hat should m ot ivat e serious developers int o considering m aking t he swit ch. For inst ance, ASP .NET support s classic ASP pages as well, allowing class ASP pages t o be execut ed side by side wit h ASP .NET pages. ASP.Net pages use t he file ext ension ".aspx" inst ead of t he classic " .asp" . This allows t he I I S web server t o different iat e bet ween ASP and ASP.Net pages and execut e t he appropriat e code. These pages can call one anot her and pass dat a t hrough query st rings, t hough t hey cannot share inform at ion in st at e m anagem ent obj ect s due t o ASP.Net 's vast ly im proved st at e m anagem ent feat ures. Note: It might be unclear at this point how ASP .NET manages .aspx pages and compiled ASP .NET code. Each ASP. Net web form has one .aspx page and one code-behind class. All of the code-behind classes in a project are compiled into a single .DLL, which is used when any of the .aspx pages are requested. This allows ASP .NET to keep all of the application code compiled and waiting in cache, while maintaining separate .aspx pages.

Anot her feat ure of ASP.Net is t he abilit y t o expose and consum e web services. A web service is basically an obj ect m et hod call t hat is exposed t hrough t he I nt er/ int ranet for consum pt ion by ot her applicat ions or service. This service is exposed t hrough a group of st andard t echnologies such as XML and UDDI . This allows users t o locat e t he service and learn about it s definit ion. The service is likewise used, or consum ed,

using t he sam e st andards. . This great ly increases t he encapsulat ion and reusabilit y of com ponent s and allows a dist ribut ed t eam of developers t o creat e a single syst em . See chapt er 12 " Fulfillm ent / Order Hist ory ( BizTalk/ Web Services) " for m ore inform at ion on publishing and consum ing Web Services. All of t he .NET languages offer enhanced support for XML. XML is a m at uring language and has been accept ed by m any m aj or com panies as an inform at ion exchange st andard. gasTI X m akes use of t his nat ive XML support by passing all event , venue, and part icipant dat a bet ween t iers in XML form at . Using a single, st andard way of represent ing dat a m akes it very easy for t he present at ion service layer t o underst and t he form at of t he dat a offered by all of t he business obj ect s. There is no need for t he present at ion t ier t o underst and a separat e dat a obj ect m odel since it already underst ands XML nat ively. This great ly sim plifies t he present at ion layer and m akes it easier t o expose t he sam e dat a t hrough ot her int erfaces, such as web services. See chapt er 11 " Purchasing a Ticket ( Creat e/ Updat e Transact ions) " for an exam ple of how t he present at ion layer uses XML provided by t he business layer.

The Page Object All ASP.Net web pages are full- fledged derivat ives of t he page class. This ensures t hat all ASP .NET pages have t he have t he sam e basic propert ies, m et hods, and event s. Com plet e int egrat ion of ASP .NET developm ent wit h a clearly- defined obj ect m odel is an int egral part of t he developm ent plat form and offers short design cycles, im proved reusabilit y, and easier m aint enance. When a developer creat es a new ASP.Net web page, t he .aspx page is com piled int o an obj ect derived from t he web form class. This ensures t hat all ASP.Net pages adhere t o t he obj ect m odel while allowing t he flexibilit y for developers t o derive t heir own classes from t he page class. This m akes it easy for developers t o creat e t heir own cust om page classes for easy re- use. Cust om page classes m ay define st andard colors, include default cont rols, et c. Since t he .aspx file is a sim ple t ext file and not an act ual class, t he Page direct ive supplies t he necessary inform at ion t o t he com piler so t hat it can be t reat ed as a m odule. Each .aspx file requires a page direct ive wit h an I nherit s param et er specifying a class from which t he web form is inherit ed. The class specified in t he I nherit s propert y m ust be derived from t he Page class or som e sub- class of t he Page class. The following page direct ive for t he gasTI X hom e page specifies t hat Visual Basic is used ( Page Language="vb") , t hat event s m ust be connect ed t o handlers explicit ly ( AutoEventWireup="false") , t he code- behind file ( Codebehind="Home.vb") , and t he class from which t he page is inherit ed ( Inherits="Search.Home") .

Page direct ives in gasTI X also use t he following at t ribut es: z

AspCom pa t - - True or False. Specifies whether an ASP.Net page is compatible with ActiveX .DLL's written for class ASP pages.

z

Con t e n t Type - - Specifies the HTTP content type. Any standard MIME type is acceptable.

z

D e scr ipt ion- - Specifies a textual description of the page.

z

En a ble Se ssion St a t e - - True or False. Enables or disables session state for the page.

A third option,

ReadOnly is also available if session state can be read but not changed. z

Er r or Pa ge - - Specifies the URL to load if an un-handled page error is encountered.

z

M a in t a in St a t e --True or False. Specifies that ViewState should be maintained across page requests.

(See

"Round Tripping and View State" later in this chapter.) z

Tr a ce --True or False. Tells the web server to output trace information for this page.

z

In addition to the page directive, ASP.Net pages can also contain one or more of each of the following five types of directives:

z

@ Con t r ol- - Used to define that a page is a user control instead of a standard ASP.Net web form.

This direct ive is m ut ually exclusive wit h t he @ Page direct ive. z

@ I m por t - - Defines a namespace to be imported into the page. This is used to make available classes and interfaces not automatically included in the page. Each @ Import directive can have only a single namespace, but each page or user control can have multiple @ Import directives.

z

@ Re gist e r --Defines aliases for user controls on a .aspx page. When we build a user control, the @ Register

z

AM FL Y

directive is used to tell our web form where it can find the user control. @ Asse m bly--Defines an assembly (a compiled .DLL) to be linked to this page.

This makes the assembly's

classes and interfaces available to the page. z

@ Ou t pu t Ca ch e --Defines how long the web server should retain the page in the output cache.

TE

Page direct ives are j ust one of t en synt act ical elem ent s t hat can appear on an ASP.Net page. The m ost com m on used are HTML cont rol and cust om cont rol elem ent s. An HTML cont rol elem ent is sim ply an HTML elem ent on t he page which you can m anipulat e at design- t im e or run- t im e. This allows t he developer t o work wit h st andard HTML obj ect s in server- side code. A cust om cont rol elem ent is a specialized version of one or m ore HTML cont rols. This includes t he st andard ASP.Net web cont rols such as Text Box, Label, and Dat aGrid. I n order t o clearly define t he cont rol desired, all server- side web cont rol m ust include an alias; t he alias for included web cont rols is " asp: " while t he alias for user cont rols is defined in t he @ Regist er direct ive. I n order t o add funct ionalit y t o t he obj ect s defined in t he cont rol synt ax sect ions, t he code declarat ion and code render blocks allow developers t o writ e code against t he cont rols. The code declarat ion block includes all m em ber variables, propert ies, and m et hods t hat will be com piled int o t he .NET class. The code render sect ion defines expressions t hat will be execut ed in order t o render t he page. These expressions can be included inline, wit hin t he .aspx file it self, or in a code- behind file. The t hird code sect ion is reserved for dat a binding expressions t o allow t he developer t o program m at ically bind cont rols t o exposed dat a sources. Finally, an ASP.Net page can include t hree server- side elem ent s: obj ect t ags, include direct ives, and com m ent s. Code in t he obj ect t ags sect ion declares and inst ant iat es bot h COM and .NET fram ework obj ect s for use wit hin t he page. The include direct ives insert t he cont ent s of a specified file direct ly int o t he ASP.Net page. The cont ent s of t his file are not parsed or execut ed; t hey are sim ply dum ped int o t he

Team-Fly®

file " as- is." Com m ent s, of course, are sim ply server- side definit ions t hat prevent code from execut ing during t he page render and are available t o t he developer t o aid m aint ainabilit y. Addit ional cont ent found in an ASP .NET page t hat does conform t o one of t hese cat egories is defined as lit eral t ext . This oft en includes such t hings as st andard t ext and st andard HTML. . I t is im port ant t o not e t hat even t his t ext is fully obj ect orient ed; for each sect ion of lit eral t ext found, a Lit eralCont rol obj ect is built and placed int o t he page's obj ect m odel. These Lit eralCont rol obj ect s can be m anipulat ed program m at ically like any ot her ASP .NET obj ect .

What are Web Controls? For die- hard ASP developers, t he best new feat ure of ASP.Net is undoubt edly web form s and web cont rols. That 's a bold st at em ent t o m ake, given t he diversit y of over one m illion ASP developers in t he world t oday. However, m ost web developers agree t hat t he m ost vexing problem facing t hem t oday is browser com pat ibilit y: t he never- ending quest t o m ake a single set of code work in every available browser on every available operat ing syst em . Web Form s and Web Cont rols prom ise t o solve t his problem . A Web Form is a server side const ruct designed t o cont ain Web Cont rols. When t he ASP.Net page is parsed, t he Web Form is t ranslat ed int o a st andard HTML form while t he Web Cont rols are t ranslat ed int o t heir respect ive HTML count erpart s. There are several benefit s t o using Web Form s and Cont rols, m ost im port ant being browser independence and event - driven program m ing. Web Cont rols offer t rue cross- plat form developm ent and achieve t rue browser independence by parsing server- side cont rols int o browser- com pliant HTML. The web server recognizes t he specific t ype and version of t he browser m aking t he page request and creat es t he m ost advanced version of HTML allowed by t he browser. This m eans t hat t he HTML creat ed by a single ASP.Net cont rol definit ion m ay look different ly in I nt ernet Explorer t han it does in Net scape, as shown in Figure 5.1. Because of t his, generat ed pages m ay not be ident ical wit hin t he sam e browser if t heir versions differ. Give credit t o Microsoft for developing a solut ion t o t his problem and recognizing t he realit y of user and browser diversit y. The definit ion of t he HTML t hat is creat ed when t he web server parses t he web cont rol is also dependent on t he operat ing syst em on which t he browser resides; t he HTML creat ed for I nt ernet Explorer running on Windows m ay look different t han HTML creat ed for I nt ernet Explorer running on a Macint osh. When you consider ot her browsers like t he Opera web browser and UNI X/ Linux plat form s as well, it is easy t o underst and how a single web cont rol can have lit erally dozens of HTML definit ions. Web cont rols are sim ilar t o Microsoft 's Design- Tim e Cont rol ( DTC) definit ion used wit h classic ASP pages. The difference is t hat DTC's had t o be aut hored in a COM- com pliant language such as Visual Basic or MS Visual C+ + inst ead of VBScript wit h t he rest of t he ASP pages. The DTC was t hen com piled and included as a server- side definit ion on t he ASP page where it was forced t o work wit hin t he ASP page. The im provem ent of web cont rols over DTC's is t hat t hey are fully int egrat ed wit h t he ASP.Net obj ect m odel and are program m ed using t he sam e language and obj ect m odel as t he web form s. Thus, t hey do not incur t he overhead or learning curve of an addit ional language

Figure 5.1 A single Web Cont rol can creat e m ult iple versions of HTML, depending on t he browser request ing t he page.

Obviously, t he benefit of allowing server- side cont rols t o writ e HTML is t he abilit y t o creat e a single page of code t hat is aut om at ically t ranslat ed for any browser request . Most current web developm ent is built on t he prem ise t hat a specific browser/ version/ operat ing syst em com binat ion is chosen and t he pages are built t o support t hat version. By definit ion, t his aut om at ically involves m ore work t han is necessary: browsers and versions have t o be evaluat ed, funct ionalit y m ust be " dum bed down" t o t he level of t hat browser, and addit ional t est ing is required t o ensure com pat ibilit y. ASP.Net m oves t hat effort from t he developer t o t he web server, where it belongs. I t is im port ant t o underst and t hat ASP.Net web cont rols are not j ust " dum bed down" t o t he lowest support ed browser/ version. I n fact , web cont rols are pushed t o expose t he highest funct ionalit y allowable w it hin t he browser m aking t he request . That m eans t hat a single Label web cont rol wit h a Cascading St yle Sheet ( CSS) t ransit ion defined on it will aut om at ically be displayed wit h t he t ransit ion in I nt ernet Explorer version 5 and will sim ply be displayed wit hout t he t ransit ion in Net scape version 4. Since web cont rols are parsed int o pure HTML before t he client ever sees t hem , t he end- user will not be able t o t ell t he difference bet ween web cont rols and st andard HTML cont rols. The t wo are fully com pat ible and can be freely int erspersed on a single page. For exam ples of using Web Cont rols, see chapt er 110( “ Event Searching” ) .

Of course, web cont rols are not a silver bullet for every possible browser incom pat ibilit y. The solut ion is dependent on t he rules em bedded on t he web server, which dict at e how t he HTML is defined. These rules are, of course, subj ect t o discrepancies wit hin t he browsers and t he ravages of t im e and new browser versions. The web server needs t o be updat ed or pat ched for every new browser version t hat becom es available in order t o ensure t he highest level of com pat ibilit y and funct ionalit y. Microsoft is st ill evaluat ing t he best m et hod for keeping t hese rules up t o dat e. Anot her concern for web developers is t he increasing num ber of handheld devices now accessing t he web. These devices have severely lim it ed funct ionalit y, com pared t o t heir deskt op count erpart s. Furt herm ore, t he funct ionalit y lim it at ions vary drast ically from device t o device, from t he inabilit y t o display im ages all t he way t o being lim it ed t o 100 charact ers or fewer on t he screen. These devices creat e an ent irely new set of cross- browser developm ent problem s, one t hat even ASP.Net web cont rols cannot easily fix. The first release version of ASP.Net does not include built int o com pat ibilit y wit h handheld devices, alt hough support is available from Microsoft in a separat e Soft ware Developm ent Kit . Fut ure versions of ASP.Net prom ise t o include full support for handheld devices. Note: You can download the .NET Mobile Web SDK at www.microsoft.com/mobile/downloads/default.asp

Despit e t hese concerns, web cont rols go a long way t owards solving t he problem s of cross- browser developm ent . The init ial version of ASP .NET clearly dem onst rat es t his progress by including cont rols which do not have a st andard HTML count erpart . ASP.Net ships wit h support for num erous addit ional cont rols such as t he Calendar and Ad Rot at or cont rols. While t here is no HTML equivalent of t hese cont rols, ASP.Net aut om at ically creat es a set of st andard HTML cont rols and arranges t hem as necessary on t he page t o creat e t he effect of a single cont rol. ASP.Net aut om at ically support s down- level browser s as well, applying it s st andard rules for creat ing HTML cont rols for each browser. Third- part y cont rols will also be available for ASP.Net which should allow developers t o easily use advanced cont rols in t heir web developm ent wit hout having t o worry about downgrading t heir applicat ions for older browsers. ASP.Net even m akes it easy for developers t o m ake t heir own user cont rols t o m axim ize code- reuse and port abilit y ( see t he " User Cont rols" sect ion lat er in t his chapt er) .

Event-Driven Programming Web Cont rols also allow web developers t o code m ore like applicat ion developers by offering event - driven program m ing. I n a t ypical applicat ion built in Visual Basic or C+ + , applicat ion funct ionalit y is only execut ed in response t o som e event . This m ay be t he user clicking a but t on, pressing a key on t he keyboard, or even t he arrival of new em ail. This allows developers t o break t he funct ionalit y of t heir code int o sm all m anageable chunks t hat are only execut ed when t he event t hey represent occurs. This im proves applicat ion perform ance by only execut ing t he necessary pieces of code and m akes code m uch easier t o m aint ain. Event - driven program m ing has becom e t he st andard in applicat ion developm ent t hough it has never been available in server- side web developm ent . The problem is t hat t he event s and t he event handlers exist in different realm s: t he event has t o occur on t he client end of t he web, while t he event handler for server- side pages has t o be run on t he server. There are m et hods for passing inform at ion from t he client - side t o t he server- side, and vice- versa, however t here is no int rinsic abilit y built int o an HTML

t ext box, or any ot her HTML cont rol, t hat allows t he developer t o hook client - side event s t o server- side handlers. Web cont rols offer t his abilit y. When t he original HTML specificat ion was writ t en, it was creat ed t o display inform at ion, not serve as an applicat ion developm ent plat form . There was no defined way t o dynam ically change obj ect propert ies or respond t o an obj ect 's event s. I n fact , no event s were even defined in t he HTML specificat ion. Event handling did not becom e possible unt il web browsers st art ed support ing anot her const ruct : script ing. Most com m only using JavaScript , browsers slowly began support ing client - side event handling and dynam ic propert y assignm ent . Though t his m arriage of t wo specificat ions is not perfect , it has evolved t o work fairly well for basic event handling and dynam ic propert y assignm ent . There are t wo m aj or drawbacks wit h t he exist ing relat ionship of HTML and client - side script . The first is t he requirem ent t o include specific code on every web page t hat allows for basic event handling and propert y m anipulat ion. All m at ure applicat ion developm ent languages hide t hese basic det ails from t he developer. While t his problem leads t o bloat ed code and addit ional developm ent risks, t he second drawback is m uch m ore problem at ic for t rue applicat ion developm ent . The second drawback is t he requirem ent t hat event - handlers m ust be defined on t he client - side. The event - handler is const rained by t he lim it ed environm ent of t he web browser. Most web browsers im pose st rict rest rict ions on t he act ions client - side code can execut e, rest rict ing access t o t he file syst em , ot her applicat ions, and syst em set t ings. These rest rict ions severely lim it t he funct ionalit y t hat can be achieved in client - side code in order t o prot ect t he end m achine and operat ing syst em from deviant act s by rogue program m ers. Different browsers also im plem ent variat ions of t he client - side script engine, requiring m ult iple code versions. Moreover, placing addit ional code on t he client - side not only increases t he file size of t he pages being downloaded, but it also im poses t he burden of execut ing t he code on t he client m achine. The end result is large code blocks t hat are downloaded and execut ed on a slow client m achine inst ead of being execut ed on a high perform ance web server. Anot her holdback t o event - driven web program m ing is t he web's inherent st at elessness. I nt ernet pages are st at eless, t hat is, t hey do not m aint ain any inform at ion about t heir own st at e bet ween calls. Thus, when a call t o a page is m ade, t he page has no idea what happened during t he previous call. I n fact , t he page has no way of knowing how t he page m aking t he request cam e int o being at all. The only inform at ion available t o t he server is t he current st at e of t he page: t ext box values, list select ions, et c. Thus, if t he user has changed a value in a t ext box, t he server- side code responding t o t he changed t ext only knows t he current value of t he t ext box; it has no inherent way t o know t he previous value. Each page is recreat ed from scrat ch each t im e it is loaded, degrading perform ance. This is a com m on scenario in applicat ion program m ing and one t hat cannot be easily duplicat ed in web developm ent …unt il now.

Using Web Controls in Visual Studio.NET One of t he goals of ASP.Net is t o bring web developm ent closer t o t he m ore m at ure world of applicat ion developm ent . Web cont rols and Visual St udio.NET m ake great st rides in t his direct ion by allowing developers t o design web pages wit h WYSI WYG ( What You See I s What You Get ) cont rol and propert y dialogs as shown in figure 5.2.

Figure 5.2 You set propert ies for your web cont rols j ust like applicat ion cont rols.

When ASP developers begin working wit h Visual St udio.NET, t he first t hing t hey will likely not ice is t hat Visual I nt erDev is m issing. Since Visual Basic.NET and C# are fully support ed for ASP.Net developm ent , ASP.Net proj ect s do not need a separat e I nt egrat ed Developm ent Environm ent ( I DE) . To creat e a new ASP.Net proj ect in Visual Basic.NET, sim ply choose File| New| Proj ect from t he Visual St udio.NET m enu. Select " Visual Basic Proj ect s" from t he " Proj ect Types" list on t he left - hand side of t he " New Proj ect " dialog and highlight " Web Applicat ion" from t he " Tem plat es" list on t he right . Change t he applicat ion nam e and locat ion if desired and click " OK" . ( See Figure 5.3)

Figure 5.3 The " New Proj ect " dialog offers a variet y of t em plat es for Visual Basic, C# , and C+ + .

By default , t he WebForm 1.aspx is displayed in linearLayout m ode, m eaning t hat cont rols dropped on t o t he web form will line up t op t o bot t om , left t o right . I f you drag a cont rol from t he Toolbox ont o t he form , it will aut om at ically reposit ion it self in t he next available posit ion t o t he right and below t he m ost recent cont rol added. Click on t he form and change t he pageLayout propert y on t he propert ies dialog t o " gridLayout " . Not ice t hat a grid appears on t he web form . Now, drag anot her cont rol ont o t he form . Not ice how it rem ains where you dropped it on t he form . The gridLayout feat ure of web pages aut om at ically generat es HTML t able or CSS definit ions t o posit ion your web cont rols as you desire. The posit ioning is designed t o be com pat ible wit h every m aj or t ype and version of web browser. Note: You may have to hover over the "Toolbox" menu item along the left side of your IDE to display the toolbox.

You should also ensure that you've selected the "Web Forms" tab before selecting controls to add to

a web form.

You will find a variet y of cont rols in t he t oolbox, som e fam iliar, som e new. The Text Box and But t on cont rols are fam iliar t o web developers and are nam ed consist ent ly wit h t heir HTML count erpart s, while t he com m on HTML Anchor t ag ( < A> ) is called " HyperLink" in t he Visual St udio.NET t oolbox. New cont rols include t he Dat aGrid, t he Repeat er, and t he Validat or cont rols. For m ore inform at ion about t hese cont rols, see chapt er 12 ( “ Purchasing A Ticket ” ) . The Visual St udio.NET I DE offers you t wo views of each web form : Design and HTML. This is sim ilar t o t he design feat ure of Visual I nt erDev 6 ASP pages, t hough it has been great ly enhanced t o accom m odat e t he m any new feat ures of web cont rols. Click on t he " HTML" t ag at t he bot t om of t he web form t o view t he server- side code generat ed by t he design page, as shown in figure 5.4. Here you can exam ine t he t able definit ion creat ed t o accom m odat e t he absolut e posit ioning of your cont rols.

Figure 5.4 The I DE aut om at ically generat es t he necessary code t o creat e t he Web cont rols.

Scroll t hrough t he HTML code t o find one of t he cont rols you placed on t he form . Not ice how t he definit ion is sim ilar t o a st andard HTML cont rol definit ion prefaced wit h t he designat ion, "ASP: " . This is t he nam espace alias for t he cont rol and indicat es t o t he web server where t o look t o find t he HTML definit ion for t he cont rol. Also com m on t o all of t he cont rols you creat ed is t he " runat = 'server'" propert y. This indicat es t o t he web server t hat processing should be com plet ed before t he HTML is sent t o t he client . Each web cont rol is defined wit h t he sam e nam e it is given in t he t ool box, i.e. a HyperLink cont rol is defined as < ASP: HyperLink> , not < A> as you m ight expect . The t ranslat ion int o com m on HTML cont rols, such as HyperLinks int o anchor t ags, will t ake place only when a browser request s t he page.

Handling Events Web cont rols in ASP.Net have t he abilit y t o respond t o client - side event s wit h server- side handlers. This new funct ionalit y allows developers t o creat e cont rols t hat can define event handlers on bot h t he client and server side of t he page. The event handler is sim ply a piece of code t hat is execut ed in response t o a defined event , such as a but t on click. I t is im port ant t o not e t hat cont rols cannot have bot h client - and server- side handlers defined for a single event . Perhaps t he m ost im port ant new feat ure in t he I DE is t he support for server- side event handlers. This gives developers t he abilit y t o sim ply double- click on a web cont rol t o be t aken t o t he code for t hat cont rol. Visual Basic.NET responds t o double- clicks by displaying t he default event for t he specified cont rol. This allows t he developer t o quickly and easily place cont rols on a form and specify event handlers for t hose cont rols. Ret urn t o t he Design view of your web form and double- click on one of t he cont rols.

Figure 5.5 ASP.Net pages have self- subm it t ing form s t hat allow server- side event handlers t o be execut ed.

I n figure 5.5, t he following st eps are out lined: A request for a web form is made. z

The web form returns standard HTML to the client.

z

The user raises an event, such as clicking a button.

z

Execution is passed back to the web form on the server.

z

Code in the appropriate event handler is executed.

z

NLX A new version of the HTML is returned to the client.

St andard ASP program m ing rest rict s com m unicat ion bet ween t he client and server t o form subm issions and page request s. Web cont rols enhance t his com m unicat ion by enabling event handlers for all cont rols. Thus, a list box can execut e code in response t o t he user select ing a different opt ion and a radio but t on can respond when t he user select s a new value. This enhanced funct ionalit y elim inat es t he need for t he developer t o m anually creat e ext raneous HTML form s whose only purpose is t o send cont rol of t he page t o t he web server. Tip: emember that the event handler code is executed on the server side, not the client-side!

The code for t he event handler will be im m ediat ely fam iliar t o any Visual Basic program m er. The event handler nam e is sim ply t he nam e of t he cont rol appended wit h an underscore ( " _" ) and t he nam e of t he event . I n t he case of a new list box value select ion, t he event handler is " List Box1_Select edI ndexChanged" . Passed as argum ent s t o t he event handler is a reference t o t he obj ect t hat raised t he event and an obj ect of t ype Syst em .Event Args, which specifies det ails about t he event it self. I n addit ion t o t he event handler creat ed by double- clicking t he cont rol, you will not ice t wo ot her event handlers creat ed for every web form : WebForm _Load and WebForm _I nit . The Load event of t he WebForm is fired j ust before t he web cont rols are parsed int o HTML by t he web server. This allows t he page t he opport unit y t o m ake any necessary changes t o t he cont rols before t he processing occurs. For inst ance, it is com m on pract ice t o populat e a label cont rol wit h t he nam e of t he person current ly signed int o t he websit e. You've probably seen t his on websit es such as Am azon.com where t he sit e indicat es, " You are logged in as David Burget t . I f you are not David…" I f t he current user nam e is st ored in a Session variable, t he WebForm _Load event would be used t o change t he t ext propert y of an ASP.Net Label web cont rol t o indicat e t he act ual nam e of t he user. This is an excellent exam ple of event - driven program m ing in ASP.Net since t he label capt ion is being populat ed in response t o an event ( t he web form 's load event ) , not j ust as code on t he page it self. The second st andard event handler, WebForm _I nit , defines t he code necessary t o creat e t he web form and cont rols and is generally hidden wit hin t he I DE. Note: caution when editing the WebForm_Init method.

Changing this code may cause your form to load

incorrectly and not display in design mode.

To creat e a sim ple dem onst rat ion of web cont rols event handling, add a Label web cont rol and a List Box web cont rol t o your web form . Select t he List Box and set t he Aut oPost Back propert y t o " True" . Open t he I t em s propert y and add several it em s t o t he List Box. Double- click t he List Box t o view t he List Box1_Select edI ndexChanged event handler. Ent er t he following code: Label1.Text=ListBox1.SelectedItem.Text

This code sim ply changes t he t ext propert y of t he Label t o t he value select ed in t he List Box. Pressing F5 runs t he code and select s an it em in t he List Box; t he Label cont rol aut om at ically updat es t o reflect t he it em select ed in t he List Box. You m ight have t o wait several seconds t he first t im e you load t he page and t he first t im e you click an it em in t he List Box. The reason for t his wait is t hat ASP.Net is aut om at ically checking for new versions of a com piled .DLL and new web pages for t his applicat ion. Click a different it em in t he List Box and you will not ice t hat t he response is im m ediat e. Note: Be prudent when setting AutoPostBack=True in order to avoid pages which make frequent, repeated trips to the server. y default, only the button click events are automatically posted back to the server. This closely resembles the standard HTML form submission model.

Som e developers will undoubt edly com plain t hat web cont rols do not offer anyt hing t hat was not available previously. I ndeed, it is possible wit h classic ASP, or any ot her server- side web developm ent language, t o creat e server- side handlers for client - side event s. I t is t rue t hat ASP.Net sim ply uses com m on t ools, such as HTML form subm ission and JavaScript , which are readily available t oday. I t is im port ant t o not e, however, t hat ASP.Net offers all of t his funct ionalit y in a WYSI WYG, rapid applicat ion developm ent environm ent , drast ically reducing developm ent t im e, all wit hout com prom ising browser independence. This represent s a significant advancem ent for web developm ent . Round-Tripping and ViewState Now t hat you have seen web cont rol event handling in act ion, you are no doubt wondering, " How does it do it ?" I n order t o bind client - side event s t o server- side event handlers, ASP.Net uses a t oolbox consist ing of client - side script s, HTML form s subm issions, and st at e m anagem ent . Each t im e a handled event occur s on t he client - side, t he page uses a com binat ion of JavaScript and form subm issions t o pass processing back t o t he server- side code. Each ASP.Net page subm it s back t o it self, allowing cont rols and t heir event handlers t o be defined on a single page. I n ASP, form s usually post t o second page, breaking t he cont rols and t heir event s int o separat e set s of code. When an ASP .NET event is raised, t he server- side handler is execut ed and a new page is delivered t o t he client . This is called "round- t ripping." During t his round- t rip, st at e m anagem ent is em ployed t o ensure t hat each of t he cont rols not affect ed by t he event ret ain t heir current propert ies. Note: Due to the overhead of each round trip to the server, some events which occur frequently on a page, such as OnMouseOver, do not call server-side event handlers.

Client - side JavaScript is required t o force a browser t o m ake a call t o t he server in circum st ances where it norm ally would not . An exam ple of t his is shown in List ing 5.1. I n our previous exam ple, t his consist ed of a user m aking a select ion in a list box. Since HTML has no support for m aking a page request in response t o a list box select ion, ASP.Net exam ines t he browser m aking t he request t o det erm ine if it support s JavaScript . I f it does, t hen ASP.Net aut om at ically creat es sim ple JavaScript t o force t he browser t o m ake a request t o t he server t o handle t he event . I f t he browser does not support JavaScript , t hen t he event cannot be aut om at ically handled and will inst ead be cached and handled on t he next form subm ission. List in g 5 .1 ASP.N e t Au t om a t ica lly Cr e a t e s t h e N e ce ssa r y Ja va Scr ipt t o For ce t h e Br ow se r t o Re spon d t o t h e On Ch a n ge Eve n t of Th is Te x t box





I n order t o t ranslat e request s t o run an event handler ina synt ax t hat HTML can underst and, all web form s are required t o have an HTML form . The HTML form act s as a conduit for event s, passing page execut ion from t he client back t o t he server for event handling. Client - side event s are processed by set t ing form propert ies and t hen execut ing a program m at ic subm ission of t he form . Since form s are a very basic HTML requirem ent , t his m odel ensures t hat t he widest possible range of browser t ypes and versions can support t he ASP.Net program m ing m odel.

AM FL Y

I n addit ion t o describing a m eans of com m unicat ion bet ween t he client - side event s and t he server- side handlers, ASP.Net also defines a m et hod for st oring an obj ect 's st at e. This allows t he web cont rols t o act as independent obj ect s, m aint aining t heir own propert ies despit e changes in ot her page obj ect s. When a classic ASP page m akes a form subm ission, all of t he HTML cont rols on t he page are dest royed and recreat ed. The developer t hen m ust program m at ically repopulat e t he cont rols wit h t he appropriat e values.

TE

ASP.Net handles t his aut om at ically, using a hidden form field nam ed, " __VI EWSTATE" . The ViewSt at e form field holds an encrypt ed st ring which describes all of t he web cont rols on t he page and t heir st at e when t he page was loaded. On t he server- side, t he __VI EWSTATE propert y direct ly t ranslat es t o t he " St at e" propert y of t he page class. This allows t he developer t o access t he st at e inform at ion program m at ically wit hin event handlers. List in g 5 .2 Th e H idde n VI EW STATE Fie ld St or e s En cr ypt e d W e b Con t r ol St a t e I n for m a t ion





There are t wo im port ant elem ent s t o not e about t he ViewSt at e field. The first im port ant not e is t hat t he ViewSt at e, as m ent ioned above, is encrypt ed. The inform at ion is encrypt ed and decrypt ed on t he server,

Team-Fly®

which is im port ant because it ensures t hat t he user never has t he opport unit y t o alt er t he exist ing st at e inform at ion. This is crit ical in e- com m erce applicat ions where t he t ext propert y of a web label on a shopping cart page would display t he t ot al am ount of t he purchase. Wit hout t he ViewSt at e's encrypt ion, a savvy user could fairly easily alt er t his inform at ion, set t ing any price t hey like for t he purchase, all wit hout t he applicat ion's knowledge. The encrypt ion of t he ViewSt at e inform at ion prevent s t am pering and m akes using web cont rols m uch m ore secure. The second im port ant not e about t he ViewSt at e field is a warning t o web developers: be careful how m uch inform at ion you st ore in t he ViewSt at e. Each web cont rol has a m aint ainSt at e propert y which allows t he developer t o det erm ine whet her or not t he st at e inform at ion for t he cont rol should be st ored in t he ViewSt at e. This is a crit ical decision when you consider t he am ount of inform at ion any given web cont rol m ight cont ain. I n t he case of gasTI X, a sim ple label web cont rol m ay cont ain only a few dozen charact ers, like t he nam e of a band. A m ult i- line t ext box on t he sam e page m ay cont ain several hundred words in a descript ion of t hat band's album s. Consider, however, t he am ount of dat a cont ained wit hin a Dat aGrid displaying a list of upcom ing concert s. There could easily be t housands of lines of t ext describing venue nam es, locat ions, show t im es, and opening bands. I t is im port ant t o t ake int o considerat ion t he fact t hat you m ay be increasing t he size of t he page downloaded t o t he client by 50- 100k or m ore when set t ing m aint ainSt at e t o t rue for Dat aGrids full of dat a. While t he pot ent ial size and increased download t im e of t he ViewSt at e field is im posing, t he upside of using ViewSt at e is easier developm ent and im proved page perform ance. Consider t he com m on scenario of a user updat ing his personal inform at ion. When t he page loads, a dat abase query m ust be run t o ret rieve all pert inent inform at ion about t he user. This inform at ion is st ored in t he appropriat e web cont rols on t he web page. Suppose t hat t he user t hen updat es som e of his inform at ion and subm it s t he form . I n addit ion t o execut ing t he appropriat e updat e st at em ent s, classic ASP pages m ust request t he ent ire dat aset again in order t o populat e t he HTML cont rols. An ASP.Net page, however, has aut om at ically populat ed t he t ext boxes wit h t he appropriat e values before t he event is handled. The St at e propert y of t he Page class offers anot her benefit which experienced web developers will appreciat e. Many web developers use t he t rick of using hidden form fields t o pass im port ant inform at ion from one page t o anot her. The problem wit h using t hese hidden form fields is t hat t hey really aren't hidden very well; a user needs only t o view t he HTML source of t he page t o view t he " hidden" inform at ion. ASP.Net can now save t his inform at ion in t he page st at e propert y inst ead of hidden form fields. The dat a st ill get s passed t o t he client and back like hidden form fields, but it is now encrypt ed in t he " __VI EWSTATE" form field and hidden from prying eyes. The Life Cycle of a Page Object As you have already seen, t he web is a st at eless program m ing m odel. Each page t hat is request ed m ust be creat ed from scrat ch since no inform at ion about t he page is st ored on t he server. This is what allows t he use of web farm s, where t wo different servers m ay be used t o fulfill subsequent request s for a single page. I n order t o underst and how t he st at e inform at ion described in t he previous sect ion is used, it is necessary t o underst and how each page is creat ed. Each page obj ect undergoes a predefined set of st eps necessary t o load t he obj ect , bind dat a obj ect s, handle event s, and perform cleanup.

Figure 5.6 Each page goes t hrough a t hree st ages during processing: configurat ion, event handling, and clean up.

The first st age of t he page life cycle is configurat ion. This st age allows t he page t o rest ore t he st at e inform at ion for t he page obj ect and each of it s const it uent cont rols. During t his st age, t he Page_Load event fires, t hus checking t he st at us of t he I sPost Back propert y of t he Page obj ect . The I sPost Back propert y is a sim ple Boolean value which specifies whet her t he page is being creat ed from scrat ch or in reference t o a post back. When an event is encount ered on t he client - side of t he page, t he page aut om at ically ( if t he Aut oPost Back propert y is set ) post s back t o it self t o handle t he event . Since t he ASP.Net page aut om at ically st ores t he st at e of web cont rols on t he page, t he I sPost Back propert y is checked t o det erm ine whet her cont rols need t o be init ialized or not . I f t he I sPost Back propert y is set t o t rue, m eaning t hat t he page has been loaded previously and is current ly responding t o an event , t hen cont rols on t he page do not need t o be reinit ialized. This drast ically im proves processing speed by elim inat ing dat abase calls t o set cont rol propert ies. At t he t im e t he Page_Load event fires, all cont rols on t he page are creat ed and init ialized wit h eit her t heir default or ViewSt at e propert ies. This event is t ypically used t o set cont rol propert ies, as well as perform init ial dat a binding and t o reevaluat e dat a binding expressions on subsequent round t rips. Dat a binding is a very powerful way t o quickly load dat a from a dat abase int o list boxes, dat agrids, and ot her cont rols. The second st age in t he page obj ect life cycle is event handling. This st age is skipped if I sPost Back is set t o false, indicat ing t hat t his page is not being request ed in response t o an event . ASP.Net aut om at ically fires t he handler associat ed wit h t he specified event , as well as handlers for any cached event s. For m or e inform at ion about cached event s, see t he next sect ion, “ Advanced Feat ures of Web Cont rols.” The event handling st age t ypically includes act ions such as dat abase updat es, st at e m anagem ent updat es, and cont rol propert y updat es. I f t he user has updat ed t he inform at ion st ored in t he dat abase, t he event handler for t he change ( i.e. Text Box_Text Changed) would be used t o execut e a SQL updat e st at em ent . I f t he page is m aint aining specific st at e m anagem ent inform at ion, such as t he num ber of users logged on t he syst em , t his inform at ion could be updat ed in t he event handler. Likewise, event handlers are frequent ly used t o updat e cont rol propert ies, such as t he t ext propert y of a st at us label cont rol. I f t he page cont ains validat ion cont rols, t he I sValid propert y of each validat ion cont rol is checked during t he event handling st age and t he appropriat e act ion is t aken.

The final st age of t he life cycle is cleanup. This st age fires t he Page_Unload event which should be used t o close files and dat abase connect ions and explicit ly discard any obj ect s creat ed by t he page. Note: Even though the .Net CLR includes automatic garbage collection, it is recommended that the page explicitly close database connections and other expensive resources to ensure that they do not continue to consume resources after the page is unloaded.

Advanced Features of Web Controls Web cont rols include m any advanced feat ures which significant ly ease t he developm ent and add im pressive funct ionalit y t o ASP.Net web applicat ions. From a developm ent st andpoint , t he consist ent obj ect m odel is probably t he m ost im port ant st ep t owards bringing web applicat ion developm ent int o line wit h st andard applicat ion developm ent . The current HTML specificat ion has been pieced t oget her by different groups and affect ed by a variet y of browsers, which has led t o fragm ent ing of t he HTML obj ect m odel. The t ype and version differences of available script ing languages accept ed by different browsers has fut her exacerbat ed t he problem . To ease web developm ent , t he ASP.Net developm ent t eam worked diligent ly t o ensure t hat all ASP.Net web cont rols share a com m on obj ect m odel and consist ent nam ing convent ion. Each of t he web cont rols packaged wit h ASP.Net derive from a com m on ancest or, Syst em .Web.UI as show in figure 5.7. Since all web cont rols derive from a single cont rol, t hey share sim ilarly nam ed propert ies. For inst ance, bot h Text Box and a Label web cont rols have " t ext " propert ies, despit e t he fact t hat t hey m ap int o HTML quit e different ly. This consist ency m akes ASP.Net m uch easier for novices t o learn and rem oves som e of t he burdens for experience program m ers. Anot her im provem ent in ASP.Net is t he enhanced WYSI WYG page designer. Previous versions of Visual St udio.NET creat ed a WYSI WYG int erface for web page design by t rying t o cobble t oget her client - side HTML elem ent s wit h different int erfaces and propert ies. The new Visual St udio.NET m akes use of t he server- side processing and t he unified obj ect m odel t o creat e an int erface which allows developers t o easily drag and drop web cont rols int o place and m odify t heir propert ies wit hin a single propert ies dialog. Visual St udio.NET also provides support for absolut e posit ioning of cont rols on t he page using CSS absolut e posit ioning. The developer sim ply drags t he cont rols t o t he desired locat ion, j ust as he would in Visual Basic, and t he web server aut om at ically defines st yle at t ribut es wit h t he absolut e posit ioning definit ion. For m axim um backwards com pat ibilit y, Visual St udio.NET also support s t he use of t ables t o posit ion elem ent s on t he page. Swit ching bet ween t hese t wo posit ioning m odels, CSS and t ables, is achieved by sim ply specifying t he t arget Schem a proper t y of t he page obj ect t o eit her HTML 4.0 or HTML 4.2. Web cont rols also im prove t he event handling m odel by adding support for nest ed and cached event s. Nest ed, or bubbled, event s occur in web cont rols t hat act as cont ainers for ot her cont rols. For inst ance, t he Repeat er cont rol explained in Chapt er 11, “ Event Searching,” creat es a variet y of web cont rols for

each row t hat exist s in t he dat abase. Each of t he web cont rols cont ained wit hin t he Repeat er is a full- fledged web cont rol wit h it s own propert ies, m et hods, and t he abilit y t o raise event s. I n order t o avoid requiring developers t o writ e event handlers for every cont rol cont ained wit hin t he Repeat er, event s of t he child cont rols will aut om at ically bubble up t o t he cont ainer t o be handled by a single set of code. The OnI t em Com m and event of t he Repeat er

cont rol ( as well as t he Dat aGrid and Dat aList cont rols) passes along a param et er which specifies which child code creat ed t he act ual event .

Figure 5.7 All web cont r ols, including pages, deriv e from a single ancest or in t his port ion of t he ASP.Net obj ect m odel.

Event s can also be cached on t he client - side. As explained previously, web cont rols have an Aut oPost Back propert y which, when set t o True, specifies t hat t he cont rol should aut om at ically post back t o t he server when an event is t riggered. There are sit uat ions, however, where t he developer will not want every cont rol t o t rigger a post back, so ASP.Net allows event s t o be cached for lat er processing. When an event is t riggered on a web cont rol wit h Aut oPost Back set t o false, t he client aut om at ically adds t hat event t o a list of event s t hat need t o be handled when processing is sent back t o t he server. When anot her event is encount ered, for which Aut oPost Back is set t o True, processing is t ransferred t o t he server, where all cached event s are handled and t hen t he post ing event is handled as well. Warning: There is no specified order in which cached events will be handled. dependent on the execution of a specific order of event handlers.

Do not write code that is

Dat a binding is t he process of aut om at ically at t aching a user int erface cont rol t o a dat a source. The cont rol t hen displays inform at ion ret rieved from t he dat abase. Dat a binding has been available in Windows applicat ion developm ent for m any years and has seen som e lim it ed funct ionalit y in web developm ent in t he m ore recent past . Dat a binding in ASP.Net , however, is revolut ionary in t hat it no longer lim it s t he t ypes of obj ect s t hat can be bound t o or how t he bound dat a is displayed. Many web cont rols can be dat a bound in ASP.Net t o eit her sim ple or com plex dat a sources. Furt herm ore, m ost of t he basic propert ies of each web cont rol can be bound, not j ust t he t ext propert y as in t he past . A sim ple dat a source is any propert y or variable t hat is of t he correct t ype for t he propert y being bound t o. Thus, t he t ext propert y of a t ext box can be bound t o a st ring variable while t he height propert y can be bound t o a num eric variable. Likewise, eit her propert y can be bound t o a sim ilar propert y on anot her t ext box or any ot her t ype of cont rol. Dat a binding offers great flexibilit y in defining propert ies for a wide range of cont rols and applicat ions while m aking it easy t o bind a st ring of cont rols t oget her so t hat t hey always share t he sam e propert y values. I t is t rivial, for inst ance, t o creat e m ult iple im age cont rols on a page, binding each one's height and widt h t o t he height and widt h of a specified im age. This would ensure t hat every im age on t he page would always have t he sam e height and widt h. I n addit ion, for sim ple dat a binding, t hat is binding t o a single value, ASP.Net fully support s binding t o com plex dat a sources. A com plex dat a source is a collect ion of dat a, such as a dat abase query result set . I n fact , web cont rols can be bound t o any st ruct ure t hat im plem ent s t he I Enum erable int erface, as show in Figure 5.8. This includes not only inform at ion ret rieved from a dat abase, but also Arrays, Dict ionaries, Queues, and St acks. I t also includes any collect ion of dat a exposed by a cont rol's propert y, such as t he list box's Obj ect Collect ion propert y. Since XMLNodeList im plem ent s t he I Enum erable int erface as well, web cont rols can even bind direct ly t o XML docum ent s, as dem onst rat ed in gasTI X. The flexibilit y t o bind t o ot her cont rols, XML docum ent s, and sim ple, program m at ic const ruct s enables a whole new world of web applicat ion developm ent . See chapt ers 11, “ Event Searching,” and 12, “ Purchasing A Ticket ,” for m ore exam ples of dat a binding. The second im provem ent in dat a binding involves displaying t he dat a t hat has been bound t o t he cont rol. The m ost obvious im provem ent for displaying t his dat a is t he abilit y t o creat e dat a binding expressions. A dat a binding expression is sim ply a reference t o t he part icular colum n or field of dat a being bound wit h a funct ion applied t o it . The funct ion can be sim ple or com plex and can change anyt hing about t he dat a, including t he t ype. Thus, a st ring propert y could be bound t o a num eric dat a source by appending a t oSt ring( ) m et hod t o t he dat a reference. Displaying bound dat a has also been great ly im proved t hrough t he int roduct ion of t em plat es. Tem plat es offer m uch m ore cont rol over t he user int erface of bot h packaged and cust om cont rols. Tem plat es allow developers t o pass in m ore t han j ust a few sim ple propert ies t o cont rols. I nst ead, m ult iple t em plat es ar e defined wit hin t he beginning and ending t ags of t he cont rol definit ion. The Repeat er, Dat aGrid, and Dat aList cont rols all use t em plat es t o display dat a ret rieved from a dat abase. See chapt er 11, “ Event Searching” for m ore inform at ion on t hese cont rols. Each of t he t hree dat a cont rols accept s t em plat es for norm al it em s, for alt ernat ing it em s, and for t he header and foot er. The basic t em plat e com m on t o all repeat ing dat a cont rols is " I t em Tem plat e." This t em plat e specifies not only cosm et ic it em s such as font and color, but also funct ional it em s such as hyperlinks and but t ons for every it em ret rieved from t he dat abase. Separat ing t he logic and design of each it em from t he code needed t o apply a dat aset t o it creat es cleaner code and easier m aint enance.

Figure 5.8 Web cont rols can be bound t o any derivat ive of t he I Enum erable int erface.

I s List ing 5.3, t he I t em Tem plat e defines hyperlinks t o inform at ion about perform ers displayed in gasTI X. Likewise, t he Alt ernat ingI t em Tem plat e creat es t he sam e inform at ion on a grey background for every ot her row, im proving readabilit y. List in g 5 .3















The Alt ernat ingI t em Tem plat e, if present , overrides t he I t em Tem plat e for every even num bered it em . This m akes it t rivial t o creat e t ables of dat a where every ot her row has a different color background in order t o facilit at e easier reading. The Header and Foot er t em plat es allow t he developer t o specify t ext and cont rols, such as record count or sort but t ons, which belong at t he t op or bot t om of t he dat a. Validat ion cont rols are a specific t ype of web cont rols which perform a sim ple funct ion: validat ing user input . Validat ion cont rols can validat e t hat t he dat a input by t he user falls wit hin a specified range or m at ches a specified pat t ern or param et er. Cust om validat ion cont rols can even be writ t en t o allow developers t o creat e t heir own validat ion rules. Validat ion happens aut om at ically on a page level. I f any cont rol on t he page fails validat ion, t he ent ire page is m arked as invalid. ASP .NET will not subm it a form unt il t he page passes t he validat ion st age successfully. The developer can also program m at ically validat e cont rols on an individual basis. When validat ion fails, a cust om ized error m essage is displayed t o t he user t hat includes inform at ion about all failed cont rols. This m essage is configurable and can be placed anywhere on t he web page. For m ore inform at ion about validat ion cont rols, see chapt er 12, “ Purchasing A Ticket .”

User Controls Two of t he m ain goals of obj ect - orient ed program m ing are encapsulat ion and im proved reusabilit y. Encapsulat ion is t he process of hiding t he im plem ent at ion det ails of som e piece of code in a convenient package. This im proves t eam developm ent and eases m aint enance by grouping relat ed funct ionalit y int o sm all, m anageable obj ect s. I f t hese obj ect s are program m ed carefully, t hey can be widely reused in a variet y of applicat ions, none of which need t o know t he int ernal workings of t he obj ect s. ASP.Net finally brings t he benefit s of obj ect - orient ed program m ing int o t he web developm ent world wit h t he advent of User Cont rols. User cont rols are t ypically sm all, reusable chunks of web UI and logic t hat form a funct ional unit . This unit can be reused in a variet y of sit uat ions on m ult iple pages, reducing developm ent t im e and im proving m aint ainabilit y. A user cont rol is sim ilar t o a web form , in t hat it cont ains web cont rols and HTML cont rols, as well as t he code behind t hese cont rols. User cont rols cannot , however, st and alone; t hey require a cont ainer docum ent . User cont rols follow st andard ASP.Net page convent ions, wit h t he except ion t hat t hey cannot have page- level t ags, such as < HTML> , < BODY> , or < FORM> t ags. Since user cont rols can only be displayed

wit hin a parent ASP.Net page obj ect , t he user cont rol will m ake use of t he exist ing page- level t ags wit hin t he parent page. I ncluding addit ional page- level t ags in a user cont rol will creat e m ult iple t ags in t he HTML sent t o t he client , which will cause t he page t o be displayed incorrect ly. A t ypical user cont rol will encapsulat e com m on funct ionalit y t hat is needed on m ult iple pages, even across m ult iple web applicat ions. Com m on uses for user cont rols include: z

login controls, which provide textboxes for the user to enter a name and password and a button to submit the information

z

search criteria, which include a text box for entering text to search for and list or check boxes for setting the search options.

z

search results, which would define the formatting of the search results

Each of t hese exam ples defines a sm all, concret e set of funct ionalit y t hat is required on m ult iple pages and m ult iple applicat ions. Any well- defined piece of work can be encapsulat ed in a user cont rol t o be included across m ult iple pages. Tip: If you find yourself writing the same code on more than one page, you could save time and improve maintainability by making a user control to encapsulate the desired functionality.

The gasTI X websit e m akes use of several user cont rols for including t he navigat ion bar, creat ing personalized cont ent , and displaying perform er inform at ion. Each page wit hin t he Search port ion of t he gasTI X websit e m akes use of t wo user cont rols t o define t he navigat ion bar along t he left side of t he page and t o creat e t he inform at ion panel along t he t op of t he page, as shown in Figure 5.9

Figure 5.9 Each page in t he gasTI X search applicat ion uses t wo user cont rols t o creat e a consist ent look and feel across t he ent ire websit e.

I nst ant iat ing t he t em plat e user cont rols for each search page is very sim ilar t o perform ing a server- side include. The difference is t hat user cont rols are fully obj ect orient ed and can accept propert ies and perform m et hods t hat include files cannot . User cont rol propert ies are well defined, allowing t he developer t o perform bounds checking on t heir values and m ake program m at ic m odificat ions if necessary. User cont rols have full access t o t he ASP.Net obj ect m odel and act as st andard web cont rols in t erm s of allowing and gaining access t o ot her cont rols. User cont rols can also define event s. Event s raised wit hin a user cont rol can be handled int ernally in t he user cont rol it self, or raised as an event t o t he page cont aining t he cont rol. This allows developers great flexibilit y in creat ing user cont rols t hat handle all int rinsic funct ionalit y while offering som e cont rol t o t he cont aining page. For inst ance, a user cont rol t hat allows users t o ent er search crit eria m ay ne1ed t o int ernally handle event s when opt ions are set t o validat e t he select ed opt ions, while allowing t he parent page t o handle t he search but t on click event . This would allow developers using t he search user cont rol t o writ e t heir own dat abase queries while not forcing t hem t o validat e t he user- select ed opt ions on every page t hat uses t he cont rol. User cont rols are st ored like st andard web form s, except t hey m ust have t he file ext ension, " .ascx." User cont rols are included in a web page by creat ing a page direct ive t hat t ells t he page how t he cont rol will be called and where t he file is t hat should be used. The following code regist ers t he gasTI X Tem plat eTop user cont rol wit h t he page:

Once a regist er direct ive has been placed on a page, t he user cont rol is added t o t he page like any ot her cont rol. I nst ead of t he t ypical "ASP: " prefix used for web cont rols, t he prefix defined in t he regist er direct ive m ust be used. The definit ion of a gasTI X Tem plat eTop user cont rol would be:

Just like st andard web cont rols, user cont rols can be added t o a web page program m at ically using t he LoadCont rol m et hod of t he Page obj ect . The LoadCont rol m et hod t akes a st ring pat h t o t he .aspx file of t he user cont rol and t ypes t he newly loaded cont rol by replacing t he dot wit h an underscore. Thus, LoadCont rol( " Tem plat eTop.ascx" ) creat es a cont rol of t ype " Tem plat eTop_aspx" . The gasTI X websit e also uses user cont rols t o display cat egories and subcat egories of perform ers for who t icket s are available. Each perform er is cat egorized int o a generic cat egory and t hen furt her specified by a subcat egory. For inst ance, t he gasBags bag pipe band is cat egorized under t he " Celt ic" subcat egory of t he generic " Music" cat egory. The relat ionship bet ween cat egories and t heir subcat egories is som et hing t hat needs t o be displayed in several places across t he gasTI X websit e, t hus a cat egory user cont rol was creat ed t o encapsulat e t his funct ionalit y. The cat egory user cont rol displays inform at ion about cat egories defined in t he SQL Server 2000 dat abase and uses a nest ed subcat egory cont rol t hat displays inform at ion about t he subcat egories under each cat egory. This allows developers t o quickly creat e an organized list showing parent - child relat ionships by sim ply dropping a cont rol and set t ing a couple of propert ies, and ensures display consist ency across t he websit e.

Separation of UI and Code Anot her benefit of ASP.Net is an im proved separat ion of t he code from t he user int erface ( UI ) . One of t he largest problem s facing developers building large, t radit ional ASP applicat ions is t he com binat ion of design and code in a single page. The UI defined by t he HTML in a t radit ional ASP is oft en inext ricably t ied t o t he code used t o m odify t he UI . This not only m akes m aint enance of bot h t he UI and t he code m uch m ore difficult , it m akes t he init ial creat ion of t he page m ore com plicat ed as well. Several challenges arise from t he fact t hat bot h t he UI designer and t he code developer have t o m odify a single set of code. The m ost obvious is t hat t he designer and t he developer cannot work on t he page at t he sam e t im e. The second challenge is t he pot ent ial for one t o inadvert ent ly alt er t he ot her's work, breaking eit her t he form or t he funct ionalit y.Web form s largely elim inat e t his problem by enabling " code- behind" classes, where t he event handlers for a web page are st ored in a separat e file. This allows t he designer t o creat e t he .aspx page while t he developer works on a .vb page which cont ains code for t he .aspx page. The t wo pages are t ied t oget her t hrough t he CodeBehind at t ribut e of t he Page direct ive. While it is st ill possible t o use t radit ional inline script ing inst ead of code behind, it is st rongly recom m ended t hat code behind is used whenever possible. The added m aint ainabilit y of t he code easily j ust ifies t he sm all learning curve needed t o adj ust t o t he new st yle of ASP program m ing. The sam ples in t his book and t he gasTI X websit e use code behind exclusively, as dem onst rat ed in List ing 5.4.

AM FL Y

List in g 5 .4

Th e Code Be h in d Pa ge for ga sTI X Se a r ch Pa ge s Se t s t h e Te x t Pr ope r t y of t h e lblH e a de r Cont r ol



TE



Code Sample: Each gasTIX search page has a Label web control defined by the designer; all the developer needs to know is the name. Dim SC As New gasTIX.BusinessFacade.Subcategory() Dim dvXML As DataView Dim dsTemp As New DataSet() dsTemp.ReadXml(New StringReader (SC.ListParticipants(Request.QueryString("SubCategory_ID").ToInt32()))) If Request.QueryString("debug") "" Then response.Write(Request.QueryString("Name")) Response.Write(server.htmlencode (SC.ListParticipants(Request.QueryString("SubCategory_ID").ToInt32()))) End If If dsTemp.Tables.Count > 0 Then dvXML = dsTemp.Tables("Participant").DefaultView With rptParticipants .DataSource = dvXML

Team-Fly®

.DataBind() End With Else lblNoRecords.Text = "There are no participants in this subcategory." End If lblHeader.Text = "Participants in the Category..SubCategory Name (" & rptParticipants.Items.Count & " records found)"

Each result s page in t he gasTI X Search applicat ion has a Label web cont rol defined on t he page t o indicat e t o t he user t he search for which t he result s are present ed and t he num ber of result s found. For exam ple, if t he user clicks t he "Celt ic" subcat egory on t he hom e page, t he result s page displays a label indicat ing t hat one record was found in t he "Music..Celt ic" cat egory. Since t he developer knows t hat a web cont r ol nam ed " lblHeader" will be present som ewhere on t he page, he sim ply inst ruct s t he code behind page t o specify t he t ext propert y of t he label. He never needs t o know t he st yle or locat ion of t he label, j ust it s nam e.

From Here ASP.Net finally brings t he benefit s of obj ect - orient ed program m ing int o t he world of web developm ent . ASP.Net feat ures m uch bet t er encapsulat ion and reusabilit y t hen ever before, allows developers and designers t o work separat ely on a single proj ect , and allows developers t o writ e code in t he language of t heir choice. I n addit ion, perform ance im provem ent s, enhanced securit y, and int eroperabilit y wit h t he .Net servers m ake ASP.Net an excellent choice for larger scale, ent erprise crit ical applicat ions.

6 Implementing .NET Business Services The business services layer of t he gasTI X applicat ion is t he t ier t hat is responsible for enforcing business rules, processing inform at ion, and m anaging t ransactions. I t is in t he business services layer t hat a developer builds reusable com ponent s. Business services com ponent s are not t ied t o any specific client , and can service m ult iple applicat ions, as well as reside in separat e locat ions from t he client s. The business services layer is crit ical t o applicat ions, even t hough it does not creat e t he look and feel of t he applicat ion. Wit h t he int roduct ion of .NET, Microsoft has m ade one of t he m ost significant plat form changes since t he int roduct ion of Windows t o t he DOS world. These changes are far m ore com prehensive t han j ust sim ple language changes, or t he creat ion of a new language. Many of t he changes in .NET affect t he im plem ent at ion of .NET business services, including t he fram ework classes, rem ot ing, and web services, j ust t o nam e a few. I f developers are not prepared for t he changes ahead, t hey m ay find t hem selves left behind on t he learning curve. Due t o t his plat form shift , m any developers and archit ect s alike are asking how t he im plem ent at ion of ent erprise applicat ions and business services will change from Windows DNA t o .NET. Perhaps t hey should also be asking how all t hese changes affect t he design of ent erprise applicat ions and business services, as well as what should be t he it em s one t hinks about when archit ect ing .NET applicat ions. The archit ect ural goals for gasTI X would have rem ained t he sam e, regardless of t he im plem ent at ion plat form . However, t he act ual design of gasTI X does vary in response t o t he im plem ent at ion plat form . The int roduct ion of t he .NET plat form has t herefore affect ed bot h t he gasTI X applicat ion design and im plem ent at ion.This chapt er begins by discussing t he archit ect ural goals of gasTI X. The chapt er t hen covers t he design of t he gasTI X Business Services and also covers t he reasons behind som e of t he design choices. Last ly t he focus is on what is new in com ponent developm ent wit h .NET.

Architectural Goals The t eam t hat was form ed t o design gasTI X consist ed of several experienced Windows DNA archit ect s and designers. Each t eam m em ber had previously part icipat ed in t he design and const ruct ion of ent erprise- class applicat ions. When t he design t eam m et t o begin t he design process for gasTI X, m any of t he decisions appeared t o already have been m ade, due t o t he t eam ’s experience. Each m em ber was fam iliar wit h bot h t he t heoret ical best pract ices for building Windows DNA applicat ions, as well as having form ulat ed t heir own "real" world best pract ices from t heir experiences. Som e design changes were appropriat e wit h t he int roduct ion of t he new feat ures incorporat ed in t he .NET plat form t hat were not present in t he Windows DNA plat form , but t he design and archit ect ural goals rem ained t he sam e. Most of what has changed wit h t he int roduct ion of .NET is t he act ual im plem ent at ion for ent erprise syst em s. When discussing ent erprise applicat ions we oft en use key words t o describe t heir funct ionalit y. These are oft en used by com panies when m arket ing t heir new applicat ions t o show how " great " and well designed t hey are. As a result t he following t erm s have becom e com m onplace wit hin all t echnology fields: z

Interoperability

z

Performance

z

Availability

z

Scalability

z

Maintainability

z

Extensibility

z

Security

I t is an im pressive list , but looking past all t he m arket ing- speak, what do all t hese t hings really m ean t o designers in t he real world? Designing an ent erprise syst em requires carefully weighing each of t he it em s in t his list against t he requirem ent s and goals of t he syst em . Several of t he it em s on t his list are int errelat ed, and are opposed t o each ot her in cert ain cases. This m eans t hat design decisions t hat are m ade can affect m ult iple it em s on t he list in bot h posit ive and negat ive ways. For exam ple, designing a syst em for scalabilit y can oft en negat ively affect t he perform ance of a syst em . The design t eam for gasTI X t ook t hese int errelat ionships int o account when designing t he archit ect ure for gasTI X. The prim ary design goals set fort h included: int eroperabilit y, perform ance, availabilit y, scalabilit y, and securit y.Maint ainabilit y and ext ensibilit y were also prim ary concerns; t heir im plem ent at ion is discussed in lengt h in Chapt er 4, “ I m plem ent ing .NET Present at ion Services” and Chapt er 11, “ gasTI X Event Searching” .

Interoperability I nt eroperabilit y deals wit h t he syst em 's abilit y t o int eract wit h ot her disparat e syst em s, pot ent ially on different plat form s. I n order t o have int eroperabilit y, it is not required t hat we choose t o use a st andard prot ocol, like SOAP over HTTP. I nst ead, we can im plem ent a propriet ary prot ocol or int erface t o com m unicat e bet ween t wo different syst em s. Bot h file- based and API int erfaces are com m only used as com m unicat ion m echanism s bet ween het erogeneous syst em s. A file- based int eroperabilit y syst em has a few obvious problem s. Som e of t hese problem s are: z

Transport across networks will not occur if the network doesn't support file based protocols.

z

There is no synchronous communication.

z

There is no efficient callback mechanism in place.

I f we im plem ent a file- based prot ocol ( FTP) , even if it were t o t ransport t hrough a firewall ( which m ay require firewall configurat ion) t here is no really good way t o get a feedback response from t he dest inat ion. Building a service t o poll for feedback would require ext ra developm ent effort , and is not necessarily t he m ost t im ely response m echanism t hat exist s. Using an API , which would solve t he response m echanism problem , m ay not t ransport well t hrough a firewall. Of course, one could open port s on t he firewall in order t o enable t his opt ion t o work, but t his would int roduce a securit y issue. The best choice is a st andard I nt ernet prot ocol t hat t alks on a port t hat t he firewall will m ost likely already have open. The designers chose t o use SOAP as t he prot ocol for m et hod invocat ion for gasTI X. SOAP calls are t ransport ed over HTTP, which is a st andard I nt ernet prot ocol, and will pass t hrough m ost firewalls wit hout t he need t o re- configure t he firewall. SOAP is not a Microsoft - only support ed prot ocol, so t here is lit t le t o no cust om developm ent involved in building a m essaging prot ocol like SOAP.

Note: SOAP is a W3C standard protocol that is supported by many web servers including but not limited to IIS, Websphere, and Apache.

The designers had no int erest in building a cust om prot ocol over HTTP t hat would require developm ent on bot h sides of t he pipe t o int erpret t he m essage. Good t ools exist on m ult iple hardware plat form s already t o support t he plum bing of sending and receiving SOAP m essages. Alt hough SOAP is not t he m ost efficient prot ocol, t he developers of gasTI X decided t hat it was wort h t he t radeoff for t he ease of developm ent , cost of developm ent , and flexibilit y t hat it offered.

Performance Perform ance refers t o t he responsiveness of a syst em t o a user's request . I t is t ypically m easured by using a set of m et rics like t he num ber of pages/ sec or t he num ber of t ransact ions/ sec. Due t o t he definit ion alone, one can see t hat perform ance can ( and probably should) be t alked about using discreet and concret e t erm s rat her t han nebulously. I nst ead of discussing t hat t he syst em m ust have good perform ance, t he discussion should focus on finit e m easures such as t hat t he syst em m ust be able t o support 50 pages/ sec or 200 t ransact ions/ sec. Also it is im perat ive t hat det ailed perform ance requirem ent s be st at ed up front in order t o t ake t hem int o account during design.. I f t hese requirem ent s are not st at ed up front , significant am ount s of work m ay be required t o m odify an already designed and const ruct ed syst em . There are t wo m ain t hings t hat can adversely affect perform ance. The first of t hese it em s is building a syst em for int eroperabilit y. Building a syst em t hat can int eroperat e wit h ot her syst em s using st andard prot ocols, like SOAP over HTTP, will cause a decrease in speed over using a binary prot ocol ( TCP is nearly four t im es t he speed of HTTP) . HTTP, alt hough port able, is not necessarily t he m ost efficient prot ocol. The second it em t hat can affect perform ance is scalabilit y. When building a syst em for only 10 users, one can opt im ize on knowing t hat fact . I n t his scenario, we are able t o hold resources for longer periods of t im e and gain responsiveness in t he syst em . When designing for scalabilit y, one t ries t o reduce t he lengt h t hat resources are held. This includes even expensive resources like a dat abase connect ion. Alt hough connect ion pooling and obj ect pooling can help, perform ance m ay suffer due t o wait ing on acquiring resources.

Availability Availabilit y refers t o t he abilit y of an applicat ion t o always be able t o respond. One view of availabilit y would involve having a web server farm where if one server becom es unavailable, anot her server would be " available" t o handle t hat server's request s. But availabilit y can go one st ep furt her t o include building a disast er recovery plan. A disast er recover plan addresses t he abilit y t o access t he syst em even if, for exam ple, t he ent ire building where t he prim ary equipm ent is locat ed get s dam aged or dest royed. . The lat t er would require separat e inst allat ions of soft ware at geographically separat e locat ions. The rem ot e in t he alt ernat e locat ion would com e on- line and handle user request s at t he press of a but t on, so t o speak.. Handling t he lat t er of t he cases oft en involves significant expense since duplicat e syst em s are required in different locat ions. Most com panies balance availabilit y by perform ing fail- over clust ering or web farm s in case a single m achine fails t o funct ion. I n doing so t hey accept t he level of risk t hat a single disast er can wipe out t heir ent ire syst em .

Scalability Scalabilit y refers t o t he syst em 's capabilit y t o handle considerably larger num bers of users while m aint aining bot h perform ance and availabilit y requirem ent s. As can be seen in t he provided definit ion, scalabilit y is closely relat ed t o bot h availabilit y and perform ance. Designing a scalable ent erprise syst em requires addressing bot h t he soft ware design and t he hardware im plem ent at ion. When designing Windows DNA applicat ions in t he past , t he design had t o include som e m echanism for st oring st at e inform at ion ( if t he client was web- based) . .NET provides designers wit h t his feat ure built - in, so t he im plem ent at ion is sim pler t han in t he DNA world. The designers of gasTI X know t hat consum pt ion of server resources is one of t he it em s t hat affect scalabilit y t he m ost . Obj ect - orient ed syst em s t ypically require significant num bers of obj ect s in m em ory and t hat consum es precious server resources. Holding a dat abase connect ion open for each user also consum es significant am ount s of resources. I f access t o resources is a bot t leneck, t hen t he scalabilit y of a syst em will be great ly affect ed. We did specific t hings t o address t he scalabilit y needs of t he applicat ion. First , designers decided t o use session st at e m anagem ent feat ures of ASP.NET t hat would allow for m ult iple web servers in t he web farm . Second, dat abase connect ion would not be dedicat ed t o a single user, but would be pooled ( as in COM+ ) . Third, " st at e- aware" com ponent s would be designed and built . When MTS first cam e int o exist ence, t he t erm " st at eless" was used t o describe t he way com ponent s should be built t o build scalable syst em s. Many people built all t he com ponent s in t he business services layer t o be " st at eless" . St at eless com ponent s hold no stat e inform at ion at all bet ween m et hod calls. The m isunderst ood " requirem ent " of st at elessness in all business services com ponent s caused and st ill causes m uch confusion for developers. The gasTI X designers do not believe t hat all classes built in t he business services layer are required t o be st at eless t o have a scalable solut ion. From t he view of t he present at ion services, t he business services ar e " st at eless" . The business façade ( which will be discussed lat er in m ore det ail) holds no st at e bet ween calls from t he present at ion services. Ot her com ponent s in t he business services layer behind t he business façade m ay in fact hold st at e bet ween calls from ot her obj ect s in t he business services layer. The lifet im e of t hese business services com ponent s will end when t he call from t he business façade is com plet ed. The design of gasTI X also needed t o t ake int o account t he addit ion of hardware t o support a large increase in users. Scaling t he present at ion services is t raditionally sim ple, by j ust building a web farm . The m ain issue t hat arises is t he issue of sharing session st at e across web servers, which .NET now t akes care of for us. There are t wo m ain approaches for scaling t he business services layer: Deploy t he business services com ponent s on t he sam e physical m achines as t he web servers, and build a farm of servers for t he business services. The design of gasTI X does not dict at e which opt ion will be used.

Security Securit y, at least in t he case of gasTI X, refers t o how prot ect ed t he sit e is from t hose at t em pt ing t o break int o it ( crackers) . Securit y also refers t o t he general principle behind how users will be set up and aut hent icat ed, and what encrypt ion m echanism is used t o send dat a securit y so t hat it is difficult t o read if int ercept ed.

.NET provides significant securit y enhancem ent s t o t he Windows DNA world. The CLR provides an ext ensive securit y m echanism ( code access securit y) t o det erm ine which users are allowed t o run and execut e m anaged and unm anaged code alike. When looking at securit y, t he designers of gasTI X were prim arily focused on how t o ensure t hat a consum er of t he web service would be aut hent icat ed. The designers of gasTI X set t led upon using a cert ificat e- base aut hent icat ion m echanism over secure HTTP ( HTTPS) . For t his, I I S would m ap t he cert ificat e t o an act ive direct ory user, from which t he applicat ion would query t o get t he part ner's I D ( part icipant , venue, or sponsor) . There would also be a securit y check t o ensure t hat a part ner at t em pt ing t o consum e t he part icipant web service was in t he part icipant group in t he NT dom ain. The m ain problem wit h j ust passing a user nam e and password on t he call t o t he web service is t hat it is possible for a part ner t o im personat e anot her part ner. There is no way t o act ually t ell what part ner is using t he usernam e and password. Alt hough t he usernam e and password is reasonably secure, t he use of a cert ificat e would guarant ee t hat t he part ner was who t he gasTI X web service t hought t hat t he part ner was. Any special processing t hat needed t o occur for a specific part ner ( like a service charge) would be sim ple t o do since t he web service façade would be able t o ident ify t he part ner m aking t he request . For m ore inform at ion on .NET securit y, see Chapt er 8 " Handling Securit y" .

gasTIX Business Services--A closer look Since we have looked at what t hings t he designers of gasTI X t ook int o account when designing gasTI X, it is now t im e t o t ake a m ore det ailed look at t he elem ent s of t he gasTI X business services. The high- level design t hat was developed can be seen in Figure 6.1. The gasTI X business services were divided int o six areas: business façade, business obj ect s, dat a, dat a access, syst em fram ework, and web services. During im plem ent at ion, t hese business services areas were assigned t heir own nam espaces.

Figure 6.1 The gasTI X High- level design.

Business Façade Note: A façade, according to the dictionary, is the principal face of a building or an artificial or deceptive front. Applying this definition, a business façade is the principal interface to the business services layer.

The business façade has t wo separat e and dist inct purposes. First , it is used t o abst ract t he com plexit y of t he business services by providing a sim ple int erface t o t he gasTI X present at ion services client . The present at ion services client is not required t o underst and how t he business services layer works; from t he view of t his client , t he business façade is t he business services layer. Secondly, t he business façade m akes up t he workflow of t he business services. The com ponent s in t he business façade glue t he ot her layers of t he business services t oget her. I f t he order of processing m ust be changed, t he business façade will m ore t han likely be t he place t o change it . The business facade cont rols t he logic t hat occurs in t he business services layer. One it em t hat arose was whet her gasTI X should have a List Event s class t hat had m et hods nam ed List Event sByPart icipant and List Event sByVenue, or have a Part icipant class wit h a List Event s m et hod and a Venue class wit h a List Event s m et hod. We chose t o design Part icipant and Venue class. First , from a purely obj ect - orient ed point of view, a Part icipant and Venue int uit ively fit t he definit ion of a class. Second, it m ade sense t hat since we had Part icipant and Venue classes, t hat each one would know how t o list event s. The classes require a part icipant and venue I D respect ively because obj ect s in t he business façade do not hold st at e bet ween calls from t he present at ion services.

Business The classes in t he gasTI X.Business area of t he gasTI X business services layer are responsible for processing and enforcing business rules in t he gasTI X applicat ion. The present at ion services m ay enforce som e business rules, but it is t he responsibilit y of t he m iddle t ier t o validat e t he dat a. Rem em ber t hat alt hough t he exist ing present at ion layer m ay validat e dat a, it m ay not be able t o validat e all business rules. I t is also possible t hat anot her present at ion layer, which has NO validat ion, could be built t o call int o t he business services layer. One rule t hat is enforced is t hat an order m ay only cont ain up t o eight t icket s. Bot h t he gasTI X consum er sit e and gasBAGS m ay or m ay not enforce t his rule. The OrderRules class in t he Business nam espace does enforce t his rule. The web services façade calls int o t he business façade. This is t he sam e business façade t hat t he gasTI X consum er sit e uses. The business façade in t urn calls int o t he obj ect s in t he business nam espace. This rule is aut om at ically enforced on bot h t he gasTI X and gasBAGS sit es because bot h consum ers end up using t he sam e business logic com ponent s. Furt her, no significant com m unicat ion is required bet ween gasTI X and t he business part nerwit h regards t o what business rules t hat t hey m ust enforce in order t o consum e our service. The obj ect s in t he business nam espace also cont rol t he t ransact ions t hat are enlist ed. All dat a access com ponent s are set t o " support s t ransact ions" . This allows t he act ual dat a access work t o be agnost ic of t he t ransact ional st at e of t he cont ext it is operat ing in. When processing t he business rules, and preparing t o save an obj ect , t he business elem ent s det erm ine under which circum st ances t ransact ions are enlist ed.

Data I t is t he Dat a area of t he gasTI X business services t hat cont ains t he act ual inform at ion ret rieved from t he dat abase. The classes t hat reside in t his area are in t he gasTI X.Dat a nam espace. The dat a obj ect s wit hin are t he cont ainers of t he act ual dat a ret rieved from t he dat abase and sent across t he wire t o t he present at ion services. Typed dat aset s are t hem selves derived from Dat aset . When dat aset s are sent from t he business services layer t o t he present at ion services layer, a copy of t he dat aset is sent . We decided t o use t yped dat aset s t o im plem ent our gasTI X.Dat a classes for several reasons, but t he prim ary reason was t hat we felt it was bet t er t o program against a gasTI X Order obj ect , rat her t han a generic dat aset . Using a t yped dat aset prevent s t he present at ion layer from const ruct ing a generic dat aset on t he fly and at t em pt ing t o pass it int o a funct ion as a param et er where a specific t ype of dat aset is required. Thusly we can m ore t ight ly cont rol t he int erface t o and from t he business services layer. For m ore inform at ion on t yped dat aset s, see t he sect ion, “ Typed Dat aset s,” lat er in t his chapt er.

Data Access The gasTI X dat a access obj ect s are used t o com m unicat e t o t he dat abase. They cont ain t he inform at ion necessary t o send and receive inform at ion from t he dat a st ore. The dat a access obj ect s t hem selves do NOT st art or com plet e t ransact ions, but " support " running in or out of a t ransact ional cont ext . The new .NET fram ework provides aut om at ic enlist m ent of t ransact ions t hrough COM+ services. Due t o t his requirem ent , t hese com ponent s are host ed in COM+ . ADO.NET cont ains obj ect s t hat are used t o connect t o t he dat abase. I n gasTI X we built a Dat a Access obj ect t hat wraps up som e ADO.NET funct ionalit y and abst ract s t he logic required t o connect t o t he dat abase and execut e queries. Note: Many people have attempted to abstract database connectivity in past versions Visual Basic.

One

problem that occurred was that since VB did not support inheritance, developers either had to put all this code in a module or implemented an interface and did what is affectionately known as "cut-an-paste inheritance". Neither solution was a pretty one. Full inheritance in the .NET platform finally makes an elegant solution possible.

The gasTI X Dat a Access obj ect s derive from t he base class gasTI X.Dat aAccess. The gasTI X.Dat aAccess baseclass is abst ract , so it m ust be inherit ed from . I t cont ains t he logic t o ret rieve t he connect ion st ring for t he dat abase from t he COM+ Const ruct St ring. The base class also cont ains all t he logic t o set up a call t o execut e a st ored procedure t o ret rieve dat a, but it is up t o t he derived class t o put in t he specific inform at ion like t he st ored procedure nam e and param et ers. All ot her dat a access obj ect s derive from gasTI X.Dat aAccess and inherit it s funct ionalit y. This is bet t er t han im plem ent ing an I Dat aAccess int erface, because t here is com m on code in all dat a access com ponent s t hat can be abst ract ed out and writ t en only one t im e. One ot her aspect of t he design of dat a access com ponent s t hat is not ewort hy. Past experience has shown t hat m aking m ult iple calls t o a dat a source is expensive, from a perform ance point of view. For t his reason, one should ret rieve large blocks of dat a in a single call rat her t hat in m ult iple calls t o a dat a source. For exam ple, say t hat t he business services needs t o ret rieve t hree dat aset s from a dat a source; it is m ore

efficient t o ret rieve all t hree dat aset s by execut ing one dat abase call, rat her t han in t hree separat e calls. I n order t o updat e dat a it is som et im es necessary t o execut e m ult iple separat e updat e st at em ent s; j ust keep in m ind t hat reducing t he num ber of calls across t he wire, or across processes, will im prove perform ance.

System Framework The syst em fram ework provides for ut ilit y funct ionalit y t hat is used t hroughout t he syst em . Two of t he obj ect s in t he gasTI X.Syst em .Fram ework nam espace are specifically designed t o deal wit h except ion handling. These obj ect s are a cust om except ion, GASExcept ion, and an error list obj ect , GASErrors. The .NET fram ework handles except ions via st ruct ured except ion handling, even across languages. I f an except ion is t hrown across a SOAP call, t he except ion is t ransport ed back t o t he caller as a SOAP fault . The purpose behind im plem ent ing a cust om except ion and error list obj ect s was not due t o a problem wit h t he .NET except ion handling m echanism . But say, for exam ple, t hat t he present at ion services t eam want s t o get a list of business rules t hat failed while t he business services were processing dat a. The except ion m echanism built int o .NET does not work t his way. By using m ult iple t ry- cat ch blocks inside one funct ion, it is possible for t he developer t o build an error list . At t he conclusion of processing, t he developer would t hrow an except ion t hat was t he cont ent s of t he error list . This would send back all t he business rule errors t o t he caller. The caller could cat ch t he errors int o an except ion and t hen perform what ever processing is necessary. For m ore inform at ion on t he cust om except ion class and error list , see Chapt er 14 " Debugging a .NET Applicat ion" .

Web Services As you can see from t he press com ing out of Microsoft , Web Services t echnology is cornerst one of .NET. A web service is a collect ion of code t hat provides services and dat a t o ot her applicat ions using st andard int ernet and web prot ocols like XML and SOAP. The im plem ent at ion of each web service is irrelevant since web services com m unicat e using t hese st andard web prot ocols. Microsoft proposes t aking your exist ing business com ponent s and m aking t hem web services sim ply by adding t he [ WebService] at t ribut e t o t hem . I f we are not careful, we could easily get caught up in t he m arket ing hype and m ake an uninform ed, if not ill- advised, decision. The idea of sim ply t aking business com ponent s and m aking t hem available as a web service inst igat ed t he m ost int erest ing discussions t hat arose during our ent ire design process. The design t eam t hought t hat if our business façade com ponent s would be exposed as web services, why not have t he gasTI X consum er sit es use t he sam e web services int erface as gasBAGS or any ot her t hird- part y consum er of t he gasTI X web service. The prim ary benefit t o having all consum ers access business services t hrough t he web service was t hat all consum ers would have used a single int erface int o t he business services layer. This single int erface would have dem anded a sim pler concept ual design as shown in Figure 6.2. Also, t here was som e t hought t hat t here would be less m aint enance of code in t he fut ure. This concept was abandoned for several reasons.

The gasTI X Proposed design.

AM FL Y

Figure 6.2

TE

First , t he use of web services arbit rarily is not a good approach. Web services com m unicat e using t he SOAP form at t er over HTTP, which is not t he m ost efficient choice for all com m unicat ion. This com m unicat ion m echanism is best for int eroperabilit y, but if int eroperabilit y is not your goal, why use SOAP over HTTP? I n general, one should only expose services as a web service if it is necessary t o consum e t hat service by using a st andard I nt ernet prot ocol. I f we are only building com ponent s t hat will be consum ed wit hin our firewall dom ain, why not use .NET rem ot ing? Second, t he web service façade does not perform t he sam e funct ions as t he business façade. The web service façade exposes only t hose services t hat t he web service is providing for a business part ner of gasTI X. A business part ner could be a part icipant , like gasBAGS, a venue, or a sponsor. The web service façade also execut es specific business rules; for exam ple, charging a fee for usage, or waiving shipping charges, depending upon t he consum er of t he web service. Third, t he web service façade cont rols access t o t he part icular services t hem selves. Consum ers of t he gasTI X web services would fall int o one of t hree cat egories: part icipant , venues, and sponsors. Only part icipant - t ype consum ers will be able t o call t he part icipant web service m et hods. This is also t rue wit h bot h venues- and sponsors- t ype consum ers. This being t he case, t he obvious quest ion is how do we know which consum ers are part icipant s, venues or sponsor t ypes? The answer is wit h digit al cert ificat es. The gasTI X web services design requires t hat a client cert ificat e be passed in on t he web service request . I I S m aps t he client cert ificat e t o a part icular dom ain user in Act ive Direct ory ( AD) . I I S forwards t he call t o t he web service façade along wit h t he aut hent icat ed user. The web service façade verifies t hat t he

Team-Fly®

aut hent icat ed user ( im plem ent ed in t he .NET Fram ework as WindowsPrincipal) of t he call is in t he appropriat e NT group. I f t he WindowsPrincipal ident ified from t he cert ificat e is not in t he group, an except ion is t hrown. I f t he WindowsPrincipal is in t he appropriat e group, t he web service façade queries for t he I D of t he user in t he AD t ree and uses t hat I D on t he call t o t he business façade.

Figure 6.3 The gasTI X sit e uses Client Cert ificat es t o aut hent icat e web service request s.

What's New for Component Development? Ent erprise applicat ion developm ent has changed since t he int roduct ion of .NET. Microsoft 's significant t echnology shift has provided developers wit h som e new choices for im plem ent at ion t hat were not previously available in t he Windows DNA world. Rem em ber t hat t he .NET plat form includes t he .NET servers as well as t he .NET fram ework. The com binat ion of t he t wo provides for som e sim plificat ion in developm ent , where significant am ount s of work were som et im es required before. I m plem ent at ion changes are usually m ore significant for t he VB developer t han for t he Visual C+ + developer. The business services layer of gasTI X was im plem ent ed using C# and Visual Basic .NET. As you m ay ( or at least should) know, t here is no significant difference bet ween t he various Microsoft provided .NET languages. Since all .NET languages adhere t o t he Com m on Language Specificat ion ( CLS) , gasTI X business services could have been writ t en in any .NET language. We chose C# and Visual Basic .NET solely based upon t he skill set s of t he developers involved in t he im plem ent at ion. I n regards t o t he cust om developm ent effort , .NET has changed t he im plem ent at ion of ent erprise syst em s and com ponent s in several ways, and t he t wo t hat we will discuss are rem ot ing and ADO.NET.

Remoting versus DCOM The concept of creat ing obj ect s t hat run in a different process or m achines is it self not a new concept . I n Windows DNA, developers used DCOM ( Dist ribut ed COM) t o do rem ot e calls t o obj ect s. Dist ribut ed COM, was designed t o allow com ponent s t o be invoked bet ween m achines on a LAN. The .NET plat form ext ends t he feat ures t hat DCOM provided. I n a way, .NET rem ot ing could be considered DCOM+ . As useful as DCOM was and is t oday, it suffered from a few problem s t hat can be addressed using .NET rem ot ing. The first problem for DCOM was t hat t he regist rat ion allowed for deploying applicat ions in configurat ions t hat were not conceived of during t he design phase. By alt ering a few regist ry ent ries, it is possible t o convert a norm al locally regist ered COM com ponent int o a rem ot ely creat ed DCOM com ponent . The developer does not need t o do anyt hing t o m ake t his happen in t he Windows DNA world. This sounds very flexible, but t he inherent danger is t hat m odificat ions at runt im e by som eone who does not underst and t he design of t he applicat ion would affect t he perform ance of t he applicat ion. I n t he .NET plat form , code m ust be added int o bot h t he server t hat host s t he obj ect and t he consum er of t he obj ect in order t o enable rem ot ing. Rem ot ing, unlike DCOM, no longer " aut o- m agically" happens. A second problem t hat faced developers using DCOM is t hat t he prot ocol t hat DCOM speaks ( RPC) is not port able t hrough a firewall wit h a st andard set up. This prevent s a person from calling com ponent s over DCOM out side of t heir firewall. DCOM solves t his by opening port s on t he firewall which is a securit y risk and is t herefore not a desirable solut ion. The use of SOAP bypasses t his problem . Rem ot ing allows t he developer t o choose t he prot ocol ( HTTP, TCP or ot her) and t he form at t er ( SOAP, BI NARY) t o t ransport inform at ion. I t is possible, t hen, t o use rem ot ing t o invoke m et hods locat ed behind firewalls using eit her a SOAP or Binary form at t er over HTTP. This leads t o an int erest ing discussion about when one should use SOAP for rem ot ing ( as when using web services) and when one should use rem ot ing. I f you need t o access rem ot e services wit hin your LAN, using .NET rem ot ing m akes a lot of sense ( j ust as using DCOM in Windows DNA would have m ade a lot of sense) . I f you m ust com m unicat e t o t he out side world using a st andard int ernet prot ocol, SOAP is a good choice. Rem ot ing is a m ore flexible rem ot ing engine because it allows developers t o choose eit her a binary, SOAP, or ot her prot ocol, but web services only use SOAP. I t is conceivable t hat your rem ot ed com ponent should be able t o support m ult iple prot ocols. I n t his case, build your com ponent as a web service, and at t he sam e t im e build it t o support rem ot ing. For m ore inform at ion on rem ot ing, see chapt er 13, “ .NET Rem ot ing.”

ADO.NET Wit h t he developm ent of a new plat form com es a new dat a access library nam ed ADO.NET. Make no m ist ake; t his is not j ust an updat e t o ADO 2.x ( "classic ADO" ) . One of t he m ost significant differences bet ween ADO .NET and classic ADO is in t he obj ect m odel. ADO.NET clearly separat es t hose obj ect s t hat ret rieve and updat e dat a from t hose obj ect s t hat represent t he dat a. ADO 2.x com bines bot h of t hese funct ions in t he ADO recordset .

ADO.NET com es wit h t wo dat a providers: t he SQLClient and t he OLEDB.. The SQLClient provider can be found in t he Syst em .Dat a.SQLClient nam espace and is designed t o t alk only wit h SQL Server. The OLEDB provider can be found in Syst em .Dat a.OleDb and is designed t o connect t o any OLEDB dat asource. Note: At the time of this writing, Microsoft has release an ODBC provider for connecting to any ODBC data source. It can be found in the assembly System.Data.Odbc.dll and is in the System.Data.Odbc namespace.

I n ADO 2.x, we can creat e a recordset , pass it a st ored procedure nam e, execut e t he st ored proc, and t he result s would be st ored in t he recordset . The recordset could be eit her connect ed or disconnect ed and t hen sent across t he wire as necessary. I n .NET t he connect ed and disconnect ed obj ect s are separat ed as well. ADO.NET cont ains several new im port ant classes and concept s wit h which a developer m ust be fam iliar. The im port ant classes and concept s t hat we will focus on are as follows: z

DataReader

z

Dataset

z

XML view of Data

z

Typed Datasets

z

DataReader

A Dat aReader ( SQLDat aReader or OleDbDat aReader) is a forward- only, read- only " firehose" cursor wit h an open connect ion t o t he dat a source. This obj ect is sim ilar t o a Recordset in " classic" ADO wit h a cursor t ype of forward- only and a lock t ype of read- only. This obj ect should always be closed when finished, so t he dat asource connect ion t hat it holds open can be ret urned t o t he connect ion pool. An exam ple of using a Dat aReader t o ret rieve dat a out of a dat asource can be found in List ing 6.1. The code in t he list ing is very st raight forward. We open a connect ion and com m and. We creat e a reader and set it equal t o t he Execut eReader m et hod on t he SQLCom m and obj ect . We can now ret rieve t he dat a out of t he reader. List in g 6 .1 Re t r ie vin g D a t a w it h t h e SQLD a t a Re a de r string connectionString = "server=gasTIXSQL;database=gastix;uid=sa;pwd="; string queryString = "select * from credit_card_type"; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(queryString, conn); SqlDataReader reader;

conn.Open(); if(conn.State == ConnectionState.Open) { reader = cmd.ExecuteReader(); while (reader.Read())

{ string outputString = ""; for (int i=0;i < reader.FieldCount; i++) outputString += "reader[" + i + "] = " + reader[i].ToString() + " "; Console.WriteLine(outputString); } reader.Close(); conn.Close(); }

A Dat aReader can be useful for ret rieving read- only dat a, but since it m ust be connect ed t o t he dat a source in order t o access dat a, a Dat aReader is very lim it ed. Since t he connect ion is being held open all t he t im e, a server will have issues wit h scaling as significant ly m ore users com e on line. I f we want ed t o do a query and send t his dat a t o t he present at ion services, sending back a Dat aReader would not be t he way t o best send t he inform at ion. We really need a disconnect ed cont ainer for t he dat a. ADO.NET provides t his funct ionalit y in a Dat aset . Dataset There is no obj ect in "classic" ADO t hat can be m apped direct ly t o an ADO .NET Dat aset . Sim ilar t o a disconnect ed recordset in " classic" ADO, a Dat aset represent s dat a disconnect ed from t he dat a source. But unlike a recordset , a Dat aset consist s of Dat aTables and t he relat ionships bet ween t hose t ables ( Dat aRelat ion) . A Dat aTable represent s a single select st at em ent . This is sim ilar in som e ways t o a " shaped" recordset , but ADO uses one single t abular obj ect t o do t his. A Dat aAdapt er in ADO.NET represent s a set of com m ands and a dat a source connect ion and is used t o fill a Dat aset . Code for ret rieving dat a in a Dat aset can be found in List ing 6.2. Using Dat aSet s and Dat aAdapt ers is pret t y different from t he old ADO coding st yle. I t ’d be wort h insert ing code com m ent s int o t he list ing. List in g 6 .2 Re t r ie vin g D a t a in t o a D a t a se t string connectionString = "server=gasTIXSQL;database=gastix;uid=sa;pwd="; //create a new connection object SqlConnection conn = new SqlConnection(connectionString); //create a new command object SqlCommand cmd = new SqlCommand(); // create a data adapter that will use the command object to get data SqlDataAdapter da = new SqlDataAdapter(cmd); //create a dataset DataSet ds; cmd.Connection = conn; conn.Open(); if(conn.State == ConnectionState.Open) {

ds = new DataSet("categories"); //create a SqlParameter to pass a value into a stored procedure SqlParameter sqlParam;

sqlParam = new SqlParameter("@customer_id", SqlDbType.Int, 4); sqlParam.Value = customerID; sqlParam.Direction = ParameterDirection.Input; cmd.Parameters.Add(sqlParam); // the command type is a stored proc cmd.CommandType = CommandType.StoredProcedure; // this is the stored procedure name cmd.CommandText = "usp_get_categories_by_customer"; // map DataTable "Table" to DataTable "category" in the dataSet da.TableMappings.Add("Table", "category"); // load results into the dataset da.Fill(ds);

conn.Close(); }

The previous code list ing shows ret rieving dat a from a SQL Server dat abase via a st ored procedure int o a Dat aset . We know t hat t he dat asource is a SQL Server dat abase because we are using t he SqlClient nam espace obj ect s. A SqlCom m and and SqlConnect ion are creat ed. The SqlCom m and.Connect ion is set t o point t o t he SqlConnect ion. A SqlDat aAdapt er is creat ed, and connect ed t o t he SqlCom m and. The st ored procedure param et ers are creat ed using t he SqlParam et er obj ect . By default when t he result s are ret rieved, t hey are put in a Dat aTable called " Table" . I f t here are m ult iple t ables, t hen t he schem e is " Table" , " Table1" , ..." Tablen" . We want t he Dat aTable inside t he dat aset t o have t he nam e " cat egory" , not " Table" , so we use a TableMapping t o m ake t his happen. The SqlDat aAdapt er.Fill m et hod loads t he Dat aSet . Aft er t his t he connect ion t o t he dat abase is closed, but t he dat a st ill exist s in t he Dat aSet . XML view of data Anot her new it em in ADO.NET is t he way in which XML has been incorporat ed. ADO.NET dat aset s allow dat a t o be t reat ed as XML. A Dat aset can be loaded from an XML file or XML buffer, and can writ e it s cont ent s out t o W3C com pliant XML. A Dat aset can writ e it s dat a t o XML regardless of where t he source of t he dat a was. This m eans t hat we can load a Dat aset from a dat abase and writ e it s cont ent s out t o XML. A Dat aset can also writ e it s schem a as XML Schem a Definit ion Language ( XSD) . Dat aset s are serialized as XML, which allows business services t o ret urn a Dat aset from a m et hod. The present at ion services can t hen apply a st yle sheet direct ly on t he XML represent at ion of t he dat a in t he Dat aset . The Dat aset provides t he following m et hods: Writ eXMLSchem a, Writ eXML, and ReadXML t o work wit h t he dat a as XML. I t was possible st art ing wit h ADO 2.5 t o save RecordSet s int o XML or load t hem from XML int o a RecordSet . I t is im port ant t o underst and t he significant advant ages t hat ADO .NET provides over previous incarnat ions. First , t he XML form at t hat was saved was predefined. Using t he Dat aRelat ion and Dat aTables, we can cont rol t he form at of t he XML by changing t he node st ruct ure. Using TableMappings, we cont rol how source t ables are m apped int o Dat aTables. By using Colum nMappings, we can cont rol how t he

Dat aColum n is m apped when t he Dat aset is saved as XML. I n effect , we could t ake dat a in from a dat a source, load it int o a Dat aset , and save it as any XML docum ent t hat we want . Typed Datasets ADO.NET support s bot h t yped and unt yped Dat aset s. I n a t yped Dat aset , all t he Dat aTables and Dat aColum ns becom e first class obj ect s in t he m odel. Typed Dat aset s are support ed by I nt ellisense in t he Visual St udio Code Edit or, and are easier t o read and underst and. Typed Dat aset s are built from a schem a. Because t he obj ect m odel of t he Dat aset is known, t he com piler can do t ype checking at runt im e t o ensure t hat invalid values are not assigned. The code generat ed for a t yped Dat aset includes code for event s for when rows change. As discussed earlier, t he gasTI X applicat ion uses t yped dat aset s. All dat aset s ret urned from t he dat a access layer are ret urned as t yped dat aset s. One disadvant age of using a dat aset is t hat t he schem a of t he dat aset is not t yped checked during com pile t im e. This m eans t hat an invalid t able nam e could be referred t o in code and t he error would not be discovered unt il runt im e. However when we use t yped dat aset s, t he com piler can check colum n and t able nam es because t he schem a has been specified. I f we are using a t yped Dat aset , t he code t o access t he cat egory_nam e colum n in t he first row can be writ t en sim ply as: ds.usp_get_participant_detail[0].last_name = "MyLastName";

Rat her t han: ds.Tables["participant_detail"].Rows[0]["last_name"] = "MyLastName";

Building a t yped Dat aset in Visual St udio.NET is very sim ple. The following st eps det ail t he st eps required:

Figure 6.4 The Server Explorer.

z

Add a Data Set Component to the project and name it ParticipantData.xsd

z

Open the Server Explorer and create a Data Connection to the database. See Figure 6.4.

z

From the server explorer, drag the stored procedure "usp_get_participant_detail" into the designer window.

z

This will create a schema that matches the results of the stored procedure. See Figure 6.5.

z

Save changes.

Figure 6.5 The Designer wit h schem a of result s from st ored procedure.

Now code can be writ t en t hat uses t his new t yped Dat aset . This schem a allows t he com piler t o check values at com pile t im e rat her t han at runt im e. We could also have dragged m ult iple obj ect s ( t ables, st ored procedures, or views) int o t he designer and set up a relat ionship bet ween t hem . I f t he com piler has t his inform at ion, it can enforce validat ion checks as well. Note: By showing the hidden items in the project explorer window, we can see the class that was generated by the designer for the typed Dataset.

The t able nam es and colum n nam es t hat are generat ed by Visual St udio .NET can be ugly. I n our case, t he t able nam e in t he t yped dat aset is t he sam e as t he source of t he dat a, i.e. t he view, st ored proc or t able t hat was t he source. The colum ns are t he sam e nam e as t he source colum ns. We can change t he nam e of t he code generat ed colum ns and t ables by m odifying t he underlying XML schem a t hat is generat ed. I n order t o do t his we m ust use what are called annot at ions.

To use an annot at ed schem a, we m ust m ake t he following change in t he XML schem a it self. We m ust insert xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"

int o t he schem a line t hat looks like t his

t o produce a line t hat looks like

Once we have added t he codegen nam espace definit ion t o t he XML schem a, we can use t he codegen at t ribut es as shown in List ing 6.3. The t ypedNam e elem ent is t he nam e t hat will be generat ed. List in g 6 .3 Cr e a t in g a n An n ot a t e d sch e m a



















Also not ice t hat on t he Part icipant _Det ail t able, t here is are bot h t ypedNam e and t ypedPlural at t ribut es. This signifies t hat t he com piler will generat e a t ype for bot h a single row and t he t able. This allows for us t o it erat e t hrough all t he rows in t he t able as a collect ion by using t he following code: foreach (ParticipantData.ParticipantDetail x in p.ParticipantDetails)

Typed dat aset s provide for t ype- safe access t o t he dat a t hat t hey hold, while sim ult aneously allowing us t o have a logical obj ect hierarchy wit h which t o access t he dat a. Since t hey are t ype checked at com pile t im e, som e errors can be elim inat ed before runt im e.

From Here

TE

AM FL Y

I n som e ways, Microsoft 's changes in t he .NET plat form affect t he design of .NET Business Services. There should be no quest ion t hat t hese sam e changes affect t he im plem ent at ion of .NET Business Services. Underst anding changes t o m arshaling, rem ot ing, web services, and ADO.NET will prove fundam ent al t o a developer's abilit y t o build reliable and robust business service com ponent s in t he fut ure.

Team-Fly®

7 Implementing .NET Data Services Dat a services are a large part of any business applicat ion. The fact t hat dat a is one of t he m ost valuable asset s for any organizat ion should com e as no surprise. Wit h t hat fact com es t he need t o select t he dat abase plat form t hat m ost appropriat ely serves t he needs of t he organizat ion. Microsoft com plet ely rewrot e it s SQL Server dat abase engine wit h version 7.0 in order t o m eet t he needs of t he largest business applicat ions. Som e of t he m aj or im provem ent s were opt im ized st orage, ease of m aint enance, and soft ware reliabilit y. SQL Server 2000 builds on t he foundat ion of SQL Server 7.0 and adds m ore long- await ed feat ures such as user- defined funct ions, array support , part it ioned dat a views, and m any ot hers. SQL Server 2000 is also one of t he first Dat abase Managem ent Syst em s ( DBMS) t o provide nat ive XML support . Furt herm ore, dat abase m anagem ent and m aint enance t asks are easier t han ever before. According t o t he lat est benchm arks, Microsoft SQL Server wins hands- down over any ot her DBMS on t he m arket as far as reliabilit y and scalabilit y are concerned. Note: For details on DBMS benchmarking, and to view the latest results created by the Transaction Processing Performance Council, an independent consortium of database industry representatives, visit www.tpc.org.

As m ight be expect ed, SQL Server is m ore expensive t han in t he past . However, t ot al cost of ownership is st ill quit e less t han com parable DBMSs on t he m arket . I n addit ion t o t ransact ional syst em s, t oday's m arket has a st rong dem and for business int elligence soft ware, oft en referred t o as dat a warehousing. SQL Server 2000 provides excellent t ools for dat a warehousing at no addit ional cost . Moreover, t he SQL Server engine is opt im ized for st ar schem as, which are at t he heart of any dat a warehouse. Note: Data Transformation Services (DTS), Analysis Services (AS), and Multi-Dimensional eXpressions (MDX) provide sophisticated ways of building and deploying data warehousing applications in SQL Server 2000. DTS allows us to move and "scrub" data from our relational database to a dimensional database. AS and MDX combine to provide dimensional cube generation, drill-down, and drill-through query functionality. The SQL Server 2000 OLAP tools are industry-leading, and offer powerful functionality that previously necessitated purchasing expensive third-party tools.

SQL Server 2000 offers reusable code m odules such as views, st ored procedures, t riggers, and user- defined funct ions. There are m any advant ages t o using t hese server- side const ruct s, as opposed t o execut ing em bedded SQL st at em ent s from m iddle- t ier and front - end program s. ( The advant ages of each t ype of code m odule will be discussed in t he individual sect ions below) . The bot t om line is t hat Microsoft SQL Server 2000 is t he best choice for building a t ransact ional applicat ion, a decision support syst em , or a fully- fledged dat a warehouse. I n t his chapt er we will present an overview of how dat a services t ie int o t he big pict ure of t he gasTI X sam ple .NET e- Business applicat ion. Alt hough t he basics of SQL Server are beyond t he scope of t his chapt er, we will int roduce som e of t he new SQL 2000 feat ures t hat gasTI X ut ilizes. We will also cover exam ples from t he gasTI X dat a services, and will explore how we can ut ilize SQL Server 2000 t o build our own applicat ions.

Data Modeling Prior t o building t he physical dat abase where t he dat a will act ually be st ored, a logical dat a m odel should be const ruct ed. A logical dat a m odel consist s of relat ed business ent it ies, and m aps ent it y at t ribut es t o t he dat a elem ent s t hat will be st ored in t he dat abase. There are m any reasons for building a logical dat a m odel. First , it helps developers underst and t he business needs and requirem ent s. Modeling t he applicat ion dat a logically is t he first st ep in m oving from abst ract business requirem ent s list ed in a requirem ent s docum ent t o a represent at ion of all t he it em s t o be t racked, and how t hey int errelat e. Second, it provides an opport unit y t o t hink about how t o st ore t he dat a in t he form at m ost appropriat e for t he t ype of applicat ion we are building. These form at s vary widely, and picking t he best form at for t he j ob is crucial t o building a successful applicat ion. The next sect ion explores t he different form at s in which dat a can be st ored.

Relational versus Dimensional Models All dat abase- relat ed applicat ions fit int o one of t hree general m odels: on- line t ransact ion processing ( OLTP) , decision support syst em s ( DSS) , or on- line analyt ical processing ( OLAP) . OLAP is oft en referred t o as dat a warehousing. OLTP applicat ions are specifically designed t o collect dat a: t he gasTI X sam ple applicat ion t his book describes sells t icket s over t he web. I t is a perfect exam ple of a high- volum e OLTP syst em . The m ain purpose of t he syst em is t o provide inform at ion about available event s and t o t rack t icket s purchased for t hose event s. The logical m odel for an OLTP dat abase will usually be highly norm alized, as is t he gasTI X logical m odel. Note: When designing OLTP systems it is important to store data in the most efficient manner possible by following the standard rules of normalization. First Normal Form states that repeating groups in tables should be eliminated, and that a primary key should be identified for each table. Second Normal Form states that separate tables should be created for sets of values that apply to multiple records, and that these tables should be related with a foreign key. Third Normal Form states that fields that do not depend on the key should be eliminated. Other rules exist, but are rarely used.

OLTP syst em s perform well for dat a- ent ry purposes, but usually fail t o perform well when used for ext ensive report ing. The reason is t hat report s t ypically need t o gat her dat a from a large num ber of t ables, and j oining m any t ables usually slows down t he report generat ion process. DSS applicat ions, on t he ot her hand, are specifically opt im ized for report ing. When we build DSS applicat ions, we add a level of redundancy t o our dat a by de- norm alizing t he appropriat e t ables. For exam ple, we m ay decide t o copy t he cust om er nam e colum n int o a purchase hist ory t able so t hat we don't have t o j oin t o t he cust om er t able for a purchase hist ory report . Conversely, DSS applicat ions do not perform as well as OLTP applicat ions for dat a gat hering purposes. I n an OLTP syst em , we would not want t o duplicat e t he cust om er nam e colum n in t he purchase hist ory t able, because every t im e a cust om er m akes a purchase, we would need t o writ e ext ra dat a ( cust om er nam e) int o t hat t able. Tip: Database modeling has often been called more of an art than a science. Only through extensive experience do we gain the ability to know when to de-normalize appropriately, or to know to what level of normalization we should implement a design. More often than not, we find ourselves making trade-offs during the modeling

process. Success comes through staying focused on the business requirements, and through soliciting feedback from others whose opinions we value.

OLAP applicat ions load dat a from m ult iple t ransact ional syst em s int o a single st orage area. The dat a m ay be cleaned up in t his st aging area, and cust om calculat ions can be perform ed. When t he dat a t ransform at ion process has been com plet ed, it is loaded int o t he OLAP warehouse, and from t his dat abase users can build easily cust om izable report s. OLAP applicat ions let t he business decision- m akers " slice and dice" t he dat a along various dim ensions. Such syst em s represent a dim ensional m odel, as opposed t o t he relat ional m odel used for OLTP and DSS applicat ions. The dim ensional m odel consist s of a large fact t able ( or m ult iple fact t ables) and m any sm all dim ension t ables. The fact t able cont ains foreign keys t o each of t he dim ension t ables, and holds t he m easures t hat users would like t o see on t heir report s. For inst ance, gasTI X m anagem ent m ight like t o see t he cit ies and st at es wit h t he highest volum e of purchases for Backst reet Boys concert s. The dim ensions chosen would include t he m usic group ( perform er) and t he cust om er. The m easures would be t he num ber of t icket s sold, or t he revenue generat ed. One of t he com m on m ist akes m ade when m odeling dat a is m ixing t he relat ional and t he dim ensional approaches, or using t he sam e applicat ion for bot h dat a ent ry and report ing. Since t he gasTI X business will require dim ensional OLAP report ing at a lat er dat e, we will event ually build DSS or OLAP funct ionalit y int o t he gasTI X dat a services ( alt hough t his is not included in t his version) . Keeping t his in m ind, we have purposefully designed an OLTP ( relat ional) applicat ion for phase one of gasTI X, rem em bering t he need for a fut ure version of t he dat abase opt im ized for report ing. At som e lat er t im e we m ay decide t o design a dim ensional m odel t hat will int erface wit h t he exist ing relat ional m odel. By no m eans, however, will we at t em pt t o im plem ent DSS/ OLAP funct ionalit y solely wit hin t he relat ional m odel.

Physical Modeling Perhaps one of t he m ost frequent ly asked ( and debat ed) quest ions is t he difference bet ween physical and logical dat a m odeling. Logical m odeling is done wit hout any considerat ion of t he DBMS t hat will be used t o physically im plem ent t he m odel. Whet her we build our dat abase in Microsoft Access, Oracle, I nform ix, Sybase, or Microsoft SQL Server, t he logical dat a m odel should be t he sam e. The physical m odel, on t he ot her hand, is specific t o t he DBMS being used. I t is in t he physical m odel t hat we define dat a t ypes and referent ial int egrit y const raint s such as prim ary keys, foreign keys, and t riggers. Any ot her dat abase obj ect s t hat will part icipat e in t he m odel are also defined, including indexes and views. Dat a t ypes and dat abase obj ect s can vary widely from one DBMS t o t he next . Therefore, t he physical m odel built for SQL Server m ight be quit e different from t he m odel built for I nform ix, alt hough t hey are based on t he sam e logical m odel. I n t he following sect ions, we will exam ine each com ponent of physical dat a m odeling. Choosing Appropriate Data Types Physical dat abase design involves choosing t he proper dat a t ype for each elem ent t o be st ored in t he dat abase. Dat a t ype select ion should be prim arily driven by t he st at ed business requirem ent s. However, we m ust select from t he m any dat a t ypes provided wit h SQL Server. For inst ance, num eric dat a can be

st ored in t he following dat a t ypes: BI T, TI NYI NT, SMALLI NT, I NT, BI GI NT, REAL, DOUBLE, NUMERI C, FLOAT, MONEY, SMALLMONEY, or even CHAR and VARCHAR. We should select t he dat a t ype t hat provides t he necessary range of values, which at t he sam e t im e uses t he sm allest num ber of byt es of st orage. For inst ance, if we are st oring a person's age, we can use t he BI GI NT dat a t ype, but we will be bet t er off using TI NYI NT ( unless we expect people in our dat abase t o exceed t he age of 255) ! The difference in st orage space bet ween t hese t wo t ypes is seven byt es for every row, which will add up quickly if we gat her m illions of rows. " Narrow" dat a t ypes creat e narrow t ables and narrow indexes, which are easier t o scan for queries. Varying lengt h dat a t ypes such as VARCHAR and VARBI NARY can also add overhead t o a t able and can t herefore slow down t ransact ions. I f we are st oring a social securit y num ber or a phone num ber in a colum n which always has t he sam e lengt h, it m akes sense t o use t he CHAR dat a t ype inst ead of VARCHAR. On t he ot her hand, if t he average lengt h of t he colum n is m uch short er t han t he m axim um allowed lengt h, variable lengt h dat a t ypes will act ually perform bet t er t han fixed lengt h dat a t ypes. Anot her im port ant decision is whet her each colum n in a t able should allow nulls. Nullable colum ns add a slight overhead t o a t able, and are som ewhat slower t han t he colum ns t hat do not allow nulls. However, nullable colum ns save a lot of space for t he t ables where only a few rows cont ain values. Tip: In general, it is not a good idea to allow nulls in columns "just in case". This can hide exceptions that would otherwise be raised when data that should be provided for a business requirement is missing in an INSERT statement.

When adding a colum n t o a t able wit h exist ing dat a, we have t o eit her allow nulls or specify a default value. Aft er we populat e all exist ing rows wit h non- null values, however, we can change t he t able definit ion t o disallow nulls for t hat colum n. Earlier versions of SQL Server required us t o export t he dat a, drop t he t able, re- creat e t he t able, and im port t he dat a. Wit h SQL Server 2000, we can sim ply change t he t able definit ion, which is a welcom e pr oduct ivit y enhancem ent . New Data Types in SQL Server 2000 Am ong t he m any welcom e addit ions t o a SQL Server program m er's arsenal of t ools are several new dat a t ypes, shown in Table 7.1. Ta ble 7 .1 Th e n e w da t a t ype s in SQL Se r ve r 2 0 0 0

New Data Type

Description

BIGINT

Exact numeric data type that can handle values larger than INT.

CURSOR

Contains a reference to an existing cursor. Can only be used for parameters and variables.

SQL_VARIANT

Provides a way to store values without knowing the data type. Similar to the

VARIANT data type found in other languages such as Visual Basic 6.0. TABLE

Contains up to 1024 columns. Similar to temporary tables, except TABLE variables are cleaned up automatically. Can only be used for variables and parameters. Provides an array-like structure.

The first new dat a t ype we will discuss is BI GI NT. This dat a t ype allows for very large num eric values bet ween + 9,223,372,036,854,775,807 and - 9,223,372,036,854,775,807. I t consum es eight byt es of st orage. Obviously, t he m aj orit y of our applicat ions can get by j ust fine wit h t he I NT dat a t ype and will never need t o use BI GI NT. However, for t hose folks t hat have st ret ched SQL Server's lim it s in t he past due t o t he need t o st ore huge dat a values, BI GI NT is t he answer. SQL Server 2000 also adds support for CURSOR dat a t ypes. Variables wit h t he CURSOR dat a t ype can st ore a reference t o a SQL cursor, which is a Transact SQL program m at ic const ruct t hat allows us t o loop t hrough a recordset row by row. The difference bet ween t he CURSOR and TABLE dat a t ypes is t hat a CURSOR allows scrolling t hrough t he dat a set one record at a t im e, while a TABLE does not . The CURSOR dat a t ype can be used t o pass an array from one stored procedure t o anot her, but cannot be used for creat ing t able colum ns. The exam ple shown in List ing 7.1 uses an out put param et er wit h a CURSOR dat a t ype. List in g 7 .1 A SQL Se r ve r 2 0 0 0 st or e d pr oce dur e ou t pu t pa r a m e t e r ca n be de fin e d a n d ca lle d w it h t h e n e w CURSOR da t a t ype . CREATE PROC return_customers (@letter CHAR(1), @cust_cursor CURSOR VARYING OUTPUT) AS SET @cust_cursor = CURSOR FOR SELECT email_address FROM customer WHERE email_address LIKE @letter + '%' OPEN @cust_cursor

-- execute the procedure with a CURSOR OUTPUT parameter: DECLARE @my_cursor CURSOR

EXEC return_customers a, @my_cursor OUTPUT FETCH NEXT FROM @my_cursor WHILE @@FETCH_STATUS = 0 BEGIN FETCH @my_cursor END CLOSE @my_cursor DEALLOCATE @my_cursor

Anot her new dat a t ype in SQL Server 2000 is SQL_VARI ANT. This dat a t ype is sim ilar t o t he VARI ANT dat a t ype found in ot her program m ing languages. I t can be used for st oring dat a of any t ype, except TI MESTAMP, SQL_VARI ANT, I MAGE, TEXT, and NTEXT. I t t akes eight byt es of st orage, and can be used for t able colum ns, variables, param et ers, or ret urn values of user- defined funct ions. The advant age of SQL_VARI ANT is t hat it can be used when we are unsure about t he nat ure of t he dat a t o be st ored in a param et er or colum n. Also available in SQL Server 2000 is t he new TABLE dat a t ype. This dat a t ype offers t he long- await ed feat ure of array support . We can declare a variable wit h a TABLE dat a t ype and st ore m ult i- dim ensional arrays in it . As t he nam e st at es, a TABLE variable behaves m uch like a t em porary t able. The advant age of using a TABLE dat a t ype is t hat it is cleaned up aut om at ically when it goes out of scope; t here is no need t o issue a DROP TABLE st at em ent . For exam ple, t he code in List ing 7.2 populat es a variable wit h m ult iple colum ns of dat a. List in g 7 .2 M u lt iple colu m n s of da t a ca n be st or e d in a TABLE va r ia ble in SQL Se r ve r 2 0 0 0 . DECLARE @customer_names TABLE (last_name VARCHAR(30), first_name VARCHAR(30), email_address VARCHAR(50))

-- populate the table variable INSERT @customer_names SELECT last_name, first_name, email_address FROM customer

-- read the values from the table variable SELECT * FROM @customer_names

Keep in m ind t hat t he TABLE dat a t ype cannot be used when specifying colum n dat a t ypes; it can only be used wit h variables. Adding Referential Integrity Constraints Referent ial I nt egrit y ( RI ) const raint s help keep t he dat abase in a consist ent st at e. RI let s t he dat abase adm inist rat or or developer enforce business rules at t he dat abase level. I t is t rue t hat t he m aj orit y of business rules should be enforced in m iddle- t ier com ponent s. However, an addit ional level of int egrit y does not hurt . Cert ain business rules are also easier t o creat e and can be m ore efficient ly enforced in t he dat abase. For exam ple, we could enforce a rule t hat a cust om er record m ust exist in t he cust om er t able before she can place orders. Sim ilarly, we can only delet e a cust om er if all orders for t hat cust om er have been canceled. These rules are quit e easy t o enforce at t he dat abase level, while in a m iddle- t ier com ponent t hey are m ore com plex t o im plem ent . SQL Server 2000 provides a way t o cascade referent ial int egrit y const raint s when UPDATE and DELETE st at em ent s are execut ed. What t his m eans is t hat if a foreign key colum n is updat ed in t he prim ary t able, t he colum n in t he relat ed t able can also be updat ed. We also have t he opt ion t o supress such UPDATES and report an error. I n previous versions of SQL Server, we had t o writ e a t rigger t o creat e t his funct ionalit y . Let 's look at a quick exam ple of a cascading UPDATE st at em ent . I n t he gasTI X dat abase we have a

Part icipant t able, as well as an Event _Part icipant t able ( see t he Appendix for a full depict ion of t he gasTI X dat abase schem a) . List ing 7.3 shows t he first few rows of t he Event _Part icipant t abl e. List in g 7 .3 Th e fir st fe w r ow s of t h e Eve n t _ Pa r t icipa n t t a ble a r e se le ct e d. SELECT * FROM Event_Participant

Results: Event_id 1

10

1

11

2

15

participant_id



Suppose we add t he following foreign key const raint t o t he Event _Part icipant t able, as shown in List ing 7.4. List in g 7 .4 A for e ign k e y con st r a in t is a dde d t o t h e Eve n t _ Pa r t icipa n t t a ble . ALTER TABLE [dbo].[Event_Participant] ADD CONSTRAINT [FK_event_participant_participant] FOREIGN KEY ([participant_id]) REFERENCES [dbo].[participant] ([participant_id]) ON UPDATE CASCADE

Now, if we change t he part icipant _id colum n in t he Part icipant t able, t he part icipant _id colum n in t he Event _Part icipant t able will also be updat ed accordingly. This is shown in List ing 7.5. List in g 7 .5 Th e pa r t icipa n t _ id colu m n is ch a n ge d in t h e Pa r t icipa n t t a ble . UPDATE Participant SET participant_id = 151 WHERE participant_id = 15 GO SELECT * FROM Event_Participant

Results: Event_id



1

10

1

11

2

151

participant_id

Alt ernat ively, we could choose t o report an error if anyone at t em pt s t o change part icipant _id, as shown in List ing 7.6. List in g 7 .6 A for e ign k e y con st r a in t is a dde d spe cifyin g t h a t a n e r r or is t o be r a ise d if viola t e d. ALTER TABLE [dbo].[Event_Participant] DROP CONSTRAINT FK_event_participant_participant GO ALTER TABLE [dbo].[Event_Participant] ADD CONSTRAINT [FK_event_participant_participant] FOREIGN KEY ([participant_id]) REFERENCES [dbo].[participant] ([participant_id]) ON UPDATE NO ACTION

Now, in List ing 7.7, when we t ry t o change t he part icipant _id colum n back t o t he way it was originally, we receive an error. The error result we receive let s us know t hat t his change is not accept able. List ing 7.7 An e r r or is r e t u r n e d w h e n w e t r y t o ch a n ge t h e pa r t icipa n t _ id colu m n ba ck t o it s or igin a l st a t e . UPDATE Participant SET participant_id = 15 WHERE participant_id = 151

Server: Msg 547, Level 16, State 1, Line 1 UPDATE statement conflicted with COLUMN REFERENCE constraint 'FK_event_participant_participant'. The conflict occurred in database 'gastix', table 'Event_Participant', column 'participant_id'. The statement has been terminated.

We could configure sim ilar act ions for t he DELETE st at em ent . The general synt ax is as follows: ON DELETE { CASCADE | NO ACTION }

Indexing for Performance Building and m aint aining appropriat e indexes on dat abase t ables is a necessit y for m aking sure t hat an applicat ion is opt im ized well. I f our indexes are not effect ive, or if index st at ist ics are out of dat e, chances are t hat our queries will not perform t o t he best of t heir abilit y. That is why adding good indexes can m ake a world of difference in an applicat ion's execut ion speed. The m ore indexes we have and t he bigger t hey are, t he fast er our queries, right ? Wrong! Choosing t he proper indexes is not t hat sim ple, ot herwise we could build indexes on every colum n of every t able and

rewards would be quickly fort hcom ing. Act ually, our choice of t he colum ns on which t o place an index depends on m any fact ors. Som e of t hese fact ors are exam ined below.

Number of Indexes Each index we define will likely im prove perform ance of a SELECT query. However, we need t o keep in m ind t hat t he sam e index will slight ly degrade t he perform ance of any relat ed I NSERT, UPDATE, and DELETE st at em ent . The reason for t his is t hat SQL Server aut om at ically m aint ains t he index keys. Therefore, each t im e we issue a Dat a Modificat ion Language ( DML) st at em ent such as I NSERT, UPDATE, or DELETE, not only does SQL Server have t o m ake changes t o t he dat a, but it also has t o change each index defined on t he affect ed t able. Perform ance degradat ion is especially not iceable wit h large t ables cont aining m any indexes, or even a few indexes wit h long keys. SQL Server 2000 has com e a long way from previous releases when considering index m aint enance. However, in som e cases, it st ill m akes sense t o drop t he indexes, issue t he dat a m odificat ion st at em ent , and t hen re- creat e t he indexes. This is especially t rue when working wit h large set s of dat a. For inst ance, suppose we have a dat a warehouse wit h a fact t able cont aining several m illion rows. Approxim at ely one m illion rows are added each t im e we load dat a. To speed up t he queries, we have defined several indexes on t he fact t able, including t he clust ered index ( covered in t he next sect ion) which spans all of t he dim ension keys. The populat ion of t he fact t able will be m uch fast er if we drop t he indexes, load t he dat a, and rebuild t he indexes.

Clustered versus Non-Clustered Indexes Anot her decision t o m ake is whet her we need a clust ered or a non- clust ered index on a part icular colum n or set of colum ns. This is where it helps t o have an underst anding of t he underlying SQL Server index archit ect ure. SQL Server indexes have a B- t ree st ruct ure wit h a root node, int erm ediat e levels, and leaf nodes. For non- clust ered indexes t he leaf nodes are t he keys of t he clust ered index. For clust ered indexes t he leaf nodes represent t he dat a it self. This brings up t wo im port ant point s: Table data is sorted according to the order of the clustered index. z

We can only have ONE clust ered index on each t able. This m akes sense if we refer t o t he previous point ; we cannot expect SQL Server t o st ore t he dat a sort ed in t wo different ways.

To illust rat e t hese point s, let us look at t he Event t able in t he gasTI X dat abase. By default t his t able has t he clust ered index event _ind defined on t he event _id colum n. This m eans t hat dat a in t his t able will be ordered according t o t he event _id. Creat ing a clust ered index on a sequent ially increasing colum n is a bad idea, as discussed short ly, so we will drop t he current clust ered index and replace it , as shown in List ing 7.8.

List in g 7 .8 Cr e a t in g a be t t e r clu st e r e d in de x on t h e Eve n t t a ble in ga sTI X .

DROP INDEX event.event_ind GO CREATE CLUSTERED INDEX event_date_and_name ON event(event_date, event_name)

Now we can exam ine how t he dat a is physically st ored in t he t able by running a sim ple SELECT st at em ent on t he Event t able ( shown in List ing 7.9) . We will force SQL Server t o use t he clust ered index wit h a query hint since we do not wish t o bring t he ent ire t able back, j ust t he dat e and t he nam e of t he event . List in g 7 .9 Ex a m in in g h ow da t a in t h e Eve n t t a ble is r e t u r n e d u sin g t h e n e w ly cr e a t e d clu st e r e d in de x .

SELECT event _dat e, event _nam e FROM event ( I NDEX = 1) event_date event_name 5/6/01 12:00 AM

Decatur Houseflies vs. Bentonville Polar Bears

5/6/01 12:00 AM

Hayward Rats vs. Decatur Houseflies

5/7/01 12:00 AM

Warwick Houseflies vs. Savannah Houseflies

5/11/01 12:00 AM

Reno Tigers vs. New London Rats

AM FL Y

We have creat ed a clust ered index on t he com binat ion of t he event dat e and t he event nam e. This will provide t he gasTI X dat abase wit h t he best perform ance for t he m aj orit y of it s queries, since t hese are t he t wo colum ns m ost frequent ly searched. I t is im port ant t o not e t hat we have agreed t o t he t rade- off of decreased perform ance on I NSERT and DELETE st at em ent s on t he Event t able for im proved SELECT perform ance. This decision fit s our business requirem ent which st at es t hat searches will be perform ed m uch m ore frequent ly t han dat a insert s or delet ions. Tip: Trade-offs between better SELECT statement performance versus better INSERT, UPDATE, and DELETE

TE

performance must often be made when implementing a database design. As shown in the previous gasTIX example, the decision should be made based on the relevant business requirements.

A com m on m ispercept ion is t hat SQL Server st ores dat a according t o t he order t hat records are ent ered int o a t able. Cont rary t o t his belief, SQL Server act ually st ores dat a according t o t he way we creat e t he clust ered index. A clust ered index will be m uch fast er for dat a ret rieval operat ions t han a non- clust ered index because t he dat a is ordered according t o t he clust ered index definit ion. What t his m eans t o us as developers is t hat t he clust ered index should be placed only on t he m ost im port ant colum n ( or colum ns) in each t able. SQL Server allows up t o 249 non- clust ered indexes per t able. Therefore, if we do need an index on ot her colum ns, we can always creat e a non- clust ered index. Note: If we have a column marked as an identity column, we should avoid putting a clustered index on that column. Identity columns use an automatically-incremented value generated by SQL Server. For instance, the gasTIX database has the identity property set on the event_id column in the Event table. The first event inserted in this table will have an event_id = 1, the second will have event_id = 2, etc. It is not likely that our users will ever run queries affecting the identity column since it has no business meaning, and normally users will never see this value.

Team-Fly®

Tip: It is very important to have a clustered index on every table. If a table does not have a clustered index, all new records will be added to the last data page occupied by the table. This can cause inefficiencies because queries are rarely executed based on the order in which data was originally inserted. If there is a clustered index on a table, new rows can be added to the last page, or to the middle of the table, whichever position is suitable for the new row according to the way data is sorted in the table (according to the way clustered index is defined).

I n general, clust ered indexes are good for queries t hat : z

contain a large number of distinct values.

z

have a WHERE clause that returns values based on BETWEEN, or > or < conditions.

z

read columns accessed sequentially. That is, if 'lastname' and 'firstname' columns are accessed often in queries, we should create an index on columns in t h a t or de r .

z

have a GROUP BY and/or ORDER BY clause that accesses the first few or all columns comprising the clustered index.

I n general, we should avoid placing a clust ered index on a frequent ly changing colum n. Since SQL Server has t o sort t he dat a according t o t he clust ered index definit ion, frequent ly changing clust ered key values will result in ext ra work for t he dat abase engine, and will degrade t he perform ance of UPDATE and I NSERT operat ions. We should also avoid having long keys for clust ered indexes, unless our queries refer t o t he ent ire com binat ion of all colum ns in t he clust ered index frequent ly. The reason t o avoid long keys is t hat non- clust ered indexes on t he sam e t able will use t he clust ered keys for lookups, and will t herefore grow very large. Non- clust ered indexes are good for ret rieving sm aller num bers of rows, and for queries t hat have exact m at ch ( = ) condit ions in t he WHERE clause. Generally speaking, we can use non- clust ered indexes any t im e we need t o speed up t he query on a part icular colum n if t he clust ered index is already being used by anot her colum n. Caution: Keep in mind that each time we rebuild a clustered index (whether we move it to a different column or not) all non-clustered indexes for the table are rebuilt as well. This is because non-clustered indexes use the clustered index values as their lookup keys. When we move the clustered index to a different column, the whole table is copied to temporary storage, ordered according to the clustered index definition, and recreated. Therefore, dropping and re-creating clustered indexes is a resource-intensive and time-consuming operation which should be performed during periods of limited user activity.

Long or Short Index Keys? When im plem ent ing indexes for a dat abase, we m ust decide how m any colum ns, and which specific colum ns, t o include in our indexes. Most of t he t im e our queries t hem selves det erm ine whet her or not we need t o include a part icular colum n in an index. However, we have t he opt ion t o creat e m ore indexes wit h fewer colum ns ( or index keys) . The short er an index is, t he m ore index keys fit on a single page, and t he

less space t aken up by t he index. This m eans t hat scanning index pages t akes less t im e, and overall t he index is m ore efficient . On t he ot her hand, som e queries t hat cont ain search crit eria defined on several colum ns could benefit from a long index t hat includes all colum ns m ent ioned in t he WHERE clause. Such an index is referred t o as a " covering index." Not e t hat if t he index key is t oo long, SQL Server m ight act ually st ore m ore dat a t han index keys on a single page. The covering index keys should fit on a single page in order t o be beneficial. I f t he index is narrower t han t he act ual t able, m ore index keys can be placed on a single page, and t herefore t he ent ire index will be easier t o scan. For exam ple, suppose we have a report t hat ret rieves event dat e, event nam e, t our id associat ed wit h t he event , and dat e t hat t icket s go on sale. We could creat e a covering non- clust ered index on t he Event t able t o opt im ize t he query for t his report as follows: CREATE NONCLUSTERED INDEX report_index ON

Event(event_date, event_name, tour_id, ticket_sell_start_date)

Index Tuning Wizard SQL Server 2000 includes a useful t ool called t he I ndex Tuning Wizard. This t ool analyzes t he current indexes on a dat abase, based on a workload file generat ed wit h SQL Profiler, a select ion of queries run wit hin Query Analyzer, or from a t race t able ( which can also be generat ed wit h SQL Profiler) . I f t he I ndex Tuning Wizard finds t hat queries can be opt im ized wit h new indexes, it provides t he opt ion t o creat e t he new indexes at t he click of a but t on. The I ndex Tuning Wizard is invoked by select ing Tools| I ndex Tuning Wizard from t he m enu bar wit hin SQL Profiler, or by select ing t he Query| I ndex Tuning Wizard m enu opt ion wit hin SQL Query Analyzer. For det ailed inform at ion on using t his helpful t ool, refer t o Chapt er 15, " Perform ance Tuning in .NET."

Transact-SQL Transact SQL is a SQL Server- specific flavor of t he St ruct ured Query Language ( SQL) , t he program m ing language support ed by all m aj or dat abase engines. Transact SQL ( T- SQL for short ) support s m ost st andard SQL feat ures which are updat ed every few years by t he Am erican Nat ional St andards I nst it ut e ( ANSI ) . Note: To learn more about the ANSI standards body, visit their web site at www.ansi.org. In addition to helping define the ANSI SQL standard, this standards body is involved with defining consistent standards for a wide variety of industries. The National Resource for Global Standards (NSSN) offers a web site at www.nssn.org where you can search for a variety of these standards, and view the details behind them.

Transact SQL has powerful ext ensions t hat let us program condit ional st at em ent s, execut e server- side cursors, m anipulat e bat ches, and work wit h reusable code m odules such as user- defined funct ions and st ored procedures. I n t his sect ion we will offer a brief overview of t he differences bet ween ANSI SQL and Transact SQL wit h an em phasis on t he new feat ures support ed in SQL Server 2000. Since t his is not a book dedicat ed t o Transact - SQL, we will only focus on t he m aj or feat ures and enhancem ent s. The key point t o rem em ber is t hat im plem ent ing .NET dat a services requires sm art use of Transact SQL in various places in SQL Server 2000.

Variables in Transact SQL Variables are user- defined or syst em - defined obj ect s t hat hold cert ain values wit hin a Transact SQL program for a period of t im e. There are t wo t ypes of variables in T- SQL: local and global. We will explore bot h variable t ypes in t he following sect ions. Local Variables Local variables are creat ed by developers using t he DECLARE st at em ent . I t is a good idea t o creat e all local variables at t he beginning of t he T- SQL program ( or st ored procedure) t o m ake m aint enance easier. Variable nam es m ust conform t o t he SQL rules for ident ifier nam es, and in addit ion, t hey m ust st art wit h t he " @" sym bol. They cannot be TEXT, NTEXT, or I MAGE dat a t ypes. As m ent ioned earlier, SQL Server 2000 support s t he new TABLE dat a t ype, which can cont ain up t o 1024 colum ns. Due t o t his fact , t he variables wit h dat a t ypes ot her t han TABLE are oft en referred t o as scalar variables. The synt ax for creat ing local scalar variables includes sim ply st at ing t he variable nam e and t he dat a t ype, as follows: DECLARE @local_variable data_type

Mult iple local variables can be creat ed wit h a single DECLARE st at em ent . For exam ple, t he following code creat es t hree local variables: DECLARE @number_of_rows INT, @event_name VARCHAR(20), @event_date SMALLDATETIME

There are m ult iple ways of assigning values t o local variables. One way is t o use a SELECT st at em ent t o assign a specified value t o t he variable, as shown in t he following line ( not e t hat all of t he following exam ples assum e t hat t he @num ber_of_rows variable has been declared as I NT) : SELECT @number_of_rows = 10

Anot her way is t o assign a value ret rieved by a SELECT st at em ent t o t he variable, as follows: SELECT @number_of_rows = (SELECT COUNT(row_id)

FROM venue_section_row WHERE section_id = 2)

A t hird way is t o use t he SET st at em ent t o assign a specified value ( or value ret urned from a sub- query) t o t he variable: SET @number_of_rows = (SELECT COUNT(row_id)

FROM venue_section_row WHERE section_id = 2)

A fourt h way is t o use a variable as a colum n alias inside a SELECT st at em ent : SELECT @number_of_rows = row_id

FROM venue_section_row WHERE section_id = 2

Caution: If a SELECT statement, such as the statement shown in the last example, returns multiple values, then the local variable will be assigned the la st value returned. While not causing a run-time error, this can produce unexpected results if not accounted for.

Scalar local variables can be used for const ant values, but cannot be direct ly referenced as dat abase obj ect s, such as t ables or views. For inst ance, t he code in List ing 7.10 generat es an error. List in g 7 .1 0 Re fe r e n cin g a sca la r loca l va r ia ble a s a da t a ba se obj e ct ca u se s a n e r r or . DECLARE @table VARCHAR(32) SELECT @table = 'region' SELECT * FROM @table Results: Server: Msg 137, Level 15, State 2, Line 4 Must declare the variable '@table'.

The reason t his error is raised is t hat variable values are resolved at com pile t im e. A workaround is t o use t he EXECUTE st at em ent ( or t he sp_execut esql syst em st ored procedure) , which is resolved at run t im e rat her t han at com pile t im e. The st at em ent in Listing 7.11 will execut e because t he EXEC st at em ent is included. I t ret urns all records from t he Region t able, which is what we were t rying t o accom plish in t he first exam ple. List in g 7 .1 1 Usin g t h e EX EC st a t e m e n t a llow s u s t o r e fe r e n ce a sca la r loca l va r ia ble a s a da t a ba se obj e ct . DECLARE @table VARCHAR(32), @string VARCHAR(100) SELECT @table = 'region' SELECT @string = 'SELECT * FROM ' + @table EXEC(@string)

Global Variables Global variables are pre- defined syst em values. Their values cannot be changed by t he program m er. Global variables used by any session have t he sam e value at any part icular point in t im e. For inst ance, t he global variable @@error always cont ains t he num ber of t he last error t hat was encount ered during program execut ion. Global variables are helpful when program m ing in Transact SQL, because t hey provide syst em - wide inform at ion t hat we oft en need t o inspect t o det erm ine an out com e.

Parameters in Transact SQL

Param et ers in T- SQL are special kinds of variables used wit hin st ored procedures, t riggers, and user- defined funct ions. Just like ot her local variables, param et ers m ust st art wit h t he "@" sym bol, and need t o be of a part icular dat a t ype. Unlike local variables, param et ers exist only for t he durat ion of a procedure or funct ion, and t hey can have a default value. General synt ax for declaring param et ers is as follows: CREATE OR ALTER PROCEDURE proc_name

[ { @parameter data_type } [ VARYING ] [ = default ] [ OUTPUT ] ] [ ,...n ]

The list of param et ers can opt ionally be enclosed in parent heses. The VARYI NG keyword applies t o out put cursor param et ers and sim ply inform s SQL Server t hat t he result set is const ruct ed dynam ically. Param et ers for bot h st ored procedures and funct ions can have default s assigned t o t hem . I f t he variable does not have a default , and no value is provided, t he st ored procedure ( or funct ion) will fail. St ored procedures can t ake param et ers of any dat a t ype, including t he TEXT, NTEXT and I MAGE dat a t ypes. The CURSOR dat a t ype can only be used as an out put param et er, and we m ust also specify t he VARYI NG keyword. User- defined funct ions can only accept scalar dat a t ypes. The following procedure from t he gasTI X dat abase, shown in List ing 7.12, ret urns all part icipant s ( m usic art ist s or sport s t eam s) t hat appear at a specified venue. Not ice t hat in addit ion t o using a param et er, t he procedure also uses a local variable, @t oday. Also not e t hat t he @venue_id param et er is assigned a default value. List in g 7 .1 2 A sa m ple ga sTI X st or e d pr oce du r e a cce pt s a sin gle in pu t pa r a m e t e r a n d u se s a loca l va r ia ble . CREATE PROCEDURE dbo.usp_get_participants_by_venue (@venue_id INT = -1) -- default value of -1 AS DECLARE @today SMALLDATETIME select @today = CAST(CONVERT(VARCHAR(10), GETDATE(), 101) AS SMALLDATETIME) SELECT DISTINCT p.participant_id, p.participant_name FROM

venue v

INNER JOIN configuration c ON c.venue_id = v.venue_id INNER JOIN event e ON e.configuration_id = c.configuration_id AND e.event_date BETWEEN @today AND DATEADD(m, 3, @today) AND e.inactive_flag = 'N' INNER JOIN event_participant ep ON ep.event_id = e.event_id INNER JOIN participant p ON p.participant_id = ep.participant_id WHERE

v.venue_id = @venue_id

Out put param et ers can be specified wit hin a st ored procedure if we would like t he procedure t o ret urn a single value, rat her t han a recordset . Alt ernat ively, we can ret urn a populat ed cursor from a st ored procedure wit h a CURSOR dat a t ype as t he out put param et er. To use out put param et ers, we sim ply include t he keyword OUTPUT ( or j ust OUT) wit hin t he param et er declarat ion. The exam ple shown in List ing 7.13 ret rieves t he t ot al am ount of revenue generat ed by t icket sales for a part icular venue as an out put param et er. List in g 7 .1 3 A sa m ple ga sTI X st or e d pr oce du r e u se s a sim ple ou t pu t pa r a m e t e r . CREATE PROC output_param_example @region_id VARCHAR(11), @total_sales SMALLMONEY OUTPUT AS SELECT @total_sales = SUM(total_charged) FROM sale a, region b, event c, configuration d, venue e WHERE a.event_id = c.event_id

AND

AND

b.region_id = e.region_id

AND

d.venue_id = e.venue_id

AND

c.configuration_id = d.configuration_id

e.region_id = @region_id

Now, in List ing 7.14, we use t he @t ot al_sales out put param et er in t he following bat ch ( which could easily be a st ored procedure) . List in g 7 .1 4 Usin g t h e ou t pu t pa r a m e t e r r e t u r n e d fr om a ga sTI X st or e d pr oce dur e . DECLARE @total_sales SMALLMONEY EXECUTE output_param_example 31, @total_sales = @total_sales OUTPUT SELECT 'the folks from New Jersey have purchased $ ' + CAST(@total_sales AS VARCHAR(10)) + ' worth of tickets'

Results: ---------------------------------------------------------------the folks from New Jersey have purchased $ 9627.45 worth of tickets

Transact SQL Cursors Transact SQL is a set - based language. This m eans we usually don't need t o process one row at a t im e. However, t here are t im es when we need t o exercise program m ing logic when reading t he result set row by row. This is when we need t o creat e and use a cursor. A cursor is an in- m em ory represent at ion of a t able or query result t hat we can m anipulat e one row at a t im e.

Caution: As expected, reading a table with a million rows one row at a time can be painfully slow. Cursors will usually perform poorer than comparable set-based operations, and should be avoided when another solution is available (such as a SELECT statement using a GROUP BY clause). However, for certain tasks cursors are the only option.

Perhaps one of t he best exam ples of when t o use cursors is when we apply a bat ch j ob t o a large t able. I n t his case, perform ance is not as im port ant as reducing t he locking cont ent ion on t he t able, or perhaps even as im port ant as filling up t he t ransact ion log. Tip: Cursors in SQL Server generally do not lock multiple pages at a time since they are reading the data set row by row. This reduces the overall amount of locking contention.

Working wit h cursors in Transact SQL consist s of im plem ent ing several st eps: 1. Create the cursor with the DECLARE statement. This command defines a SELECT clause, possibly joining multiple tables and views to create the recordset to be referenced by the cursor. 2. Open the cursor with the OPEN command. SQL Server will populate the recordset by executing the SELECT statement that defined the cursor, but will not return any records. 3. Fetch one row at a time and take the desired programmatic action on each row. Depending on the type of cursor, we might change values in the underlying table or view. We might also move forward and back (scroll) inside the cursor. 4. Once all processing is complete (all rows have been traversed), CLOSE the cursor (we can reopen it later if necessary). 5. Once the program has completed its task(s) and it is clear that we will not need the cursor again, remove it from memory with the DEALLOCATE command.

Cursors cannot be shared am ong connect ions, but t hey can be declared as local or global, and can t hus be shared across t he sam e connect ion. Declaring a global cursor sim ply m eans t he cursor is m ade available for any st ored procedure or bat ch for t he durat ion of t he connect ion. A cursor declared as local has a m uch m ore lim it ed scope. I t can only be used by t he st ored procedure or bat ch in which it is declared. I f neit her opt ion is specified, t he default is det erm ined by t he dat abase opt ion DEFAULT TO LOCAL CURSOR, which can be m odified wit h t he ALTER DATABASE com m and or t he sp_dbopt ion syst em st ored procedure. The SELECT st at em ent used t o declare a cursor cannot cont ain t he FOR BROWSE or FOR XML clauses, and also cannot use COMPUTE, COMPUTE BY, or I NTO. Base t ables or views cannot be updat ed t hrough a cursor if t he SELECT st at em ent in t he cursor declarat ion cont ains: z

an aggregate function (such as SUM, COUNT, etc.)

z

a JOIN of any kind

z

a subquery

z

the UNION keyword

z

the DISTINCT keyword

z

a GROUP BY clause

Functions in Transact SQL Transact SQL funct ions are built - in or user- defined code m odules t hat let us m anipulat e dat a, perform com put at ions and com parisons, and ret rieve ot her syst em inform at ion. Wit h SQL Server 2000 user- defined funct ions are also support ed. Funct ions can be det erm inist ic and non- det erm inist ic. Det erm inist ic funct ions ret urn t he sam e result wit h t he sam e set of input values, regardless of when t hey are execut ed. Built - in funct ions t hat are always det erm inist ic include I SNULL, CEI LI NG, FLOOR, and ot hers. Non- det erm inist ic funct ions ret urn a different result depending on when t hey are execut ed. For inst ance, get dat e( ) is a non- det erm inist ic funct ion since it ret urns a different result each t im e. Caution: If a SQL Server table has a column populated with the result of a non-deterministic function, we cannot build an index on that column. We also cannot create a clustered index on a view that references a non-deterministic function.

The following exam ple uses several built - in funct ions. I SNULL ret urns a specific value if a NULL is encount ered. SUBSTRI NG let s us ext ract a port ion of a st ring. LEN let s us det erm ine t he num ber of charact ers in a st ring. CAST allows us t o change t he dat a t ype of a variable or const ant . List in g 7 .1 5 A SELECT st a t e m e n t ca n be m a de pow e r fu l w it h t h e u se of Tr a n sa ct SQL fu n ct ion s. SELECT TOP 3 SUBSTRING(event_name, 1, LEN(event_name)) AS event, CAST(SUBSTRING(ISNULL(special_instruction, 'not available'), 1, LEN(ISNULL(CAST(special_instruction AS VARCHAR(100)), 'not available'))) AS VARCHAR(40)) AS special_instr FROM event

Results: event

special_instr

-------------------------------------------------- -------------Hayward Polar Bears versus Fort Dodge Polar Bears

not available

Knoxville Rats versus Jackson Polar Bears

not available

Witchita Polar Bears versus Washington Rats

not available

Views Views are st ored queries t hat look and act like virt ual t ables. A view can be based on one or m ore t ables, or on ot her views. A view's cont ent is not st ored as a t able. Rat her, t he query t hat defined t he view is execut ed against t he base t ables each t im e a view is queried. A view cannot accept param et ers and cannot cont ain m ult iple SELECT st at em ent s ( wit h t he except ion of sub- queries, which are allowed) .

We can define a view using t he CREATE VI EW st at em ent ( refer t o t he SQL Server docum ent at ion for synt ax) . List ing 7.16 provides an exam ple of creat ing a view in t he gasTI X dat abase. List in g 7 .1 6 Cr e a t in g a vie w in t h e ga sTI X da t a ba se . CREATE VIEW event_participant_names AS SELECT participant_name, event_name FROM event a INNER JOIN event_participant b ON a.event_id = b.event_id INNER JOIN participant c ON c.participant_id = b.participant_id

I f we wish t o renam e a view ( or any ot her reusable code m odule) , we can use t he sp_renam e syst em st ored procedure. Wit h SQL Server 2000, we can also m odify t he SELECT st at em ent t hat creat ed t he view wit hout dropping and recreat ing a view. This can be accom plished by using an ALTER VI EW st at em ent , as shown in List ing 7.17. List in g 7 .1 7 Cr e a t in g a vie w in t h e ga sTI X da t a ba se . ALTER VIEW event_participant_names AS SELECT participant_name, event_name FROM event a INNER JOIN event_participant b ON a.event_id = b.event_id INNER JOIN participant c ON c.participant_id = b.participant_id INNER JOIN category d ON c.category_id = d.category_id WHERE category_name = 'music'

I f we are no longer using a view, we can drop it by execut ing t he DROP VI EW st at em ent as follows: DROP VIEW event_participant_names

The SELECT st at em ent t hat creat es a view cannot cont ain any of t he following: z

COMPUTE or COMPUTE BY

z

INTO

z

ORDER BY clause (unless we also use TOP keyword)

z

FOR XML or FOR BROWSE

Not e t hat t he CREATE VI EW st at em ent cannot be com bined wit h ot her st at em ent s in a bat ch. We are allowed t o execut e Dat a Modificat ion Language ( DML) st at em ent s against views wit h som e lim it at ions: z

DML statements must affect only one underlying table. For example, an INSERT statement can only add values to one table through a view and it must have the column list specified.

z

We cannot insert or update a computed or aggregate column through a view.

z

We cannot use WRITETEXT to modify TEXT data type column inside a view.

z

We cannot run an INSERT statement against a view without specifying columns for all non-nullable columns of the underlying table, unless those columns have default constraints associated with them.

z

If we use the WITH CHECK option when defining a view, all data modifications will have to conform to the SELECT statement that created the view.

The addit ion of I NSTEAD OF t riggers t o SQL Server 2000 allows developers t o build updat eable views on m ult iple base t ables. This t ype of t rigger is discussed lat er in t his chapt er in t he sect ion t it led " I NSTEAD OF Triggers."

AM FL Y

Advantages of Using Views Views are used for several different reasons. The first is t o hide dat a com plexit y. I nst ead of forcing our users t o learn t he T- SQL JOI N synt ax, we m ight wish t o provide a view t hat runs a com m only request ed SQL st at em ent .

TE

I f we have a t able cont aining sensit ive dat a in cert ain colum ns, we m ight wish t o hide t hose colum ns from cert ain groups of users. For inst ance, cust om er nam es, addresses, and social securit y num bers m ight be st ored in t he sam e t able. For a group of em ployees such as shipping clerks, we can creat e a view t hat only displays cust om er nam e and address. This is referred t o as vert ical part it ioning and is accom plished by specifying only t he appropriat e colum ns in t he CREATE VI EW st at em ent . Anot her way t o secure t able dat a is t o creat e a view t hat allows reading only cert ain rows from a t able. For inst ance, we m ight have a separat e view for each depart m ent 's m anager. This way, each m anager can provide raises only t o t he em ployees of his or her depart m ent . This is referred t o as horizont al part it ioning and is accom plished by providing a where clause in t he SELECT st at em ent creat ing a view. We can also use views t o enforce sim ple business rules. For inst ance, if we wish t o generat e a list of cust om ers t hat need t o receive a wint er cat alog of upcom ing gasTI X event s, we can creat e a view of cust om ers t hat have previously bought t icket s for art ist s/ t eam s scheduled t o perform during t he wint er. Views can also help us export dat a using t he Bulk Copy Program ( BCP) . BCP is a com m and- line ut ilit y t hat provides lower- level im port and export capabilit ies for SQL Server. I f we are using BCP t o export SQL Server dat a t o t ext files, we can form at t he dat a t hrough views, since t he form at t ing abilit y of t he BCP ut ilit y is quit e lim it ed.

Team-Fly®

Cust om izing dat a is also m ade easy using views. I f we wish t o display cust om ized com put ed values or colum n nam es form at t ed different ly t han t he base t able colum ns, we can do so by creat ing views. Disadvantages of Using Views Even t hough views can be a great t ool for securing and cust om izing dat a, t hey can at t im es perform slowly. I ndeed, t hey are no fast er t han t he query t hat defines t hem . Wit h SQL Server 2000, indexed views ( also referred t o as m at erialized views) are support ed t o overcom e t his lim it at ion. This feat ure enables us t o creat e a unique clust ered index on a view, which causes t he view result s t o be st ored sim ilar t o how a t able wit h a clust ered index is st ored. This feat ure is covered in m ore det ail lat er in t his chapt er. Caution: Views based on other views can especially degrade performance. Therefore, it is recommended that views should NOT be created based on other views. All views should be created against base tables.

Partitioned Views One of t he m any excit ing new feat ures of SQL Server 2000 is t he abilit y t o build a group of dat abases residing on different servers t hat can act as a single dat abase as far as client applicat ions are concerned. This can be useful if our t ables grow so large t hat we cannot effect ively st ore dat a on a single server. This concept is referred t o as a group of federat ed dat abases. Part it ioned views are im plem ent ed t hrough rem ot e ( dist ribut ed) queries. A part it ioned view is sim ply a view built on a set of t ables residing on local or rem ot e servers. All t ables part icipat ing in t he part it ioned view have t he sam e or sim ilar st ruct ure. The colum n nam es, dat a t ypes, precision, and scale m ust be sim ilar for a read- only view, and m ust be t he sam e t o allow updat es t hrough a view. Each server part icipat ing in building a part it ioned view m ust be set up as a linked server on t he local server where t he view is defined. DML st at em ent s, as well as t he CREATE VI EW st at em ent , are im plem ent ed t hrough OPENQUERY or OPENROWSET keywords. A t able on each server cont ains a port ion of t he ent ire dat a set . The part it ioned views, in effect , are really a way t o split a t able horizont ally for opt im izing perform ance. The colum n used t o split t he t able int o several sm aller t ables is referred t o as t he part it ioning colum n. Due t o t he relat ively large size and low price of hard disks, it is likely t hat m ost of our applicat ions will not m ake use of dist ribut ed part it ioned views. However, if we do encount er t he need t o use t hem , it is helpful t o know t hat t hey are support ed. There are som e issues t o keep in m ind when building and using dist ribut ed part it ioned views: z

Only the Enterprise and Developer editions of SQL Server support partitioned views.

z

Each linked server participating in the partitioned view has to have lazy schema validation option set on. This is accomplished through the sp_serveroption system stored procedure and advises SQL Server not to request metadata from any linked servers until it is actually needed. Indeed, if our query can be satisfied from the data on the local server, there is no need to run distributed queries.

z

The partitioned view must be set up on each of the servers participating in building the view. This means we must set up all participating servers as linked servers for each other.

z

A distributed transaction will be initiated if the partitioned view is distributed and updateable, and a DML statement is executed against the view. This also means that the Distributed Transaction Coordinator (DTC) must be running on all participating servers.

z

Each server must have SET XACT_ABORT option set to on, which ensures that if there are any type of runtime errors when executing a DML statement, the entire transaction will be rolled back.

For a com plet e discussion of t he advant ages and lim it at ions of dist ribut ed part it ioned views, refer t o t he SQL Server 2000 docum ent at ion.

Indexed Views Anot her excit ing new feat ure of SQL Server 2000 is t he abilit y t o build indexes on t he views. Just like using a part it ioned view, using an indexed view requires som e ext ra work. However, t he perform ance gains t hat can be achieved m ake t he ext ra st eps wort hwhile. I n order t o be indexed, a view m ust be bound t o t he dat abase schem a. I t t hen behaves in a sim ilar fashion t o a regular t able. As m ent ioned earlier, unless we bind t he view t o t he schem a, it is j ust a st ored query, rat her t hen a physical obj ect . The view creat ed in List ing 7.18 can be indexed because it uses t he WI TH SCHEMABI NDI NG keyword. ( Not e t hat in t he exam ple we m ust specify t he t wo- part form at for dat abase obj ect s, e.g. dbo.Venue, when using t he WI TH SCHEMABI NDI NG opt ion) . List in g 7 .1 8 Cr e a t in g a vie w in t h e ga sTI X da t a ba se u sin g t h e W I TH SCH EM ABI N D I N G k e yw or d. CREATE VIEW ca_venues WITH SCHEMABINDING AS SELECT venue_id, venue_name FROM dbo.Venue V, dbo.Region R WHERE V.region_id = R.region_id AND R.short_name = 'ca'

Now we can define an index on t he venue_id colum n in t he view t o speed up t he query perform ance: CREATE UNIQUE CLUSTERED INDEX ix_ca_venues ON ca_venues(venue_id)

SQL Server can now use t he ix_ca_venues index not only when reading rows from t he view, but also when querying t he underlying Venue t able ( depending on t he cost of using t he index versus t he cost of scanning t he ent ire t able or using anot her index) . I n order for SQL Server t o use an index defined on a view, t he following session set t ings m ust be set on: z

ANSI_NULLS

z

ANSI_PADDING

z

ANSI_WARNINGS

z

ARITHABORT

z

CONCAT_NULL_YIELDS_NULL

z

QUOTED_IDENTIFIERS

I n addit ion, NUMERI C_ROUNDABORT m ust be t urned off. Note: According to the SQL Server documentation, requiring that the clustered index for a view be unique improves efficiency. This is because it can more easily locate rows in the index affected by future data modifications.

Stored Procedures St ored procedures are groups of T- SQL st at em ent s execut ed t oget her. Using st ored procedures allows program m ers t o enclose m uch of t he business logic of a .NET e- Business applicat ion inside T- SQL code. There are m ult iple advant ages t o using st ored procedures: z

Stored procedures generally perform better than the same group of statements executed individually. When a stored procedure is first invoked, it is compiled, and the execution algorithm is cached. The SQL Server engine reads from this cache on subsequent invocations and does not have to decide how to best run a stored procedure each time it is executed.

z

Stored procedures provide a high level of security for SQL Server data. When we write a stored procedure for all DML statements and grant database users access only to these stored procedures, we can tightly control what users can and cannot do with the data.

z

Stored procedures can accept and return parameters. With SQL Server 2000, each stored procedure can have up to 2100 parameters.

z

Stored procedures allow us to encapsulate business rules within manageable components of code. When the business rule changes, we only have to modify our code in one place.

z

Stored procedures can be executed automatically when SQL Server starts up, or they can be invoked explicitly.

Creating Stored Procedures St ored procedures can be creat ed wit h t he CREATE PROCEDURE SQL st at em ent , or t hrough t he SQL Ent erprise Manager user int erface. There are cert ain rules t o keep in m ind when using t he CREATE PROCEDURE st at em ent . First , st ored procedure nam es m ust follow t he general rules for ident ifier nam es. Second, t he obj ect s t hat a st ored procedure references m ust exist when it is run. We no longer have t o pre- creat e obj ect s before referencing t hem , as we did in previous versions of SQL Server. We can now creat e, populat e, and ret rieve dat a from a t able or view wit hin a single st ored procedure. We j ust have t o m ake sure t hat t he obj ect is creat ed before it is referenced. This feat ure is called deferred nam e resolut ion because t he SQL Server engine does not check for t he exist ence of an obj ect unt il run- t im e. Despit e t he fact t hat we can creat e and reference obj ect s wit hin t he sam e procedure, we cannot drop and re- creat e t he obj ect wit h t he sam e nam e wit hin a single procedure.

So if we drop t he gasTI X t able nam ed Venue in a st ored procedure, we cannot creat e anot her t able nam ed Venue wit hin t he sam e procedure. Cert ain st at em ent s are not allowed wit hin st ored procedures. These include: z

CREATE DEFAULT

z

CREATE PROCEDURE

z

CREATE RULE

z

CREATE TRIGGER

z

CREATE VIEW

z

SET SHOWPLAN_TEXT

z

SET SHOWPLAN_ALL

St ored procedures can be nest ed up t o 32 levels deep. The m axim um size of a st ored procedure is 128 MB ( alt hough a st ored procedure of t his size would be hard t o m aint ain) ! Local t em porary t ables creat ed wit hin a st ored procedure go out of scope as soon as procedure execut ion has com plet ed. Cert ain Dat a Definit ion Language ( DDL) st at em ent s ( ALTER TABLE, CREATE I NDEX, et c.) as well as t he UPDATE STATI STI CS and DBCC com m ands need t o specify t he t wo- part form at when referencing dat abase obj ect s ( obj ect owner along wit h obj ect nam e) . This rule holds t rue only if users ot her t han t he obj ect owner will execut e t he st ored procedure. We can use t he sp_helpt ext st ored procedure t o read t he t ext of any exist ing st ored procedure wit hin a dat abase. This saves a fair am ount of t im e if we use st ored procedures as t em plat es for ot her procedures. The t ext of st ored procedures is available in t he SYSCOMMENTS t able, unless we use t he ENCRYPTI ON opt ion. Caution: Although we can query the SYSCOMMENTS system table directly to retrieve the text of our stored procedures, Microsoft recommends against querying system tables. It is better to use the sp_helptext stored procedure to retrieve this text.

Stored Procedure Performance Issues One of t he m ain benefit s of st ored procedures is t heir abilit y t o execut e fast er t han t he sam e set of T- SQL st at em ent s execut ed individually. This perform ance gain is due t o t he fact t hat st ored procedure execut ion plans are cached and reused aft er t he first execut ion. At t im es, however, we need t o force SQL Server t o choose a different execut ion plan each t im e t he st ored procedure is execut ed. A good exam ple of t his would be when we do not know how m uch dat a is going t o be creat ed in t he t em porary t ables used by t he st ored procedure. I n such cases, it is advisable t o use t he RECOMPI LE opt ion. There are several different ways t o force SQL Server t o choose an alt ernat ive execut ion plan: z

Create the stored procedure using the WITH RECOMPILE option. This will cause SQL Server to recompile the procedure each time it is called.

z

Execute the stored procedure using the WITH RECOMPILE option in the EXEC command.

z

Execute UPDATE STATISTICS on any table affected by the stored procedure. SQL Server will flag the affected procedure and will recompile it the next time it is called.

z

Drop and recreate the procedure. Alternatively, use the ALTER PROC statement.

z

Stop and restart SQL Server. All execution plans are recreated when the stored procedures are first executed after a server restart.

z

Execute the sp_recompile system stored procedure for a particular stored procedure to refresh the query plan.

I t is a good idea t o recom pile st ored procedures when indexes have been m odified, as well as when t he dat a volum e affect ed by a st ored procedures changes. Anot her reason t hat st ored procedures are very efficient is t hat t hey reduce net work t raffic. I nst ead of sending num erous SQL St at em ent s over t he wire, users only have t o writ e a single EXEC procedure_nam e com m and. Since t here are very few lim it at ions on what we can do wit h st ored procedures, t heir level of com plexit y can vary widely. The sim plest st ored procedures ret rieve values from a single t able according t o a param et er passed in. The exam ple shown in List ing 7.19 ret rieves all part icipant s whose nam e st art s wit h a specified let t er. List in g 7 .1 9 Cr e a t in g a st or e d pr oce du r e t h a t t a k e s a n in pu t pa r a m e t e r a n d r e t u r ns a r e su lt se t . CREATE PROCEDURE dbo.usp_get_participants_by_name ( @participant_name VARCHAR(50) ) AS SET CONCAT_NULL_YIELDS_NULL OFF SELECT participant_id, participant_name = CASE WHEN salutation IS NULL THEN participant_name ELSE salutation + ' ' + participant_name END FROM participant WHERE participant_name LIKE '%'+ @participant_name + '%' UNION SELECT participant_id, participant_name = CASE WHEN middle_initial IS NULL THEN salutation + ' ' + first_name + ' ' + last_name ELSE salutation + ' ' + first_name + ' ' + middle_initial + '. ' + last_name

END FROM Participant WHERE last_name LIKE '%' + @participant_name + '%' OR first_name LIKE '%'+ @participant_name + '%'

The following execut ion of t he st ored procedure will bring back all records from t he Part icipant t able wher e t he part icipant 's first or last nam e cont ains t he st ring "ga" : EXEC usp_get_participants_by_name ga

More com plicat ed st ored procedures m ay cont ain looping st ruct ures, cursors, large num bers of param et ers, condit ional processing, and m ore. The st ored procedure shown in List ing 7.20 ret rieves t he next available row for a specified sect ion at a specified event .

List in g 7 .2 0 Cr e a t in g a st or e d pr oce du r e in ga sTI X t h a t t a k e s m u lt iple in pu t pa r a m e t e r s, e n list s a t r a n sa ct ion , ca lcu la t e s fu n ct ion s, a n d r e t u r n s a va lu e . CREATE PROC usp_get_available_row (@event_id INT, @section_id INT) AS DECLARE @row_id INT BEGIN TRAN SELECT @row_id = MIN(row_id) FROM event_section_row WITH (XLOCK) WHERE section_id = @section_id AND event_id = @event_id AND purchased_flag = 'n' AND (DATEDIFF(mi, locked_time, GETDATE()) > 5 OR locked_time IS NULL) UPDATE event_section_row SET locked_time = GETDATE() WHERE row_id = @row_id AND section_id = @section_id AND event_id = @event_id IF @@ERROR 0 BEGIN RAISERROR ('error occured while retrieving the best available row', 16, 1) ROLLBACK TRAN RETURN 1 -- failure END COMMIT TRAN SELECT row_id FROM event_section_row WHERE row_id = @row_id AND section_id = @section_id and event_id = @event_id RETURN 0 --success

Triggers A t rigger in SQL Server is a piece of code run whenever a DML ( I NSERT, UPDATE, or DELETE) st at em ent is execut ed against t he dat abase, but before t he dat a t ransact ion is com plet ed. This provides us wit h t he opport unit y t o validat e or check dat a prior t o let t ing t he SQL Server engine com plet e t he t ransact ion. I f a cert ain condit ion is encount ered, we can choose t o disallow t he DML operat ion, and roll t he dat a change back. Triggers can perform j ust about any operat ion support ed in a st ored procedure. I n addit ion, t riggers have access t o t he pseudo- t ables nam ed I NSERTED and DELETED t hat st ore t he rows affect ed by I NSERT, UPDATE and DELETE st at em ent s. Wit hin t riggers, we can define local variables, use condit ional and looping st ruct ures, and call powerful funct ions. Triggers can call each ot her, a concept referred t o as nest ed t riggers. This occurs if t able_a has a t rigger t hat includes an I NSERT, UPDATE, or DELETE st at em ent against t able_b. Table_b, on t he ot her hand, m ight have a t rigger t hat includes an I NSERT, UPDATE, or DELETE st at em ent against t able_a. The nest ed t riggers set t ing m ust be enabled on t he server for t riggers t o be able t o call each ot her; t his set t ing can be m odified by execut ing t he sp_configure syst em st ored procedure, or t hrough SQL Ent erprise Manager. Caution: Triggers can be nested up to 32 levels deep. This is a rather dangerous setting, however, which can easily lead to an infinite loop. Nested triggers should therefore be implemented with caution. In SQL Server 2000, if we do happen to initiate an infinite loop with nested triggers, the loop will automatically be terminated as soon as the nesting level exceeds the allowed limit.

Anot her powerful yet pot ent ially dangerous set t ing is RECURSI VE_TRI GGERS. This opt ion can be set by using t he sp_dbopt ion syst em st ored procedure, and let s a t rigger include a DML st at em ent t hat will cause it t o execut e it self again. For exam ple, an UPDATE t rigger m ight it self include an UPDATE st at em ent , and will t herefore init iat e anot her execut ion of t he t rigger. St at em ent s not perm it t ed inside t riggers include t he following: z

ALTER DATABASE

z

DISK INIT

z

DISK RESIZE

z

CREATE DATABASE

z

DROP DATABASE

z

RECONFIGURE

z

RESTORE DATABASE

z

RESTORE LOG

z

LOAD DATABASE

z

LOAD LOG

Tip: We cannot call a trigger explicitly. Therefore, the only way to test a trigger is to execute a DML statement against the table on which a trigger is defined.

AFTER Triggers AFTER t riggers are t he t riggers execut ed aft er a record has been insert ed, updat ed, or delet ed. AFTER t riggers have been around since t he early versions of SQL Server. They are used t o m aint ain referent ial int egrit y and t o enforce business rules. I n fact , before SQL Server added support for Declarat ive Referent ial I nt egrit y ( DRI ) , t riggers were t he only way t o m aint ain RI . Wit h SQL Server 2000, we can have m ult iple I NSERT, UPDATE, and DELETE t riggers defined on a t able. We can also define a t rigger for any com binat ion of I NSERT, UPDATE, and DELETE. List ing 7.21 rest rict s t he delet ion of a venue associat ed wit h an event t hrough a t rigger. List in g 7 .2 1 Cr e a t in g a t r igge r in ga sTI X t h a t r e st r ict s de le t ion of a Ve n u e w it h a n a ssocia t e d Eve n t , e n for cin g Re fe r e n t ia l I n t e gr it y. CREATE TRIGGER del_venue ON venue FOR DELETE AS IF EXISTS (SELECT E.* FROM Event E, Configuration C, Deleted D WHERE E.configuration_id = C.configuration_id AND D.venue_id = C.venue_id) BEGIN RAISERROR('You cannot delete a Venue associated with an Event', 16, 1) ROLLBACK END

The exam ple shown in List ing 7.22 ensures t hat a Part icipant cat egory m at ches t he cat egory of t he Event prior t o adding or m odifying an Event _Part icipant record. This ensures, for exam ple, t hat we do not assign a m usician t o a foot ball gam e accident ally. List in g 7 .2 2 Cr e a t in g a t r igge r in ga sTI X t h a t e n su r e s t h e Pa r t icipa n t a n d Eve n t ca t e gor ie s m a t ch . CREATE TRIGGER ins_upd_event_participant ON event_participant FOR INSERT, UPDATE AS DECLARE @category_id INT, @participant_id INT SELECT @category_id = (SELECT E.category_id FROM Inserted I, Event E WHERE I.event_id = E.event_id) SELECT @participant_id = (SELECT participant_id FROM inserted)

IF (SELECT category_id FROM participant WHERE participant_id = @participant_id) @category_id BEGIN RAISERROR ('The Participant''s category does not match the Event category', 16, 1) ROLLBACK END

I n previous versions of SQL Server, program m ers oft en used cascading t riggers t o m aint ain referent ial int egrit y. Cascading t riggers delet ed all corresponding rows from relat ed child t ables when a part icular record was delet ed from t he parent t able. For exam ple, a cascading t rigger could delet e all records for cust om er num ber '2' when t hat cust om er record was delet ed from t he Cust om er t able. Wit h SQL Server 2000, we can specify cascading updat es and delet es wit hout writ ing t riggers. This is accom plished t hrough SQL Ent erprise Manager by viewing t he t able in design view, clicking t he relat ionships but t on, and select ing t he appropriat e opt ions. Refer t o t he " Adding Referent ial I nt egrit y Const raint s" sect ion earlier in t his chapt er for addit ional inform at ion about cascading updat es and delet es. AFTER t riggers are som ewhat t ricky when enclosed inside t ransact ions due t o t he fact t hat t here are t hree t ransact ion st at es wit hin an AFTER t rigger: z

Before changes are made. This is when the transaction begins.

z

During data modifications. This is when the INSERTED and DELETED tables are created, and contain both the old and new images of the modified record(s).

z

After the changes are completed. This is when the transaction is committed (or rolled back), and the underlying table contains the new version of the record(s). The INSERTED and DELETED tables are dropped.

Note: AFTER triggers cannot be created on views. They can only be specified on tables.

INSTEAD OF Triggers I NSTEAD OF t riggers are new t o SQL Server 2000. They t ake an act ion inst ead of execut ing a part icular DML st at em ent . For inst ance, inst ead of delet ing a row in a Sales t able, a t rigger m ight copy it first t o t he Sales_Hist ory t able and t hen delet e it . Keep in m ind t hat t he original com m and t hat invoked t he I NSTEAD OF t rigger is never execut ed. Therefore, if we j ust want t o copy t he Sales record t hat was at t em pt ed t o be delet ed, we can do so wit hout act ually delet ing t he record. Not ice t hat I NSTEAD OF t riggers can st ill t ak e advant age of t he I NSERTED and DELETED pseudo- t ables, even t hough t he original com m and does not get execut ed. Tip: INSTEAD OF triggers can be defined for views, and can make a view updateable even if it refers to multiple base tables. Since the INSTEAD OF trigger effectively disables the original DML statement, it can substitute multiple DML statements for the single statement executed against a view. This way, we can actually allow updates through a view based on multiple tables.

User-Defined Functions User- defined funct ions are a welcom e addit ion t o t he program m er's arsenal in SQL Server 2000. User- defined funct ions are useful any t im e we need t o perform a t ask repeat edly, and any t im e we need t o ret urn a single value or a result set . Som et im es built - in funct ions are not sufficient for m eet ing business requirem ent s specific t o a .NET e- Business applicat ion. There are t hree t ypes of user- defined funct ions: scalar, inline t able- valued, and m ult ist at em ent t able- valued funct ions. These t ypes progress in com plexit y. Scalar funct ions ret urn a single value, while t he t able- valued funct ions ret urn an ent ire t able. For an inline funct ion t he t able ret urned is t he result set of a single SELECT st at em ent . The t able ret urned by a m ult ist at em ent funct ion is built using I NSERT st at em ent s wit hin a BEGI N and END block. All user- defined funct ion t ypes can be called direct ly from SELECT st at em ent s. Therefore, when grant ing perm issions t o a user- defined funct ion, t he synt ax is t o grant SELECT ( not EXECUTE) , as follows: GRANT SELECT ON fn_participant_category TO PUBLIC

Inline Table-Valued Functions

AM FL Y

I nline t able- valued funct ions can be t hought of as views t hat can accept param et ers. Such funct ions can cont ain only a single SELECT st at em ent . No DML st at em ent s are allowed wit hin t he funct ion. Unlike views, inline t able- valued funct ions can accept param et ers and ret urn dat a accordingly. Addit ionally, t he funct ion's body is not enclosed wit hin BEGI N and END delim it ers.

TE

Bot h inline and m ult ist at em ent t able- valued funct ions are called wit h a synt ax different from t hat used wit h scalar funct ions. Since t able- valued funct ions ret urn result set s sim ilar t o t ables, we can execut e t hem wit h t he SELECT * FROM dbo.m y_t able_valued_funct ion_nam e synt ax. The exam ple shown in List ing 7.23 creat es a sim ple inline funct ion t hat ret urns a list of Sales where t he t ot al am ount charged is great er t han t he specified input param et er. List in g 7 .2 3 Cr e a t in g a n in lin e t a ble - va lu e d u se r - de fin e d fu n ct ion . CREATE FUNCTION sales_by_total (@total SMALLMONEY) RETURNS TABLE AS RETURN SELECT * FROM Sale WHERE Total_Charged > @total

Not ice t hat wit h inline t able- valued funct ions, we do not have t o specify t he t able st ruct ure, even t hough we are ret urning a rowset ( t able) . To call t he funct ion we j ust creat ed, we sim ply invoke it by nam e, passing in t he single param et er:

Team-Fly®

SELECT * FROM sales_by_total (255)

Multistatement Table-Valued Functions Mult ist at em ent t able- valued funct ions are sim ilar t o st ored procedures in t hat we can use cont rol- of- flow st at em ent s, assignm ent s, cursors, and ot her st at em ent s. A m ult ist at em ent funct ion m ust have a funct ion body enclosed wit hin t he BEGI N and END delim it ers. The st at em ent wit hin t he funct ion body can accom plish any of t he following: z

Assign values to local variables inside the function.

z

Create, open, and fetch rows from a cursor. The fetch statements returning data to the client are not supported, but we can still assign values to variables with the INTO keyword.

z

INSERT, UPDATE, and DELETE rows in table variables local to the function.

z

Perform any kind of conditional or loop processing.

z

Execute extended stored procedures.

Note: We are not allowed to use SQL Server non-deterministic functions within the body of a multistatement function because they might return different values. Hence, we cannot rewrite a stored procedure as a user-defined function if the stored procedure uses non-deterministic built-in functions or executes DML statements outside the scope of the procedure.

The m ult ist at em ent funct ion shown in List ing 7.24 ret urns t he revenue generat ed by t icket s shipped t o various st at es. The funct ion also assigns a ranking t o t he am ount of revenue generat ed by each st at e. List in g 7 .2 4 Cr e a t in g a m u lt ist a t e m e n t t a ble - va lu e d u se r - de fin e d fu n ct ion . CREATE FUNCTION fn_revenue_by_state (@start_date SMALLDATETIME, @end_date SMALLDATETIME) RETURNS @revenue_by_state TABLE (state_name VARCHAR(55) NOT NULL, number_of_events INT, revenue MONEY NULL, ranking VARCHAR(20) NULL) AS BEGIN

INSERT @revenue_by_state (state_name, number_of_events, revenue) SELECT long_name, COUNT(*) AS number_of_events, SUM(total_charged) AS revenue FROM region r, sale s, event e WHERE r.region_id = s.ship_to_region_id AND e.event_id = s.event_id AND event_date BETWEEN @start_date AND @end_date

GROUP BY long_name

UPDATE @revenue_by_state SET ranking = CASE WHEN revenue < 1000 THEN 'poor' WHEN revenue BETWEEN 1000 AND 5000 THEN 'average' ELSE 'great' END

RETURN END

We can execut e t he funct ion we j ust creat ed as follows: SELECT * FROM dbo.fn_revenue_by_state ('1/1/01', '12/31/01')

XML Support in SQL Server 2000 Over t he past few years, soft ware developers have used a variet y of t echnologies t o ret rieve recordset s from relat ional dat abases. DAO, RDO, and m ost recent ly ADO each had it s advant ages and drawbacks. Now wit h t he .NET Fram ework, we can t ake advant age of t he power of ADO.NET. Wit h SQL Server 2000, we are provided wit h even m ore opt ions because of t he XML funct ionalit y built in t o t he SQL Server archit ect ure. We can use t he SQL Server XML ext ensions t o ret rieve a dat aset from t he dat abase as an XML t ree, as opposed t o ret rieving a recordset . Middle- t ier com ponent s or front - end code can parse such an XML t ree and form at it appropriat ely. The m ain advant age of using XML is t hat it is passed as a t ext st ring, and is t herefore support ed by all plat form s. SQL Server 2000 is one of t he first DBMSs t o fully support XML. T- SQL ext ensions allow us t o read and writ e dat a in XML form at , while t he int egrat ion of I I S and SQL Server allows us t o access our relat ional dat a from t he I nt ernet in XML form at wit hout writ ing any code. The following sect ions discuss bot h opt ions, and com m ent on t heir pros and cons.

Transact-SQL XML extensions The m ain XML ext ension of Transact - SQL is t he FOR XML clause of t he SELECT st at em ent . The FOR XML clause has m any different opt ions, and can be som ewhat t ricky t o m ast er. However, if we spend enough t im e figuring out t he det ails, we will be pleased t o discover what t his clause has t o offer. The synt ax of t he FOR XML clause is as follows: FOR XML mode [, XMLDATA] [, ELEMENTS][, BINARY BASE64]

There are t hree different m odes of t he FOR XML clause: AUTO, RAW, and EXPLI CI T. AUTO and RAW are ext rem ely easy t o use. AUTO sim ply t reat s t he t able nam e as t he elem ent and colum n nam es as at t ribut es. RAW appends a < row> t ag t o t he sam e out put and suppresses t he t able nam e. For exam ple, List ing 7.25 shows queries t hat ret urn t he t op row of t he gasTI X Configurat ion t able:

List in g 7 .2 5 Re t u r n in g XM L fr om SQL Se r ve r 2 0 0 0 w it h t h e AUTO a n d RAW m ode s. SELECT TOP 1 * FROM Configuration FOR XML AUTO

Result (formatted for readability): XML_F52E2B61-18A1-11d1-B105-00805F49916B ------------------------------------------------------------- SELECT TOP 1 * FROM Configuration FOR XML RAW Result (formatted for readability): XML_F52E2B61-18A1-11d1-B105-00805F49916B -------------------------------------------------------------- Neit her AUTO nor RAW offer m uch flexibilit y. I f we want t o ret urn a single- node XML t r ee for a m ult it able j oin using AUTO, we m ust first creat e a t em porary t able t o hold t he result s of a non- XML query and t hen use anot her SELECT FOR XML st at em ent t o build and ret urn t he XML t ree. For exam ple, t he query show n in List ing 7.26 ret urns a hierarchy of t wo XML nodes, not one. List in g 7 .2 6 Th e AUTO m ode ca n n ot r e t u r n a sin gle - n ode XM L t r e e for a m u lt it a ble j oin . SELECT TOP 1 participant.*, category_name FROM Participant, Category WHERE Participant.category_id = Category.category_id AND Category.category_name = 'comedy' FOR XML AUTO

Result: XML_F52E2B61-18A1-11d1-B105-00805F49916B ---------------------------------------

I f we choose t o use RAW m ode inst ead of AUTO, a single node will be ret urned. However, t he t able nam e will not be ret urned as t he XML node, as is oft en desired. The AUTO opt ion of t he FOR XML clause allows us t o ret urn colum n nam es as separat e elem ent s, rat her t han at t ribut es, by sim ply adding t he ELEMENTS opt ion, as shown in List ing 7.27. List in g 7 .2 7 Re t u r n in g colu m n n a m e s a s se pa r a t e e le m e n t s u sin g t h e ELEM EN TS opt ion . SELECT TOP 1 participant.*, category_name

FROM Participant, Category WHERE Participant.category_id = Category.category_id AND Category.category_name = 'comedy' FOR XML AUTO, ELEMENTS Result: XML_F52E2B61-18A1-11d1-B105-00805F49916B ------------------------------------------------------- 51 3 10 Isaiah Gardner Mr Gardner Isaiah A www.isaiahgardner.com n D9690100-C78D-48B2-9CFB-35C1E3A47D3C

Comedy

The EXPLI CI T clause, while offering great er flexibilit y, is som ewhat m ore cum bersom e t o use t han t he AUTO clause. When using EXPLI CI T m ode, we can define a hierarchy of XML nodes t hat we wish t o see in our out put by specifying t he TAG and PARENT of each node. The inform at ion about t he XML hierarchy we define is st ored in a "universal t able" and is used for form at t ing our out put . As t he t op t ag in t he XML hierarchy has no parent , each SELECT st at em ent using t he EXPLI CI T m ode of t he FOR XML st at em ent st art s wit h t he following: SELECT 1

AS TAG,

NULL AS PARENT

Each node in t he hierarchy m ust be defined by a separat e SELECT st at em ent , which m ust cont ain t he colum n definit ions, as well as any aliases we m ight like t o use in our out put . For inst ance, t he SELECT st at em ent shown in List ing 7.28, which is a port ion of a larger exam ple of EXPLI CI T m ode, ret urns colum ns from t he gasTI X Event t able. List in g 7 .2 8 Qu e r yin g t h e da t a ba se in EXPLI CI T m ode , pa r t 1 . SELECT

1

AS TAG, NULL

AS PARENT,

Event.event_id

AS [Event!1!event_id],

Event.category_id AS [Event!1!category_id], Event.subcategory_id AS [Event!1!subcategory_id], Event.event_name

AS [Event!1!event_name],

Event.event_time

AS [Event!1!event_time],

NULL

AS [Category!2!category_id],

NULL

AS [Category!2!category_name],

NULL

AS [Subcategory!3!subcategory_id],

NULL

AS [Subcategory!3!subcategory_name]

FROM Event

Not ice t hat in addit ion t o specifying t he colum n nam es from t he Event t able, t he query in t he previous exam ple also defines t he skelet on for t he rest of t he st at em ent by providing colum n nam es from t wo ot her t ables, Cat egory and Subcat egory. This is necessary because EXPLI CI T m ode com bines out put s from m ult iple queries wit h a UNI ON ALL clause. Caution: Each query participating in a SELECT … FOR XML EXPLICIT statement must be valid, and must contain the same number of columns.

For all SELECT st at em ent s t hat follow in a SELECT st at em ent t hat uses t he EXPLI CI T keyword, we can also include t he TAG and PARENT colum ns. However, t hese keywords are opt ional, as long as we specify t he hierarchy. For exam ple, we could have a second SELECT st at em ent as shown in List ing 7.29. List in g 7 .2 9 Qu e r yin g t h e da t a ba se in EXPLI CI T m ode , pa r t 2 . SELECT 2, 1, Event.event_id

AS [Event!1!event_id!hide],

Event.category_id AS [Event!1!category_id!hide], Event.subcategory_id AS [Event!1!subcategory_id!hide], Event.event_name

AS [Event!1!event_name],

Event.event_time

AS [Event!1!event_time],

Category.category_id AS [Category!2!category_id!hide], Category.category_name

AS [Category!2!category_name],

NULL

AS [Subcategory!3!subcategory_id!hide],

NULL

AS [Subcategory!3!subcategory_name]

FROM Event, Category WHERE Event.category_id = Category.category_id

SELECT 2, 1 sim ply m eans t hat 2 will be t he t ag of t he out put , and 1 will be t he parent . Since t he first query cont ained [ event ! 1! event _id] and [ cat egory! 2! cat egory_id] , t he < event > t ag will be t he parent , and t he result s of t he second query will be enclosed in t he < cat egory> t ag. Alt ernat ively we could have specified t he full synt ax sim liar t o SELECT 2 AS TAG, 1 AS PARENT, et c. The last query of SELECT … FOR XML EXPLI CI T m ust cont ain t he ORDER BY clause if we want our out put t o be properly organized. I f we fail t o provide t he ORDER BY clause, our query will st ill wor k, but t he out put will not be exact ly what we expect . For inst ance, List ing 7.30 dem onst rat es what happens when ORDER BY is om it t ed. List in g 7 .3 0 N ot in clu din g t h e ORD ER BY cla u se pr odu ce s un de sir e d r e su lt s. SELECT

1

AS TAG, NULL

AS PARENT,

Event.event_id

AS [Event!1!event_id!hide],

Event.category_id AS [Event!1!category_id!hide], Event.subcategory_id AS [Event!1!subcategory_id!hide], Event.event_name

AS [Event!1!event_name],

Event.event_time

AS [Event!1!event_time],

NULL

AS [Category!2!category_id!hide],

NULL

AS [Category!2!category_name],

NULL

AS [Subcategory!3!subcategory_id!hide],

NULL

AS [Subcategory!3!subcategory_name]

FROM Event

UNION ALL

SELECT 2, 1, Event.event_id

AS [Event!1!event_id!hide],

Event.category_id AS [Event!1!category_id!hide], Event.subcategory_id AS [Event!1!subcategory_id!hide], Event.event_name

AS [Event!1!event_name],

Event.event_time

AS [Event!1!event_time],

Category.category_id AS [Category!2!category_id!hide], Category.category_name

AS [Category!2!category_name],

NULL

AS [Subcategory!3!subcategory_id!hide],

NULL

AS [Subcategory!3!subcategory_name]

FROM Event, Category WHERE Event.category_id = Category.category_id

UNION ALL

SELECT 3, 1,

Event.event_id

AS [Event!1!event_id!hide],

Event.category_id

AS [Event!1!category_id!hide],

Event.subcategory_id

AS [Event!1!subcategory_id!hide],

NULL

AS [Event!1!event_name],

NULL

AS [Event!1!event_time],

Category.category_id

AS [Category!2!category_id!hide],

Category.category_name

AS [Category!2!category_name],

Subcategory.subcategory_id

AS [Subcategory!3!subcategory_id!hide],

Subcategory.subcategory_name AS [Subcategory!3!subcategory_name] FROM Event, Category, Subcategory WHERE Event.subcategory_id = Subcategory.subcategory_id AND Event.category_id = Category.category_id FOR XML EXPLICIT

XML_F52E2B61-18A1-11d1-B105-00805F49916B ---------------------------------------



...





...





Obviously t his is not what we int ended our out put t o look like. I nst ead, we want t o see a relat ional t ree where < event > is t he parent , and < cat egory> and < subcat egory> are children. List ing 7.31 dem onst rat es t he com plet e st at em ent wit h proper synt ax.

List in g 7 .3 1 I n clu din g t h e ORD ER BY cla u se in ou r XM L SELECT st a t e m e n t pr oduce s de sir e d r e su lt s.

SELECT

1

AS TAG, NULL

AS PARENT,

Event.event_id

AS [Event!1!event_id!hide],

Event.category_id AS [Event!1!category_id!hide], Event.subcategory_id AS [Event!1!subcategory_id!hide], Event.event_name

AS [Event!1!event_name],

Event.event_time

AS [Event!1!event_time],

NULL

AS [Category!2!category_id!hide],

NULL

AS [Category!2!category_name],

NULL

AS [Subcategory!3!subcategory_id!hide],

NULL

AS [Subcategory!3!subcategory_name]

FROM Event

UNION ALL

SELECT 2, 1, Event.event_id

AS [Event!1!event_id!hide],

Event.category_id AS [Event!1!category_id!hide], Event.subcategory_id AS [Event!1!subcategory_id!hide], Event.event_name

AS [Event!1!event_name],

Event.event_time

AS [Event!1!event_time],

Category.category_id AS [Category!2!category_id!hide], Category.category_name

AS [Category!2!category_name],

NULL

AS [Subcategory!3!subcategory_id!hide],

NULL

AS [Subcategory!3!subcategory_name]

FROM Event, Category WHERE Event.category_id = Category.category_id

UNION ALL

SELECT 3, 1, Event.event_id

AS [Event!1!event_id!hide],

Event.category_id

AS [Event!1!category_id!hide],

Event.subcategory_id

AS [Event!1!subcategory_id!hide],

NULL

AS [Event!1!event_name],

NULL

AS [Event!1!event_time],

Category.category_id

AS [Category!2!category_id!hide],

Category.category_name

AS [Category!2!category_name],

Subcategory.subcategory_id

AS [Subcategory!3!subcategory_id!hide],

Subcategory.subcategory_name AS [Subcategory!3!subcategory_name] FROM Event, Category, Subcategory WHERE Event.subcategory_id = Subcategory.subcategory_id AND Event.category_id = Category.category_id ORDER BY [Event!1!event_id!hide], [Category!2!category_id!hide], [Subcategory!3!subcategory_id!hide] FOR XML EXPLICIT

XML_F52E2B61-18A1-11d1-B105-00805F49916B ---------------------------------------







... Tip: Note that in the previous example we used the hide directive to exclude the event_id, category_id, and subcategory_id columns from the output. As these columns are used for joining the tables and do not provide any helpful information to the user, there is no need to include them in the output. However, we need these columns to order the result set, so we must include them in the query. The hide directive simply gives us a way to use the columns but not display them.

Anot her point wort h not ing in t his query is t hat we are using t he t wo child ent it ies < cat egory> and < subcat egory> under t he sam e parent < event > . To do so, our hierarchy is organized as follows: SELECT 1

AS TAG,

NULL

AS PARENT



SELECT 2

AS TAG,

AS PARENT



SELECT

3

AS TAG,

AS PARENT

Ot her queries m ight need several levels of nest ed t ags t o define t he XML hierarchy. For inst ance, we can ret rieve all Subcat egories for a part icular Cat egory. I n t his case, we can alt er t he hierachy slight ly by specifying:

SELECT 1

AS TAG,

NULL AS PARENT



SELECT 2

AS TAG,

AS PARENT



SELECT 3

AS TAG,

AS PARENT

Rem em ber t hat t he key t o successfully using t he FOR XML EXPLI CI T clause is t o define t he " universal t able" appropriat ely, specifying an equal num ber of colum ns in each part icipat ing SELECT st at em ent , and providing a correct ORDER BY clause.

AM FL Y

OPENXML

TE

The OPENXML funct ion allows us t o read an XML st ring, parse it as a rowset , and t ake t he appropriat e act ion depending on t he values cont ained in t he rowset . OPENXML provides an addit ional benefit t o t he T- SQL program m er: it also let s us read XML and pass an unlim it ed num ber of param et ers t o our queries ( or st ored procedures) . I t is hard t o im agine a procedure t hat will ever exceed t he lim it of param et ers ( 2100) . However, if we j ust need t o pass a huge am ount of dat a, we can put it in an XML st ring and use OPENXML t o parse it . So OPENXML is m uch like a ut ilit y t hat parses a m ult idim ensional array ( enclosed in XML t ags) and convert s it int o a t able. The synt ax of OPENXML m ay seem som ewhat convolut ed at first . However, syst em st ored procedures do t he m aj orit y of t he hard work; aft er using OPENXML a few t im es it will seem m uch easier. The first syst em procedure, sp_xm l_preparedocum ent , reads t he XML docum ent and creat es an int ernal represent at ion of it in m em ory. Next , we specify t he root node of t he XML docum ent t hat we wish t o use for parsing. Finally, t he second syst em procedure, sp_xm l_rem ovedocum ent , rem oves t he XML docum ent from m em ory. List ing 7.32 dem onst rat es a sim ple exam ple. List in g 7 .3 2 Usin g t h e OPEN XM L fu n ct ion . DECLARE @idoc INT

-- for sp_xml_preparedocument

DECLARE @doc varchar(1000) SET @doc ='

Team-Fly®







'

--Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

-- Execute a SELECT statement that uses the OPENXML rowset provider. SELECT

*

FROM OPENXML (@idoc, '/ROOT/event',2) WITH (event_name VARCHAR(55) '@event_name', event_time VARCHAR(12) '@event_time')

EXEC sp_xml_removedocument @idoc Not ice t hat t he WI TH clause of OPENXML let s us specify t he colum n headings and dat a t ypes for our rowset . I t also let s us define t he dat a source for our colum ns. I n t he query shown in List ing 7.32, t he source of event _nam e was @event _nam e under ROOT/ event ,and t he source of event _t im e was @event _t im e under ROOT/ event . We can easily ext end t his exam ple by adding t he cat egory nam e t o t he out put . To do so, we will have t o navigat e one level down t he XML t ree and base our OPENXML clause from ROOT/ event / cat egory node. This will force us t o specify which XML node is t he dat a source for each colum n in t he result set , as shown in List ing 7.33. List in g 7 .3 3 Usin g t h e OPEN XM L fu n ct ion t o fu r t h e r spe cify w h ich XM L n ode is t h e da t a sou r ce for e a ch colu m n . DECLARE @idoc INT

-- for sp_xml_preparedocument

DECLARE @doc varchar(1000) SET @doc ='







'

--Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement that uses the OPENXML rowset provider. SELECT

*

FROM OPENXML (@idoc, '/ROOT/event/category',2) WITH (event_name VARCHAR(55) '../@event_name', event_time VARCHAR(12) '../@event_time', category VARCHAR(20) '@category_name') EXEC sp_xml_removedocument @idoc Note: Once the OPENXML keyword converts the XML into a rowset, we can do any type of data manipulation allowed in SQL Server. OPENXML is thus not necessarily just for dressing up output. It can also easily be used to provide parameters to an UPDATE or INSERT statement.

Integration of SQL Server and IIS SQL Server 2000 offers a way t o query a dat abase direct ly from t he Web once t he web server is appropriat ely configured. Configuring XML support in I I S is fairly st raight forward. From t he St art m enu, select Program s| Microsoft SQL Server| Configure SQL XML Support in I I S. This opens t he Microsoft Managem ent Console for I I S. Not e t hat t he web server m ust be running at t his point . From t he Act ion m enu, select New| Virt ual Direct ory. This will bring up a dialog box t hat let s us configure all t he necessary opt ions. The General t ab is quit e int uit ive; it asks for t he virt ual direct ory nam e and t he physical pat h where t he files need t o be st ored. Unless we are int ending t o expose t em plat e files ( discussed short ly) , t he physical pat h specified on t his t ab is irrelevant . The Securit y t ab, shown in Figure 7.1, let s us define t he logon credent ials of t he users t hat will access our dat abase t hrough t he Web. Caution: If we provide the 'sa' password on the Security screen (as shown in Figure 7.1), every user querying our database will have system administrator privileges. Needless to say, this can easily lead to serious security issues, and should not be allowed

Choosing Windows aut hent icat ion on t his t ab let s our users log int o SQL Server based on t heir Windows 2000 perm issions. Keep in m ind t hat Windows user account s st ill need t o be assigned t o SQL Server roles. I f we choose t he 'basic aut hent icat ion t o SQL Server account ' opt ion, users m ust provide t heir own securit y credent ials. Alt hough t he last opt ion is t he m ost secure, it is st ill lim it ed t o clear t ext aut hent icat ion, which is fairly sim ple t o int ercept . The Dat a source t ab let s us specify t he server and t he dat abase we wish t o m ake available t hrough t he web. Before we can specify a dat a source, t he securit y account we specified on t he Securit y t ab m ust have t he appropriat e perm issions t o t he server and t he dat abase t o which we are t rying t o connect . I f we chose t he basic aut hent icat ion opt ion on t he Securit y t ab, we m ust specify t he valid SQL Server account and password prior t o select ing a dat a source.

Figure 7.1 The Securit y t ab of t he New Virt ual Direct ory dialog allows us t o rest rict access t o t he direct ory.

The Set t ings t ab shown in Figure 7.2 let s us configure t he t ypes of queries allowed t hrough t he web. URL queries allow users t o t ype in a SQL query direct ly in t he locat ion bar of a browser. This opt ion is useful if all of our users can writ e appropriat e queries and we can t rust t hem not t o dest roy our dat a. On t he ot her hand, if t he t ypes of queries users need t o execut e becom e com plex, we should opt for t em plat e queries. Tem plat e queries let t he adm inist rat or save cert ain pre- defined queries in t he direct ory specified on t he General t ab. Users can execut e t hese queries by file nam e. I n a way, t em plat e queries resem ble navigat ing t o a specific web sit e, since we are calling a query saved in a direct ory as a file. Xpat h queries are execut ed against an annot at ed XML- Dat a Reduced schem a. ( Defining t his schem a requires a fairly good underst anding of t he Xpat h language, and is beyond t he scope of t his chapt er) . The 'Allow POST' opt ion allows t he users t o post dat a t o t he dat abase along wit h ret rieving dat a from t he dat abase. The Virt ual Nam es t ab let s us define a short cut t o cert ain t em plat e files we wish t o expose t hrough I I S. This can be useful if our direct ory st ruct ure is com plicat ed, and we do not want dat abase users t o know t he int ricacies of our file syst em .

Figure 7.2 The Set t ings t ab allows us t o set several opt ions for our New Virt ual Dialog.

Finally, t he Advanced t ab let s us m odify t he locat ion of t he SQLI SAPI .DLL, which is t he I SAPI filt er t hrough which XML support is im plem ent ed in I I S. I f we have accept ed t he default opt ions when inst alling SQL Server, we will not need t o m ake m odificat ions t o t his t ab. Figure 7.3 dem onst rat es a URL query execut ed on t he t est server. The query sim ply brings back a few rows from t he gasTI X Part icipant t able. The XML ret urned from t he dat abase is in a very basic form at , but it can be easily given a nice shape by applying an XSL st ylesheet t em plat e. Alt hough useful for rem ot e SQL Server m anagem ent and cert ain int ranet applicat ions, t he abilit y t o query SQL Server t hrough t he Web has securit y ram ificat ions. For inst ance, if we allow users t o excut e queries t hrough t heir browsers, t hey could easily delet e t ables or even t he ent ire dat abase if t hey have appropriat e perm issions. The solut ion t o t hese problem s is t o secure t he dat a by only allowing users t o execut e XPATH queries or a lim it ed set of st ored procedures t hat do not perm it t he m odificat ion of dat a st ruct ures.

Figure 7.3 The URL quer y ret urns st andard XML in I nt ernet Explorer. Tip: In order to determine what specific queries are being executed over the Web on a database, we can turn on auditing through SQL Profiler. This tool allows us to track at a detailed level any and all database activity. If desired, we can set a filter for just the activity occurring over the Web.

From Here Chapt er 10, " Event Searching," and Chapt er 11, " Purchasing a Ticket ," discuss building t he ASP.NET websit e which consum es t he dat a services built in t his chapt er. Chapt er 15, " Perform ance Tuning in .NET," explores t he det ails of t uning bot h .NET dat a services and ot her part s of a .NET e- Business applicat ion. Finally, Chapt er 16, " Deploying t he Applicat ion," discusses deploying all part s of t he gasTI X sam ple applicat ion, including t he SQL Server 2000 dat a services.

8 Handling security Securit y is a growing concern for developers in a world where applicat ions are increasingly com posed of m ult iple, dist inct com ponent s each providing a specific funct ionalit y. The need t o verify t he securit y right s of t hese com ponent s is vit al t o ensuring safe execution of t he ent ire applicat ion. This chapt er discusses how t he .NET fram ework im plem ent s a securit y archit ect ure which allows developers t o easily incorporat e evidence- or role- based securit y using aut hent icat ion and aut horizat ion.

Security Concepts Securit y is a growing concern for developers in a world where applicat ions are increasingly com posed of m ult iple, dist inct com ponent s each providing a specific funct ionalit y. The need t o verify t he securit y right s of t hese com ponent s is vit al t o ensuring safe execution of t he ent ire applicat ion. This chapt er discusses how t he .NET fram ework im plem ent s a securit y archit ect ure which allows developers t o easily incorporat e evidence- or role- based securit y using aut hent icat ion and aut horizat ion. I n order t o underst and t he securit y m echanism s present wit hin t he .NET Fram ework it is im port ant t o first underst and som e securit y basics. There are t wo t ypes of securit y m echanism s built direct ly int o t he .NET Com m on Language Runt im e ( CLR) infrast ruct ure.: z

Role-based security

z

Code access, or Evidence-based security

An applicat ion t hat uses role based securit y grant s a user access t o resources or dat a based on t he ident it y and role m em bership of an user. The ident it y is det erm ined using t he credent ials a user provides. A m or e det ailed discussion on role based securit y is supplied in Aut horizat ion - working wit h role based securit y. Evidence- based securit y is new in t he .NET fram ework and is aim ed at securing t he execut ion of code t hat originat es from out side resources. I t is also referred t o as code- access securit y, since it rest rict s applicat ions abilit y t o access individual chunks of code. Exam ples of such code are t hird part y cont rols. Wit hin .NET you can use t his code and assign it execut ion perm issions. This m eans you can rest rict behavior and deny or grant access t o cert ain resources like your hard disk. A m ore det ailed discussion on evidence- based or code access securit y is supplied in Working wit h Code Access Securit y.

Authentication and Authorization Securit y is a t wo st ep process t hat involves bot h aut hent icat ion and aut horizat ion. These t wo t erm s are com m only used int erchangeably, but in realit y t hey each have a specific m eaning. Aut hent icat ion is t he process of discovering t he ident it y of t he connect ed user and verifying t he user’s ident it y wit h a t rust ed aut horit y. Aut horizat ion, on t he ot her hand, is t he process of select ing resources t hat are accessible by t he aut hent icat ed user. The aut hent icat ed user is ident ified using a concept called principal. A principal is a given user’s ident it y and any roles t hat user is a m em ber of. A role is a set of securit y clearances allowed t o a group of users. Com m on roles are “ Adm inist rat or” and “ Typical User” . Breaking securit y int o roles allows developers t o easily assign t he sam e securit y clearances t o a dynam ically defined set of users.

When we use Windows as our aut hent icat ion m ode, t he .NET fram ework aut om at ically creat es a WindowsPrincipal obj ect . This obj ect is part of t he Syst em .Securit y.Principal nam espace and assum es t he right s assigned t o t he specified Windows user. A generic Principal obj ect can also be m anually creat ed based on our own cust om roles. This allows us t o m aint ain our own dat abase of user/ roles and creat e t he appropriat e principal obj ect . Thus, a principal is defined by t he securit y right s an individual user has, com bined wit h t he right s offered by roles t he user is a m em ber of. The principal t herefore has right s t o act on t he user’s behalf. Securit y right s are cum ulat ive, t hat is, if a user belongs t o t wo roles, he has t he sum of all perm issions included in eit her role. So if t he first role allows access t o an applicat ion but t he second role does not , t he user can st ill access t he applicat ion using t he perm issions of t he first role. Wit hin t he .NET fram ework we dist inguish bet ween t hree t ypes of principals: z

A generic principal t hat represent s users and roles independent of t he underlying Windows NT or Windows 2000 user s and roles. This is t he t ype of principal used by t he gasTI X w ebsit e t o allowing us t o use Digit al Cert ificat es, as described lat er in t his chapt er.

z

A windows principal t hat represent s user s and roles based upon t he underlying Windows NT or Windows 2000 users and roles t hat can im personat e anot her user. Through im personat ing a user can get access t o resources on behalf of t he im personat ed user.

z

A cust om principal t hat can be defined by an applicat ion and can ext end t he not ion of a principal's ident it y and roles.

As m ent ioned previously, t he process of aut hent icat ing users is done by checking t he provided credent ials wit h a t rust ed aut horit y. I n general, a variet y of m echanism s can be used. Exam ples of t hose m echanism s are basic aut hent icat ion, Microsoft Passport , and digest aut hent icat ion. Basic aut hent icat ion and Microsoft Passport are used t hroughout t his chapt er t o describe t he aut hent icat ion m et hods used by gasTI X. Digest aut hent icat ion is a new feat ure of I I S 5.0 which encrypt s t he user’s password inform at ion inst ead of using clear t ext as basic aut hent icat ion does. This process is designed t o t hwart t he int ercept ion of password inform at ion being t ransm it t ed t o t he websit e. While t his m akes t he ent ire process m ore secure, it com es at an addit ional cost . I n order t o encrypt t he password inform at ion, t he client m ust be running I nt ernet Explorer 5.x or have t he .NET fram ework inst alled. This lim it s t he reach of applicat ions using digest aut hent icat ion, t hus gasTI X uses basic aut hent icat ion in order t o m axim ize our pot ent ial audience. Once t he user is aut hent icat ed, all inform at ion about t his user is available and usable wit hin t he code. Wit h t his inform at ion we decide what access should be grant ed t o our code or resources. This is called aut horizat ion and is only possible when t he aut hent icat ion process is finished.

Authenticating Users As st at ed previously, t he aut hent icat ion process is t he process of discovering and verifying t he ident it y of t he connect ed user. There are several m et hods t hat can be used t o verify t his ident it y. On t he I nt ernet , it is not always possible t o use built - in aut hent icat ion, so ot her m et hods should be used. Com m only used m et hods are:

z

Using built-in operating system authentication mechanisms such as Windows NT Challenge/Response

z

Using username/password type of validations in combination with cookies

z

Using Digital Certificates

z

Using built-in authentication mechanisms

I n a built - in or int egrat ed aut hent icat ion scenario, t he user is aut hent icat ed using eit her NTLM Challenge/ Response or Kerberos. Built - in securit y is very secure since no encrypt ed passwords are sent over t he net work. A benefit of int egrat ed securit y is t hat you can provide t he user wit h a single- logon policy but it should only be considered when t he users have Windows NT Dom ain or Act ive Direct ory account s, all of t hem are behind a corporat e firewall ( int ranet ) and use I nt ernet Explorer 3.01 or higher. Not e t hat Kerberos is only used when t he browser is com pat ible wit h t he Kerberos V5 prot ocol and Act ive Direct ory Services are inst alled on t he server. I n t his case t he aut hent icat ion process uses bot h challenge/ response and Kerberos. When you want t o use int egrat ed securit y t he configurat ion file for t he websit e needs t o be t old t o use int egrat ed securit y. The rest of t he process is very st raight forward. I f t he user is not aut hent icat ed he will not be given access t o t he pages on t he websit e. List in g 8 .1 Th is list in g sh ow s a por t ion of t h e w e b.con fig file for ga sTI X.com u sin g in t e gr a t e d se cu r it y.



Using Username/Password Validation with Cookies I n a usernam e/ password scenario, t he user will be prom pt ed by t he syst em t o ent er a valid com binat ion which is checked on t he websit e against som e st ore cont aining t he valid com binat ions. Once t his is done, t he connect ion st at e can be cont rolled by eit her using a cookie or st oring t he credent ials in a session variable. For t his sam ple we use cookie- based aut hent icat ion. The exam ple cont ains t hree pages; t he first is t he configurat ion file for t he websit e. I n t his file we t ell t he sit e t o use cookie- based aut hent icat ion, or “ form s aut hent icat ion.” The second page is t he default .aspx page t hat will be hit by t he user when accessing t he websit e. And finally, t he t hird page is a login page where t he user will be prom pt ed t o ent er a valid usernam e/ password com binat ion. The first page t ells t he websit e t o use cookies. I n line t hree of list ing 8.1, t he page t ells t he com piler t o use form s based aut hent icat ion. Line four defines t hat when t he cookie is not found t he user should be redirect ed t o t he login page which we have nam ed login.aspx. The nam e of t he cookie is t he second param et er, in t his case, “ .ASPXgasTI XCookie.”

List in g 8 .2 Th is list in g sh ow s a por t ion of t h e w e b.con fig file for ga sTI X.com u sin g for m s ba se d se cu r it y.







Now t he websit e knows t o redirect unaut hent icat ed users t o t he login page when t he aut horizat ion cookie is not present . The default st art ing point for an user is t he default .aspx file. For t his exam ple, t he gasTI X page has been reduced t o display only t he nam e of t he aut hent icat ed user. I t also cont ains a but t on t o sign out and clear t he cookie. Pushing t his but t on will also redirect us t o t he login page. I n t he gasTI X websit e, t he login can be achieved m anually, like on t his page, or t hrough t he use of Microsoft Passport . List in g 8 .3 Th is sim ple .a spx pa ge ide n t ifie s t h e a u t h e n t ica t e d u se r a n d offe r s t h e opt ion t o sign ou t , cle a r in g t h e a u t h e n t ica t ion cook ie .



Demo - Using username/password validation with cookies



User authenticated page





The t hird page in t his sam ple is t he login.aspx page. On t his page, t he user is asked t o provide his credent ials which will be checked against som e cust om code. I n t his sim ple exam ple, we are sim ply checking t o see if t he predet erm ined values have been ent ered. This code could also be writ t en t o validat e t he user against Act ive Direct ory Services or Hailst orm services. For our dem o we will only accept a usernam e/ password com binat ion of gasbags/ gast ix and depending on t he choice of t he user we can persist t he cookie. When t he user is aut hent icat ed he is redirect ed t o t he URL he t yped in t o get access t o t he sit e, in t his case, default .aspx. List in g 8 .4 Th is code a llow s t h e u se r t o e n t e r h is cr e de n t ia ls for a u t h e n t ica t ion .

AM FL Y





TE

Demo - Using username/password validation with cookies (Login page)





Login

Team-Fly®

Username:

Password:

Remember Login:






Using Certificates Using Digit al Cert ificat es is an excellent securit y solut ion for cert ain scenarios. A Digit al Cert ificat e is a piece of soft ware t hat has been aut hent icat ed by a designat ed t hird- part y, such as Verisign. I nform at ion about t he cert ificat e is passed along wit h t he web request , m aking it easy t o verify. This act ually provides a sim ple solut ion for t he client code, as t he developer does not need t o worry about passing usernam es and passwords t o be aut hent icat ed. Digit al Cert ificat es are not suit ed for every sit uat ion, however. Since t he Digit al Cert ificat e is a piece of soft ware t hat m ust be inst alled on t he m achine m aking t he request , it is not an ideal solut ion for public websit es. The average web user is not willing t o inst all new soft ware on his m achine j ust t o browse a web sit e. Therefore, t he use of Digit al Cert ificat es is com m only relegat ed t o business- t o- business t ransact ions such as invent ory inquiries, order purchasing, and work out sourcing. Digit al Cert ificat es are also appropriat e for int ernal applicat ions such as a secure int ranet .

The gasTI X XML Web services are designed t o check for t he presence of an aut horized Digit al Cert ificat e before allowing cert ain m et hod calls t o be m ade. Som e m et hod calls, such t hose for list ing event s and ret rieving venue inform at ion do not require secure access. The m et hod calls dealing wit h credit card t ransact ions, however, do require t he addit ional securit y provided by t he Digit al Cert ificat es. Since t he business m odel for gasTI X calls for a sm all num ber of client s consum ing t he gasTI X XML Web services, t his is an appropriat e securit y solut ion. I n our fict ional business m odel, we would require new client s wishing t o consum e our services t o obt ain and inst all a Digit al Cert ificat e on t heir web server. This allows t he gasTI X Web Services t o work wit h full confidence t hat only regist ered gasTI X consum ers will be able t o access t he secure credit card processing Web Services. I n order for t he websit e t o use cerficat es, we need t o first updat e t he web.config file. We will use inform at ion from t he Digit al Cert ificat e t o im personat e a user. I m personat ing a user m eans assum ing t he ident it y of a know account t hat has cert ain perm issions. Access t o dat a and resources is cont rolled by t his account . To m ake t his happen we set up t he web.config file t o use windows aut hent icat ion by adding t he following sect ion int o t he web.config file: List in g 8 .5 Th is list in g sh ow s a por t ion of t h e w e b.con fig file for ga sTI X.com u sin g ce r t ifica t e s.





Next , we need t o updat e t he global.asax file so t hat it will handle t he im personat ion. This is done wit h t he following code:



The im personat ion is done by t aking t he SUBJECTEMAI L propert y from t he client cert ificat e and using t hat t o im personat e a windows user. A GenericPrincipal obj ect is creat ed and st ored in t he User propert y of t he current Ht t pCont ext obj ect . We st ill have not m ade sure t hat t he user is allowed t o use t he websit e, but we

have creat ed a Principal, so we are able t o use t hat for t he aut horizat ion process, e.g. by using Role- based securit y m echanism s. Now we have set up t he applicat ion t o allow a Digit al Cert ificat e t o im personat e a Windows user. All we need t o do now is build a web page t hat displays som e inform at ion on t he user and inform at ion from t he client cert ificat e being passed in. I n t his scenario we will display t he nam e of t he im personat ed user, t he windows ident it y and t he issuer of t he cert ificat e in a sim ple .aspx page. The code for t his page is shown in list ing 8.4. ( Not e: For t he sake of sim plicit y, t he Visual Basic .NET code has been included in t he .aspx file.) List in g 8 .6 Th is sim ple .a spx pa ge displa ys t h e n a m e of t h e im pe r sona t e d u se r , t h e w in dow s ide n t it y, a n d t h e issu e r of t h e D igit a l Ce r t ifica t e .

< % @ I m port Nam espace= " Syst em .Securit y.Principal" % > < ht m l> < head> < script runat = " server" language= " vb" >

sub Page_Load( Src As Obj ect , e As Event Args) UserNam e.Text = User.I dent it y.Nam e ThreadUser.Text = WindowsI dent it y.Get Current ( ) .Nam e Cert I ssuer .Text

= Request .Client Cert ificat e.I ssuer

end sub

< / script > < / head> < body> < H3> Cert ificat es< / H3> < t able> < t r> < t d valign= " t op" align= " right " > User: < / t d> < t d valign= " t op" > < asp: label id= " UserNam e" runat = " server" / > < / t d> < / t r> < t r> < t d valign= " t op" align= " right " widt h= " 100px" > Thread I D: < / t d> < t d valign= " t op" > < asp: label id= " ThreadUser" runat = " server" / > < / t d>

< / t r> < t r> < t d valign= " t op" align= " right " > I ssuer: < / t d> < t d valign= " t op" > < asp: label id= " Cert I ssuer" runat = " server" / > < / t d> < / t r> < / t able> < / body> < / ht m l>

Authorization - working with Role-Based Security Role- based securit y is a m echanism t hat enables us t o adm inist rat ively const ruct an aut horizat ion policy for an applicat ion. Using role based securit y, we can define which users can access which resources, even down t o assigning access t o individual m et hods. As we have seen in t he gasTI X applicat ion, it is com m on t o apply different securit y rest rict ions at t he m et hod level. When we want t o allow people t o access our applicat ion we have t o assure t wo t hings: z

That the user is authenticated, i.e. the user is who he claims to be.

z

That the user is authorized, i.e. has sufficient permissions based upon his identity.

Take for exam ple t he com m on scenario of expense reim bursem ent . When an em ployee spends his own m oney for com pany purposes, he is ent it le t o be reim bursed for t hose expenses. To do so, t he em ployee m ust com plet e an expense report indicat ing t he am ount spent and t he purpose for t he expendit ure. An em ployee is able t o subm it his expense report s t hrough an applicat ion. A clerk in t he account ing depart m ent receives t he expense report and is only allowed t o approve expenses up t o a cert ain am ount . When t he am ount on an expense report exceeds t hat lim it , t he m anager of t he account ing depart m ent has t o approve t he report personally. Finally, t he CFO has t o forward t he approved report t o t he financial depart m ent t hat m akes t he paym ent . Looking at t he responsibilit ies and roles in t his scenario, we end up wit h t able 8.1. Table 8.1 Role - ba se d se cu r it y r e st r ict s a cce ss t o diffe r e n t a r e a s ba se d on t h e de fin e d r ole .

Role

Employees

Employee

X

Clerk

X

Accounting

X

Accounting Management

Executive

Manager

X

X

X

CFO

X

X

X

X

The t able shows t hat all em ployees are allowed t o subm it expense report s. However, t here are m ult iple roles t hat m ay be necessary t o process t he report . For any specific t ask, only a person in a role wit h perm issions for t hat t ask can com plet e it . Microsoft provides a role- based securit y infrast ruct ure wit h MTS and COM+ services. I n .NET t he role- based securit y infrast ruct ure is enhanced wit h added flexibilit y and ext ensibilit y t o m eet t he requirem ent s of m ore diverse, int egrat ed applicat ions. The .NET Fram ework allows t he opt ion of using role- based securit y on bot h server and client plat form s. Since t he role- based securit y in t he .NET Fram ework ext ends upon COM+ role- based securit y, it is useful t o com pare t he t wo securit y m odels. Rem em ber t hat a principal is a user or an agent t hat act s on a user's behalf. Wit h t hat in m ind, we can t ake a look at t he differences bet ween role- based securit y in COM+ and in t he .NET Fram ework. Table 5.2 shows t he com parison bet ween Com + and .Net . Table 5.2

Topic Principals

COM+

.NET Framework

Based on Windows NT

Not necessarily associated

Accounts and tokens

with Windows NT Accounts and tokens

Windows NT trust

Required

relationships

Optional, applications can require trust relationships, but there is not underlying assumption of trust in the framework

Management of roles

Per-application basis

Application specific roles can be defined. Possibility for run-time mapping between roles and user accounts

Application registration

Application code must be

No registration required,

registered with the

except for some

registration database

interoperability scenarios where COM and .NET work together

Mapping between user accounts and roles

Tracked by COM+

The runtime attaches a principal object to the call context, which is always available to the current thread. The principal object contains a reference to an

identity object as well as the roles to which the identity belongs.

The t able shows t hat t he .NET role based securit y m echanism s provide far m ore flexibilit y t han COM+ does. For exam ple, t he opt ion t o do run- t im e m apping of roles and responsibilit ies allows for less adm inist rat ive overhead. The .NET Fram ework provides a m echanism for checking role m em bership, WindowsPrincipal. The WindowsPrincipal reveals inform at ion about t he current user, t he aut horizat ion m echanism used for aut hent icat ion and t he dom ain. Exam ple of using WindowsPrincipal: AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsPrincipal user = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal; Console.WriteLine("User name: {0}", user.Identity.Name); Console.WriteLine("Authentication type: {0}", user.Identity.AuthenticationType); Console.WriteLine("Is in Administrators group: {0}", user.IsInRole(WindowsBuiltInRole.Administrator)); Console.WriteLine("Is in Guests group: {0}",

user.IsInRole(WindowsBuiltInRole.Guest));

The exam ple above shows how t o access t he WindowsPrincipal inform at ion and get som e inform at ion from it using t he WindowsBuilt I nRole enum erat ion. We can alt ernat ively replace t he fift h line above wit h t he following line of code t o accom plish t he sam e effect : Console.WriteLine("Is in Administrators group: {0}", user.IsInRole("BUILTIN\\Administrators"));

Mind t he correct spelling of t he groups; case- sensit ivit y is also very im port ant . I t is also possible t o check on ot her dom ain roles by replacing t he BUI LTI N keyword wit h t he nam e of t he dom ain t o check. For inst ance, t he adm inist rat ive funct ions of t he gasTI X sit e validat e against t he GASTI X\ \ Adm inist rat ors role. The second opt ion is t o use declarat ive securit y. Declarat ive securit y works by adding m et adat a about t he securit y rest rict ions direct ly t o t he assem bly. From a developm ent st andpoint , t his is accom plished by adding an at t ribut e t o a m et hod, as shown here: [PrincipalPermission(SecurityAction.Demand, Role=@"BUILTIN\Administrators")] private static string DoSomething() { return "Passed ..."; }

I n t his exam ple, t he DoSom et hing( ) m et hod has a securit y rest rict ion placed on it which st at es t hat t he principal m ust be assigned t o t he BUI LTI N\ Adm inist rat ors role in order t o execut e t he m et hod. Thus, t he m et hod is only accessible if t he user can im personat e t he request ed role. Tip: Remember that when using declarative security, t r y...ca t ch ...fin a lly constructions have to be used to catch security exceptions.

Working with Code Access Security Code- access securit y is a new m echanism provided by t he .NET Fram ework and designed t o com bat com m on securit y at t acks and abuses. The securit y is placed on t he code it self, rest rict ing bot h access t o t he code as well as t he funct ions t he code can perform . This helps ensure t hat t he code is secure whet her it is run direct ly by a user or indirect ly from anot her set of code. Securit y becom es m ore im port ant and m ore apparent when syst em s are being int egrat ed inst ead of being built from scrat ch. When an applicat ion uses code elem ent s t hat reside on different m achines and are downloaded upon request , we need t o be absolut ely sure t hat we are working wit h t rust ed code. Since code can reside anywhere, we need som e m echanism t hat confirm s t he ident it y of t he developer. We are able t o ident ify t he developer based on several elem ent s like a digit al signat ure and t he I P address of t he websit e where t he code originat ed. Using t he ident it y of t he developer we are able t o gain addit ional confidence t hat t he code is secure. But even when we can t rust t he code, since we know t he organizat ion or person t hat wrot e it , we m ight st ill want t o lim it execut ion and access perm issions. We also need t o be able t o verify t hat every caller in t he call st ack has every securit y right dem anded by t he code. Code Access Securit y relies on securit y policies t hat describe t he rules which cont ain t he perm issions grant ed t o t he code. Those rules are read and followed by t he com m on language runt im e ( CLR) . The beaut y of Code Access Securit y is t hat adm inist rat ors can set policies independent from developers. The developers, on t he ot her hand, can specify which perm issions are required for t he code t o run, which perm issions are desirable and which perm issions can be denied. Using Code Access Securit y reduces t he likelihood of execut ing m alicious code. An applicat ion can be given or denied cert ain perm issions. For exam ple, perm issions for accessing t he file syst em can be denied. Since perm issions are grant ed or denied on individual com ponent s, we m ust rem em ber t hat t here will likely be m ult iple set s of perm issions being applied t o any given applicat ion. When t he applicat ion calls code t hat needs addit ional perm issions, t he call st ack is checked t o see if, at every higher calling level, t his operat ion is allowed. This ensures t hat t he com ponent request ing access t o t he secured resource is always checked t o see whet her it has t he appropriat e right s. This elim inat es t he possibilit y t hat code wit hout t he appropriat e perm issions can gain access t o secured code m odules by using an int erm ediary code m odule. I f t he original caller is found t o have sufficient perm issions, t hen t he next caller in t he st ack is aut horized, and so on. I f any com ponent lacks t he necessary perm issions, access t o t he com ponent will be denied. All code t hat is execut ed wit hin t he CLR benefit s from t his code access securit y, even when it is not explicit ly st at ed. This m eans t hat developers do not need t o writ e anyt hing t o check t he securit y access of

com ponent s calling t heir code; t hey sim ply need t o check for except ions raised by t he securit y runt im e and handle t hem appropriat ely.

Signing Assemblies Code access securit y allows assem blies t o be signed by t he developer. This allows developers and ot her assem blies verify t he developer of t he original code. Signing an assem bly is done using t he signcode ut ilit y included wit h t he .NET Fram ework. To m ake t his work you need t o have cert ificat es already est ablished t hat you can use for signing. You can creat e a t est cert ificat e using t he m akecert ut ilit y, run it by t he cert 2spc ut ilit y and t hen use it wit h t he signcode ut ilit y. We can dem onst rat e t his process using an applicat ion incuded wit h t he SDK sam ples. I n t his case we will use t he WordCount exam ple. The WordCount sam ple applicat ion dem onst rat es how t o open files and count t he num ber of words cont ained wit hin. We will digit ally sign t he applicat ion t o prove it s aut hent icit y. Use t he following st eps t o creat e a t est cert ificat e: 1. Create the sample - Open a command prompt and go to the directory where the sample is located. When the .NET Framework SDK is installed at the default path it should be 'c:¥ pr ogr a m file s¥ m icr osoft .n e t ¥ fr a m e w or k sdk ¥ sa m ple s¥ a pplica t ion s¥ w or dcou n t '. 2. Build the sample using "N M AKE ALL". 3. Create a test certificate - Microsoft has a command-line tool that enables the creation of a test certificate. This certificate is for development purposes only, no deployed application should be signed using the certificate. 4. Create the certificate using 'MAKECERT -sk developer -n "CN=developer" developer.cer'. 5. Create a valid Software Publishers Certificate - The newly created test certificate is a X.509 certificate. When signing an assembly we need a valid Software Publishers Certifitate (SPC). This certificate is created by using the CERT2 SPC command-line tool. 6. Create the certificate using 'CERT2SPC developer.cer developer.spc'. 7. Check current trust - When an assembly has not been signed there is no way of determining where the code originated from. The origin can be verified using the CH KTRUST command-line tool. 8. Verify the trust using 'CHKTRUST wordcount.exe'. A security warning will pop up stating that the authenticode signature is not found. 9. Sign the assembly - Assembly signing is done using the SI GN COD E command-line tool. 10. Sign the assembly using 'SI GN COD E - spc de ve lope r .spc - k de ve lope r w or dcou n t .e x e '. We can ignore the message that the file has not been timestamped. 11. Re-check trust - Now that the assembly has been signed we should get a message containing information about the publisher and the assigned certificate. 12. Re-check trust using 'CH KTRUST w or dcou n t .e x e '.

The above st eps have dem onst rat ed how t o creat e a t est cert ificat e, convert it int o a Soft ware Publisher’s Cert ificat e, and sign an assem bly wit h it . We can following t hese sam e st eps for any assem bly we build for t est ing. For deploym ent , we will need t o obt ain an aut hent ic cert ificat e and use it t o sign our assem bly.

Code permissions and associated tools Code access securit y cont rols t he t ype of access a program has t o cert ain resources and operat ions. Code Access Securit y has been int roduced t o accom m odate t he new connect ed world where we can download pieces of code t hat will execut e on our own m achines. I t helps prot ect syst em s from m alicious code and allows for a safe execut ion environm ent of t his m obile code. We can gain t rust about t he aut hor by checking t he Digit al Cert ificat e, but t hat m ight not be enough. Even when we know t he ident it y of t he developer we m ight want t o prevent access t o cert ain resources, like t he file syst em . The .NET Fram ework allows us t o set code perm issions for all m anaged code assem blies. However, we can not set perm issions for unm anaged code. Unm anaged code can be called from m anaged code given t he right perm issions. Exam ples of t hose are invoking m et hods on COM+ applicat ions. This of course m eans t hat allowing access t o unm anaged code is a pot ential securit y risk which has t o be assessed very carefully. Wit hin .NET, t he code perm issions are cont rolled at runt im e. Code Access Securit y perform s several funct ions. These are: z

Define permissions and named permission sets that control the right to access system resources.

z

Enable system administrators to configure a security policy that associates the permission sets with groups of code.

z

Allow code to make a request for required permissions, useful permissions and off-limit permissions.

z

Grant permissions to loaded assemblies based on permissions requested by the code and the permissions granted by the security policy.

z

Enable code to demand that callers have certain permissions.

z

Enforce run-time restrictions on code by comparing the granted permissions of every caller with the required permissions of the code.

Run- t im e checking of perm issions is carried out based on a predefined pat t ern. When code want s t o access a resource, t he runt im e's securit y syst em walks t he call st ack t o see if t he dem anded perm issions are in com pliance wit h t he grant ed perm issions of each caller. I f one of t he callers does not have sufficient perm ission, a securit y except ion is t hrown. This m echanism is in place t o prevent less t rust ed code from accessing resources t hrough highly t rust ed code. This approach incurs addit ional overhead and a slight perform ance hit , but t he downside is even less at t ract ive. The .NET Fram ework provides som e com m and- line ut ilit ies t o work wit h perm issions and securit y. Adm inist rat ors and users can use t he caspol ut ilit y t o m odify a securit y policy for t he user policy level and t he m achine policy level. This t ool gives access t o t he grant ed perm issions of an assem bly and t hose code groups which t he assem bly belongs t o.

Note: The command-line utilities described in this section are installed by default with the .NET Framework. If, for some reason, they are not installed on your machine, simply re-run the .NET Framework installation and make sure you have selected the samples for installation.

The perm view ut ilit y allows us t o look at t he perm issions t he assem bly requires. Not e t hat required perm issions are different t han t he grant ed perm issions. The .NET Fram ework has a good sam ple relat ed t o code perm issions. The sam ple is called Perm Request .cs and dem onst rat es how an assem bly defines which perm issions are request ed, which perm issions are opt ional and which perm issions are refused. The sam ple uses declarat ive securit y: [assembly:EnvironmentPermission(SecurityAction.RequestMinimum, Read="PROCESSOR_IDENTIFIER;PROCESSOR_REVISION;NUMBER_OF_PROCESSORS;OS")] [assembly:EnvironmentPermission(SecurityAction.RequestOptional, Unrestricted=true)] [assembly:EnvironmentPermission(SecurityAction.RequestRefuse, Read="USERNAME;USERDOMAIN;USERDNSDOMAIN;COMPUTERNAME;LOGONSERVER", Write="USERNAME;USERDOMAIN;USERDNSDOMAIN;COMPUTERNAME;LOGONSERVER")]

From Here

TE

AM FL Y

The .NET Fram ework is designed t o ut ilize eit her role- or evidence- based securit y inherent ly. This allows developers t o writ e m ore secure code wit h less work. The Fram ework also ships wit h several ut ilit ies and exam ples which m ake building and enforcing securit y policies easy t o underst and and im plem ent .

Team-Fly®

9 COM+ Integration I n t he previous chapt ers we have seen how .NET is about t o reshape t he process of soft ware developm ent . However, com panies have m ade ext ensive invest m ent s in previous t echnologies, including Microsoft 's COM+ t echnology. I n m any cases, it will not m ake sense t o m ove previously developed COM+ com ponent s t o .NET, at least not init ially. As a result , t here will be a need t o int egrat e newly developed .NET com ponent s wit h COM+ com ponent s. This chapt er focuses on t ips and t echniques for m aking t hat happen.

A Legacy from the Past Those of us, who have been around long enough t o wit ness ot her 'm aj or' changes in soft ware developm ent , can relat e t o so- called legacy syst em s. 'Legacy' norm ally refers t o any syst em or product not considered t o be st at e of t he art . This does not necessarily m ean such syst em s are not being used anym ore. I t does not even have t o m ean everybody agrees t hat t hese syst em s are no longer st at e of t he art .

The Need for Legacy Components There are several reasons why legacy syst em s survive. I n t he next t hree paragraphs we will look at t he t hree m aj or reasons. z

Momentum, also known as Installed Base-Replacing a legacy system by a new system takes both time and energy. Often, the energy that was put into building the system keeps it moving forward in its current state for some time to come. The more momentum the system has, the more time or energy it will take to make it change course.

z

The Black Box-One of the advantages of component-based development is a bst r a ct ion . Developers can hide implementation details from other developers using their components and only expose certain properties, methods and/or events to them. Developers using the component can treat it like a bla ck box . If the black box was designed correctly, then there may not be a major reason for updating the software. It is necessary in these cases that newly developed applications be able to adequately interface with those boxes.

z

The Cost of Change-Rewriting existing software can be expensive in terms of both time and money. Of course, n ot rewriting software may be expensive as well. In the end, it comes down to cost versus benefits, and that analysis may determine it is best to leave the application "as is."

To Migrate or Not to Migrate There are m any t hings t o consider when we look at t he possibilit y of m igrat ing an exist ing applicat ion t o t he .NET plat form . Let 's have a look at a t ypical, but hypot het ical, sit uat ion, built around t he COM+ int erface. COM+ has been t he st andard int erface for building n- t ier applicat ions based on various incarnat ions of t he Windows operat ing syst em . The COM+ int erface and n- t ier archit ect ure are beyond t he focus of t his book. We will assum e t he reader is fam iliar wit h bot h concept s. Figure 9.1 shows a Win32 applicat ion called m yApp.exe. The applicat ion calls int o one of t he COM+ int erfaces exposed by m yServer.dll. A second COM+ com ponent ( m yClient .dll) calls anot her int erface of m yServer.dll.

Figure 9.1 A sam ple pre- .NET sit uat ion, based on t he COM+ int erface

I f we look at t he int erfaces bet ween t hese com ponent s, t here are always t wo part ies involved: t he client process t hat is m aking t he call ( eit her m yApp.exe or m yClient .dll) and t he server process t hat is receiving t he call ( m yServer.dll) . This does not m ean t hat a com ponent 's role is fixed. On t he cont rary- As can be seen in Figure 9.1, m yClient .dll exposes a num ber of int erfaces it self. I t is not unt hinkable t hat anot her EXE or DLL will call m yClient .dll and t reat it as a server com ponent , while it act s like a client com ponent t o m yServer.dll. I n Figure 9.1, bot h sides of t he COM+ int erface cont ain so- called nat ive code. This is code t hat is com piled t o run direct ly on t he t arget syst em 's processor. I n m any cases, t his will be code t hat is m eant t o run on a 32- bit Windows plat form using an x86 t ype of processor. I f we look at t his sit uat ion from a .NET perspect ive, t he sit uat ion t hat would benefit m ost from t he advant ages of t he .NET plat form would be t o m igrat e bot h sides of t he int erface t o run inside t he Com m on Language Runt im e ( or CLR for short ) , since t his would int roduce as lit t le overhead as possible while st ill m aking use of t he .NET plat form . This would require m yApp.exe, m yClient .dll and m yServer.dll t o be port ed t o t he .NET plat form . We refer t o t his sit uat ion as a " full port ." Obviously, convert ing every single part of an applicat ion can be a lot of work, especially if t he source language for t he original applicat ion cannot be recom piled as part of t he .NET plat form wit hout m aking changes t o it s synt ax or t o it s use of library funct ions. Note: Visual Basic 6 is one of the most commonly used languages for the creation of COM+ components. With the introduction of Visual Basic.NET, many new features have been added to the Visual Basic language that were not part of Visual Basic 6. Visual Basic.NET is a fully featured programming language for the .NET platform that can compete with other languages such as Visual C++.NET and Visual C#.NET. One consequence of the changes and additions that have been made to the Visual Basic language and syntax will cause some

Visual Basic 6 sources to produce syntax errors when compiled with the Visual Basic.NET compiler. When making the transition, at least some Visual Basic 6 sources will have to rewritten.Documentation regarding the differences between Visual Basic 6 and Visual Basic.NET can be found on Microsoft's MSDN web-site. For Visual Basic 6 developers, Craig Utley's book "A Programmer's Introduction to Visual Basic.NET" describes the differences and points out ways to convert Visual Basic 6 code to Visual Basic.NET. It can be downloaded from the SAMS Publishing web-site.

I f t he decision t o port an applicat ion t o t he .NET plat form were an " all- or- not hing" t ype of choice, we would be facing a t ough challenge for t he next couple of years. I n t he previous paragraphs we have already seen t hat m any fact ors m ust be considered when m aking t his choice. To m ake t he t ransit ion t o .NET go as sm oot hly as possible, Microsoft has added support for calling COM+ com ponent s from wit hin t he CLR. This is not j ust a nice feat ure for developers; it 's an act ual m ust - have for t he plat form . Because t he init ial release of t he .NET Fram ework will be based on Microsoft 's 32- bit Windows plat form s, t he operat ing syst em it self will st ill consist of 32- bit " nat ive" code. This m eans t hat .NET assem blies will frequent ly have t o call code t hat resides out side of t he CLR. I t won't be unt il Microsoft releases Windows.NET furt her down t he road, t hat we will see t he first exam ple of a com plet ely .NET- based plat form . Unt il t hen, m ost solut ions will be hybrid syst em s t hat use bot h .NET assem blies and COM+ DLLs. Because support for COM+ int erfaces is built int o t he .NET plat form , applicat ions can be a m ixt ure of Win32 code m odules and .NET assem blies. Not only can assem blies call COM+ com ponent s, but nat ive code can call .NET assem blies as well. This m eans t hat we have m uch m ore freedom when deciding which part s of our original applicat ion will be port ed t o t he .NET plat form . When we m igrat e only part of an applicat ion, we refer t o t his sit uat ion as a 'Part ial port '. Table 9.1 shows t he possible choices when planning t he m igrat ion of a client - server int erface t o t he .NET plat form . Ta ble 9 .1 . Possible ch oice s w h e n m igr a t in g a COM + solu t ion

COM+ CLR

Client

Server

COM+

CLR

Original situation

Partial port

Partial port

Full port

This leaves us wit h t hree possible choices t o consider: z

No port (original situation) „

Of course choosing not to migrate an existing application to the .NET platform is as much a choice as any other. As we have seen in the previous paragraphs, sometimes migrating is n ot the best choice we can make. There may be technical or financial reasons that dictate us to follow another course of action. We will, however, explore the creation of enterprise solutions using .NET in the remaining chapters of this book.

z

Full port to Common Language Runtime „

The other chapters of this book describe how to build an e-Commerce solution from scratch by leveraging the many features of the .NET platform. Using the techniques described in these chapters, we can also take an existing application and give it the full .NET treatment. This may or may not involve using the existing source code of our original application. In some cases starting from scratch based on the original case study (or an updated revision) yields certain advantages over line-by-line conversion of the existing code. If this is the case, then COM+ integration is not what we're looking for and we may as well proceed directly to chapter 10 "Event Searching".

z

Partial port „

In situations where we want to maintain a COM+ client or server component as part of our solution while still integrating with a .NET assembly, a number of techniques are important. Most are existing techniques that have been re-tooled to work with .NET. There are also some new techniques that are only available within the .NET Framework. The remainder of this chapter will focus on what it takes to make .NET integrate with COM+ interfaces. We will explore the integration of COM+ legacy components with .NET assemblies such as Web Services and Class Libraries. We will also look at how .NET Class Libraries can be called from a Win32 application.

A highly det ailed discussion of int egrat ing .NET wit h COM+ is beyond t he scope of t his chapt er. However, we will discuss how t o efficient ly leverage our exist ing code base in order t o m inim ize t he am ount of effort needed t o reap t he benefit s of int egrat ing wit h .NET.

A Sample Architecture Since gasTI X is an all- new business case, it doesn't cont ain any legacy com ponent s. This is an advant age in t hat t he result ing applicat ion will run m ore efficient ly and can use all of t he excit ing new feat ures of t he .NET Fram ework. The downside is t hat we will have t o sidest ep t he gasTI X business case t o dem onst rat e int egrat ion wit h COM+ . For t his purpose we will look at a sim ple hypot het ical sit uat ion. The archit ect ure for t his exam ple is shown in Figure 9.2. The legacy code is an uncom plicat ed COM+ DLL called gasTim e.dll. I t can be used t o det erm ine t he current t im e on t he server plat form and will be im plem ent ed using Visual Basic 6. We will m ake t his DLL available t o t wo assem blies: gasWebService.asm x and gasClass.dll. These represent a .NET Web Service and a .NET Class Library respect ively and bot h run inside t he Com m on Language Runt im e. Let 's j ust ignore t he gasRCW.dll for t he m om ent . I t s purpose will be explained lat er in t his chapt er.

Figure 9.2 The COM+ int egrat ion exam ple discussed in t his chapt er m akes use of a COM+ DLL, t wo .NET assem blies, and a .NET Web Service.

We will t est t he Web Service by querying it using Microsoft I nt ernet Explorer. Aft er t est ing t he Web Service we will writ e a Visual Basic 6 deskt op applicat ion t o t est t he Class Library. Since t he Class Library is act ually j ust a wrapper DLL, it will call our original VB6 DLL t o ret urn t he t im e. The result will be t wo pieces of nat ive code and t wo pieces of m anaged code ( gasRCW.dll com es int o play as well) co- exist ing wit hin a single applicat ion and being able t o call one anot her across t he CLR boundary. Note: Since Web Services utilize industry standard communication protocols, the Web Service can be tested in any HTTP-compliant browser.

We will have a closer look at t his exam ple in t he det ailed walkt hrough lat er in t his chapt er. However, before we are ready t o st art working on t he sam ple applicat ion, we should fam iliarize ourselves wit h t he t echnologies involved in COM+ int egrat ion.

Technology Overview I n t his sect ion, we look at t he t echnologies involved in COM+ int egrat ion. First we quickly brush up on assem blies and t he Com m on Language Runt im e. Then we look at various ways in which m anaged and unm anaged code can coexist wit hin a single applicat ion.

Managed versus Unmanaged Code All code creat ed using Visual St udio .NET is com piled int o one or m ore assem blies. An assem bly is a collect ion of one or m ore files ( .EXE, .DLL, resource files, and so on) t hat im plem ent a logical applicat ion or com ponent . Assem blies are not t arget ed t owards a part icular processor or operat ing syst em . They consist of MSI L ( Micr osoft I nt erm ediat e Language) inst ruct ions inst ead of nat ive code ( a.k.a. m achine language) . Assem blies also cont ain m et adat a t hat describes t he cont ent s of t he assem bly. We m ight say t hat assem blies are t he .NET count erpart of Win32 execut ables.

As a result , assem blies require t he .NET Fram ework in order t o run. The .NET Fram ework perform s t ask s such as com pilat ion t o t he nat ive code form at , garbage collect ion, loading and unloading of classes and perform ing securit y checks. That 's why we say MSI L program s don't j ust run: They are m anaged. The m odule t hat perform s all t hese t asks is called t he Com m on Language Runt im e. We call t he program s t hat run " inside" t he CLR m anaged code. COM+ com ponent s, on t he ot her hand, are unm anaged code because t hey run direct ly on t he plat form 's CPU, wit hout t he int ervent ion of t he CLR. Unm anaged code is t ypically t arget ed t owards a fixed range of processors and operat ing syst em s. I f we com pile our unm anaged applicat ion for t he st andard Windows environm ent , it will run on x86 CPUs and, m ost likely, any 32- bit version of Windows. Users wit h anot her com binat ion of processor and operat ing syst em , say a 68000 processor running Palm OS, are out of luck. As we have seen before, it is essent ial t hat applicat ions be able t o cont ain a m ixt ure of m anaged and unm anaged m odules. Som e of t hese unm anaged m odules m ay be syst em DLLs like user32.dll or kernel32.dll. Ot her unm anaged m odules could be COM+ com ponent s. But how can we bridge t he gap bet ween m anaged and unm anaged code? Managed code can't run direct ly on t he plat form 's CPU because it is not in nat ive form at , and unm anaged code can't run inside t he CLR because it does not cont ain t he m et adat a required by t he CLR. The answer t o t his quest ion is .NET I nt eroperabilit y Services. The .NET Fram ework ships wit h a nam espace called Syst em .Runt im e.I nt eropServices. This nam espace cont ains all t he services required for running applicat ions across t he CLR boundary. A det ailed descript ion of I nt eropServices is beyond t he scope of t his chapt er, but we will explore som e of t he basic t echniques here.

Calling the Operating System Since calling nat ive Win32 DLLs from wit hin t he CLR is likely t o happen frequent ly, I nt eropServices provides a special m echanism for t his purpose. This m echanism is called Plat form I nvoke, or P/ I nvoke for short . P/ I nvoke allows us t o define a CLR wrapper around an exist ing DLL's m et hods. Let 's say for exam ple t hat we would like t o invoke t he MessageBox m et hod of t he user32 syst em DLL from a Visual Basic.NET program . First we would need t o t ell Visual Basic.NET t hat we want t o use I nt eropServices. We do t his by including t he following line of code: Imports System.Runtime.InteropServices

This t ells Visual Basic.NET t o im port t he Syst em .Runt im e.I nt eropServices nam espace so t hat we m ay easily refer t o it s classes in our code. Next we define a class called Win32 ( or any ot her nam e t hat we see fit ) and add t he following code: Public Class Win32 Declare Auto Function MessageBox Lib "user32.dll" (Byval hWnd As Integer, _ Byval txt As String, Byval caption As String, Byval Type As Integer) _ As Integer End Class

Once t his class has been added t o our proj ect , we can sim ply invoke t he MessageBox funct ion of t he class. A MessageBox will now appear when we execut e t he following code:

Win32.MessageBox(0, "Hello World", "gasTIX Platform Invoke Sample", 0)

I t will have " gasTI X Plat form I nvoke Sam ple" as it s t it le, will display t he t ext " Hello World" and will only have an " OK" but t on. Of course, since displaying m essage boxes is an inherent part of t he Visual Basic.NET language as well, we could have replaced all of t he preceding code wit h t he following sim plified code: MsgBox "Hello World ", 0, "gasTIX Platform Invoke Sample"

Our developm ent experience t ells us it would probably have been a lot m ore efficient as well. But why? Our MessageBox funct ion t ells t he Visual Basic.NET com piler which param et ers t o expect . That inform at ion is im port ant , because t he com piler needs t he inform at ion t o creat e m et adat a for t he CLR. However, our declarat ion doesn't t ell t he CLR what code should be execut ed. There is no body for t he MessageBox funct ion. So what happens when we call it from our m anaged code? Our declarat ion does not t ell t he CLR what t he funct ion is supposed t o do, but it does indicat e where t he funct ion can be found. First , I nt eropServices locat es t he DLL cont aining t he funct ion as defined by our Declare st at em ent and loads it int o m em ory. When t he DLL has been loaded, I nt eropServices places our argum ent s ont o t he Call St ack and t ransfers cont rol t o t he unm anaged funct ion inside t he nat ive DLL. The unm anaged funct ion t hen execut es, possibly changing t he cont ent s of t he Call St ack while it does, and t ransfers cont rol back t o I nt eropServices. Here, any result s are collect ed from t he Call St ack and passed back t o our m anaged code. Those fam iliar wit h COM+ will not e t hat t his m echanism is sim ilar t o t he way in which COM+ handles rem ot e procedure calls. The m echanism of placing argum ent s on a Call St ack is called m arshalling and is not unique t o t he .NET plat form . What P/ I nvoke does is creat e a proxy class t o handle t he m arshalling for us. We will look int o t his m echanism in m ore det ail lat er in t his sect ion. Note: Each time we pass arguments and function results in or out of the CLR, data needs to be marshaled across the Call Stack. In addition, a piece of managed code will be created to handle the marshalling and to pass control to native code. The overhead required to perform this marshalling will incur a performance penalty based on the type and amount of data being marshaled. Using a managed code equivalent in place of native DLL's wherever possible will typically offer better application performance.

Wrapping it All Up. Now t hat we have seen t hat t he .NET plat form can wrap a proxy class around a nat ive DLL, how does it work? Whenever a nat ive COM+ obj ect needs t o be called, t he CLR creat es a Runt im e- Callable Wrapper ( RCW) for t hat obj ect . Only one RCW needs t o be creat ed for any COM+ obj ect . The CLR keeps t rack of t he num ber of references m ade t o t he RCW. The RCW act s j ust like any m anaged piece of code t o t he CLR, but exposes a copy of t he COM+ obj ect 's int erfaces t o ot her classes. The wrapper class ( including it s int erfaces) is creat ed based on t he t ype library associat ed wit h t he COM+ obj ect .The m ain purpose of t he RCW is t o invoke t he COM+ obj ect and m arshal dat a bet ween m anaged and unm anaged code based on t he obj ect 's int erface definit ion. Since t he .NET t ypes associat ed wit h t he int erface m ay differ from t hose used in t he COM+ com ponent , som e conversion m ay be required. We will address t his m echanism in m ore det ail in t he 'Advanced Topics' sect ion lat er in t his chapt er. I f t he COM+ obj ect changes any by- reference argum ent s or ret urns a result , t hese values will have t o be m arshaled back t o t he CLR in t he sam e way. Once all

references t o t he RCW have been released, t he RCW releases it s reference t o t he COM+ obj ect . The CLR can t hen perform a garbage collect ion on t he RCW. Figure 9.3 shows a diagram depict ing t he RCW.

Figure 9.3 The RCW and CCW act as proxies across t he CLR boundary

The reverse process ( calling m anaged code from nat ive code) works in m uch t he sam e way. I n t his case a COM- Callable Wrapper ( CCW) is creat ed. Only one CCW is creat ed for any .NET obj ect . The CCW is reference- count ed like any ot her COM+ obj ect so it can be discarded once all references have been released. The CCW holds j ust one reference t o it s corresponding .NET obj ect . The CCW exposes COM+ copies of t he .NET obj ect 's int erfaces t o nat ive code and m arshals argum ent s and result s in t he sam e way t he RCW does, only in t he opposit e direct ion. The CCW is also shown in Figure 9.3. Being able t o call obj ect s at run- t im e regardless of on which side of t he CLR boundary t hey are locat ed is great , but it 's not m uch use at developm ent t im e. At developm ent t im e, we'd like t o use early binding t o reduce t he possibilit y of run- t im e com pat ibilit y errors. Developers would also like t o use Microsoft 's I nt elliSense feat ures t o show propert y and m et hod nam es and offer code com plet ion. This should be available not j ust for COM+ com ponent s in Visual St udio 6 and .NET assem blies in Visual St udio .NET, but for any com binat ion of developm ent environm ent and com ponent t ype. When we look at t he t ype of inform at ion t hat bot h versions of Visual St udio use t o describe int erfaces, we are faced wit h a problem . Visual St udio 6 uses t ype libraries, while Visual St udio .NET inspect s t he m et adat a in an assem bly. What we would need is som e way of t ranslat ing bet ween t he t wo. Fort unat ely, Microsoft com es t o our aid by shipping a num ber of very useful t ools wit h Visual St udio .NET. These t ools include t he Type Library I m port er Ut ilit y ( TlbI m p) and t he Type Library Export er Ut ilit y ( TlbExp) . TlbI m p t akes a t ype library or COM+ m odule as argum ent and produces a .NET DLL cont aining CLR equivalent s of t he m odule's int erfaces, including t he required m et adat a. TlbExp does exact ly t he opposit e: it t akes a .NET assem bly and export s a COM+ com pliant t ype library based on t he m et adat a in t he assem bly. We will use bot h t ools in building t his chapt er's sam ple applicat ion.

Tip: TlbImp will not expose standard interfaces like IUnknown and IDispatch, but will map a COM+ component's interfaces onto a .NET wrapper DLL. The resulting DLL contains all the required metadata we need for setting references in Visual Studio .NET. In the detailed walkthrough later in this chapter, we will look at converting a COM+ DLL using TlbImp and using it in Visual Studio .NET to reference our original COM+ object.

Note: Although TlbImp and TlbExp can save us a lot of work by automatically converting between metadata and type libraries or vice versa, there are some situations in which we need more control over the marshalling process. In these cases we need to translate the interfaces by hand. In the "Advanced Topics" section later in this chapter, we will look at the mechanisms involved in marshalling data across the CLR boundary. We will also discuss methods to control the data types used in marshalling.

There is one m ore t ool t hat needs t o be m ent ioned when using wrapper funct ions t o st raddle t he CLR boundary. I n order t o call .NET assem blies t hrough t he COM+ int erface, COM+ m ust be able t o locat e t he assem bly and it s int erfaces. Tradit ionally we m ake COM+ com ponent s available by regist ering t hem using t he regsvr32.exe ut ilit y t hat ships wit h Windows. Since t his ut ilit y expect s a com ponent wit h a COM+ com pliant int erface, it is not m uch use when t rying t o regist er .NET assem blies. Fort unat ely, Microsoft has included t he .NET Assem bly Regist rat ion Ut ilit y ( or REGASM) wit h Visual St udio .NET. REGASM t akes an assem bly and regist ers it as if it were a COM+ com ponent . This will allow t he assem bly t o be called t hrough a CCW like any " real" COM+ com ponent . We will look at REGASM in m ore det ail at t he end of t his chapt er.

Approaches to Partial Ports We have now exam ined t he m echanism s involved in calling obj ect s across t he CLR boundary. Now we can exam ine t he various ways of em ploying t hese m echanism s in order t o achieve a part ial port of a legacy applicat ion. Partial Port-RCW Approach I n t his approach, we have chosen t o convert only t he client port ion of t he applicat ion. As shown in Figure 9.4, m yClient .dll has been convert ed t o m yNewClient .dll. Since m yNewClient .dll consist s of m anaged code, it needs an RCW t o com m unicat e wit h m yServer.dll, which st ill consist s of nat ive code. We can use TlbI m p t o aut om at ically generat e m yServerRCW.dll based on m yServer.dll or t ranslat e t he int erface by hand. The advant age of t his approach would be t hat ot her nat ive com ponent s ( for exam ple, m yApp.exe) could st ill call m yServer.dll wit hout any changes.

Figure 9.4 When only m yClient .dll is convert ed t o m anaged code, we need a RCW t o allow access t o m yServer.dll.

Partial Port-Quick Approach

TE

AM FL Y

I nst ead of convert ing m yClient .dll t o m anaged code by hand, t here is a m uch sim pler approach. We can use TlbI m p t o generat e an RCW for m yClient .dll ( called m yClient RCW.dll in Figure 9.5) and leave all ot her code unchanged. Ot her pieces of m anaged code can t hen call upon t he int erfaces of m yClient RCW.dll j ust as if it was m yClient .dll it self. However, t his approach only works wit h nat ive code for which a t ype library exist s. This m eans t hat st andard EXEs ( like m yApp.exe in t he exam ple) cannot be convert ed t his way.

Figure 9.5 I n t he " quick" approach, no source code needs t o be convert ed and t he RCW for m yClient .dll can be generat ed aut om at ically by TlbI m p.

Team-Fly®

Partial Port-CCW Approach The t hird opt ion t o explore in t his exam ple is convert ing m yServer.dll t o m anaged code and leaving t he client com ponent s unchanged. Here we t ake m yServer.dll and rewrit e it t o m anaged code t hat can run inside t he CLR. Since our client soft ware st ill consist s of nat ive code, we need a CCW t o m arshal dat a back and fort h t o our new server com ponent . This solut ion is shown in Figure 9.6.

Figure 9.6 When only m yServer.dll is convert ed t o t he CLR, we need a CCW t o be able t o call it from nat ive code.

Performance of Partial Ports An im port ant fact or when considering t he various approaches is t heir perform ance when com pared t o t he original COM+ solut ion and t o a full port . For t his purpose, let 's consider t he sit uat ion were m yServer.dll would cont ain a class called Server and were we would add t he following t wo funct ions t o t his class: Public Function DoubleLoop(ByVal value As Double) As Double Dim result As Double Dim counter As Long

result = value

For counter = 0 To 2000000 result = result + value result = result - value Next counter

DoubleLoop = result End Function

Public Function IntLoop(ByVal value As Integer) As Integer Dim result As Integer Dim counter As Long

result = value

For counter = 0 To 2000000 result = result + value result = result - value Next counter

IntLoop = result End Function

We could call t hese funct ions from m yClient .dll using code like t his: Public Sub runTest(ByVal iterations As Integer) Dim myObject As myServer.Server Dim counter As Long Dim doubleResult As Double Dim intResult As Integer

For counter = 1 To iterations Set myObject = New myServer.Server doubleResult = myObject.DoubleLoop(CDbl(counter)) Set myObject = Nothing Next counter

For counter = 1 To iterations Set myObject = New myServer.Server intResult = myObject.IntLoop(CInt(counter)) Set myObject = Nothing Next counter End Function

We could t hen call m yClient .dll from a t est program t hat would m easure t he t im e required t o run runTest wit h a cert ain num ber of it erat ions. Using t he t echniques described in t he det ailed walkt rough lat er in t his chapt er, we can produce an " original" Visual Basic 6 version and a full port , as well as a RCW, " Quick" and CCW version of t his set up. Alt hough t he act ual perform ance of any given solut ion will depend m uch on t he code involved, it will give us som e rules of t hum b regarding t he perform ance of t he different approaches. Since any .NET assem bly needs t o be convert ed t o nat ive code prior t o being execut ed, we m ight argue t hat t his will inflict a loss on perform ance com pared t o t he original Win32 applicat ion. Even t hough t he CLR's Just - I n- Tim e ( JI T) com piler m ay be able t o use opt im izat ions based on t he act ual processor and plat form , while a nat ive code com piler has t o set t le for t he least com m on denom inat or, we see a 10 t o 15% overall drop in perform ance for a first run of t he t est code described above. Exact num bers differ for t he t ype of param et ers being used. Because Microsoft 's JI T com piler uses a clever caching m echanism , code only needs t o be t ranslat ed on it s first execut ion. This result s in subsequent calls of our t est code being execut ed 5 t o 20% fast er t han on t heir first call. As a result , we have verified t hat cert ain funct ions like DoubleLoop m ay indeed out perform t heir nat ive code versions once t he JI T com piler has done it s j ob.

When looking at t he differences in perform ance bet ween t he part ial port approaches and t he full port approach, we expect a drop in perform ance because addit ional wrapper classes have t o be added and execut ed. Our benchm arks seem t o verify t his. CCW, RCW and "Quick" approaches decrease perform ance for our t est code by approxim at ely 28% , 36% and 39% respect ively when com pared t o a full port . I t is int erest ing t o not e t hat t he perform ance of t he " Quick" version is only slight ly lower t han t hat of t he RCW approach. The CCW t ypically out perform ed bot h in our t est s, probably because it is based on m et adat a t hat cont ains m ore det ailed inform at ion t han a t ype library and can use m ore efficient m arshalling code as a result . Tip: As far as performance is concerned, whenever we need to convert at least part of our application to the .NET platform, the full port approach is preferable. If performance is not a major issue, we might settle for a simple and convenient "Quick" approach and save ourselves a lot of work. Of course these are only rules of thumb, since different scenarios may outperform others in different situations. Because the "Quick" approach is very simple to implement, we should consider using it as a performance test case whenever performance is an issue.

Advanced Topics Now t hat we have seen in which sit uat ions m arshalling is required, we will look at what goes on inside t he RCW and CCW. We will also look at ways t o cont rol t he dat a t ypes used in m arshalling.

Typed Transitions and the Stack Wit h t he int roduct ion of Visual Basic.NET, several changes have been m ade t o t he Visual Basic program m ing language. Since t he Visual Basic.NET com piler generat es m anaged code t hat is t arget ed t owards t he .NET plat form , all dat a t ypes m ust m ap ont o one of t he CLR dat a t ypes. Table 9.2 shows t he dat a t ypes available in Visual Basic.NET, along wit h t heir value ranges, st orage size ( in byt es) and t he CLR dat a t ype t hey m ap unt o. Table 9.3 list s t he dat a t ypes available in Visual Basic 6 along wit h t heir value ranges and st orage sizes. Ta ble 9 .2 Ove r vie w of Visu a l Ba sic.N ET da t a t ype s

Visual

Value Range

Storage

Basic.NET

Common Language Runtime

Isomorphic /

Type

Nonisomorph

Size

Data Type

ic

Boolean

True / False

2

Syst em .Boolean

NI

Byt e

0 t o 255

1

Syst em .Byt e

I

Char

0 t o 65535

2

Syst em .Char

NI

Dat e

January 1, 0001 t o Decem ber 31, 9999

8

Syst em .Dat eTim e

NI

Decim al

+ / - 79,228,162,514,264,337,593,543,950,335 w it h no decim al

16

Syst em .Decim al

NI

8

Syst em .Double

I

point ; + / - 7.9228162514264337593543950335 w it h 28 places t o t he right of t he decim al; sm allest non- zero num ber is + / - 0.0000000000000000000000000001

Double

- 1.79769313486231E308 t o - 4.94065645841247E- 324 for negat ive values; 4.94065645841247E- 324 t o 1.79769313486232E308 for posit ive values

I nt eger

- 2,147,483,648 t o 2,147,483,647

4

Syst em .I nt 32

I

Long

- 9,223,372,036,854,775,808 t o 9,223,372,036,854,775,807

8

Syst em .I nt 64

I

Obj ect

Various

4

Syst em .Obj ect

NI

Short

- 32,768 t o 32,767

2

Syst em .I nt 16

I

Single

- 3.402823E38 t o - 1.401298E- 45 for negat ive v alues;

4

Syst em .Single

I

1.401298E- 45 t o 3.402823E38 for posit ive values

St ring

0 t o approx im at ely 2 billion Unicode charact ers

Various

Syst em .St ring

NI

User-

Various

Various

I nherit s from Syst em .ValueType

NI

Various

Various

Syst em .Array

I

Various

Various

Syst em .Array

NI

Defined Type

Single dim ensiona l array of isom orphic t ypes

All ot her arrays

I f we com pare t he t wo t ables we m ay not ice a num ber of t hings. Som e dat a t ypes in one version of Visual Basic have no corresponding t ype in t he ot her. Also, t he st orage size for a num ber of dat a t ypes is different . For exam ple, a Visual Basic.NET I nt eger is equivalent t o a Visual Basic 6 Long, not t o a Visual Basic 6 I nt eger. This is not j ust a Visual Basic problem , but is affect s ot her .NET languages as well. Not all dat a t ypes used on t he .NET plat form have equivalent dat a t ypes in t he world of COM+ . However, when we look closely at t he t able, we can see t hat som e .NET dat a t ypes can be m apped direct ly ont o COM+ dat a t ypes and vice versa. We call t hese t ypes isom orphic or blit t able dat a t ypes. They can be m arshaled from one side of t he int erface t o anot her wit hout act ual dat a conversion. The t ypes t hat need conversion are called nonisom orphic or non- blit t able. Table 9.2 shows which Visual Basic.NET dat a t ypes are isom orphic and which dat a t ypes are not .

Ta ble 9 .3 Ove r vie w of Visu a l Ba sic 6 da t a t ype s

Visual Basic 6 Data Type

Value Range

Boolean

True / False

2

Byte

0 to 255

1

Currency

-922,337,203,685,477.5808 to 922,337,203,685,477.5807

8

Date

January 1, 100 to December 31, 9999

8

Decimal

+/-79,228,162,514,264,337,593,543,950,335 with no decimal

Storage Size

14

point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001 Double

-1.79769313486231E308 to

8

-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values Integer

-32,768 to 32,767

2

Long

-2,147,483,648 to 2,147,483,647

4

Single

-3.402823E38 to -1.401298E-45 for negative values;

4

1.401298E-45 to 3.402823E38 for positive values String

0 to approximately 65,400 characters

Various

UDT

Various

Various

Variant

Various

Various

Object

Various

4

Each inst ance of a RCW or CCW m aint ains t wo copies of t he Call St ack: one for t he m anaged side and one for t he unm anaged side. To m arshal dat a from one side t o t he ot her, dat a is copied from one Call St ack t o t he ot her. I f t he dat a t ype is isom orphic, it can sim ply be copied byt e- by- byt e ( hence t he nam e blit t able) . I f t he dat a t ype is nonisom orphic, it will have t o be convert ed in t he process.

Tip: It shouldn't come as a surprise that the overhead involved in marshalling is proportional to the amount of work required to convert arguments and results from one data type to another. Therefore, if we choose isomorphic types wherever we can, we may significantly increase the performance of our application.

Controlling the Marshalling Mechanism Based on t he dat a available from m et adat a or a t ype library, t he CLR will choose a default dat a t ype for m arshalling. While t his will work in m ost sit uat ions, som et im es we want t o pick t he dat a t ype by hand. I n t his case we can't use TlbI m p and TlbExp t o creat e t he wrapper class for us. On t he ot her hand we have com plet e cont rol over t he way dat a is being m arshaled across t he Call St ack. Let 's look at t he wrapper funct ion of t he list ing shown earlier in t his chapt er under " Calling t he Operat ing Syst em " again. The t xt argum ent is defined as St ring in our funct ion definit ion. I f we want t o m ake sure t his argum ent is m arshaled as a part icular unm anaged t ype t o user32.dll, we can define t his t ype using t he MarshalAs at t ribut e. Visual Basic.NET references unm anaged dat a t ypes t hrough t he Unm anagedType enum erat ed t ype. I f we change t he definit ion of t he t xt argum ent t o Byval txt As String

t he argum ent will be m arshaled as a LPSt r t ype. The revised definit ion of our Win32 class can be found in t he following list ing. See t he Visual St udio .NET docum ent at ion for m ore inform at ion on unm anaged dat a t ypes and t he MarshalAs at t ribut e. Public Class Win32 Declare Auto Function MessageBox Lib "user32.dll" (Byval hWnd As Integer, _ Byval txt As String, Byval caption As String, _ Byval Type As Integer) As Integer End Class Caution: Take extreme care when defining MarshalAs attributes! If we define types that are not compatible with the types defined in the type library for a given COM+ interface, unexpected things may happen.

Detailed Walkthrough The rem ainder of t his chapt er will dem onst rat e how t o use t he t echniques discussed in t he previous sect ions. To do so, we will build a sam ple applicat ion consist ing of a Visual Basic 6 DLL. We will call t his DLL from a Web Service and also from a Visual Basic.NET Class Library. Then we will call t he Class Library from a Visual Basic 6 execut able. The different com ponent s of our exam ple were shown previously in Figure 9.2.

What We Need I f we want t o dem onst rat e t he int egrat ion of Visual St udio .NET assem blies and Visual St udio 6 COM+ com ponent s, we m ust at least be able t o run bot h versions of Visual St udio. The best way t o do t his is t o have t wo separat e developm ent environm ent s: one running Visual St udio .NET, t he ot her running Visual St udio 6. These environm ent s can eit her be on separat e com put ers or on separat e boot able part it ions of

t he sam e com put er. I f we use separat e com put ers, t hey don't need t o be connect ed t hrough a net work. We will only be exchanging sm all files ( t hat will easily fit on a floppy disk) from one environm ent t o t he ot her. Please refer t o t he Microsoft Visual St udio .NET and Visual St udio 6 docum ent at ion for inform at ion on t he syst em requirem ent s for bot h developm ent environm ent s and for inst ruct ions on how t hese environm ent s should be inst alled.

Creating a Simple Visual Basic 6 DLL First we will creat e t he Visual Basic 6 DLL. Since t his book is not prim arily about Visual St udio 6, we will keep t he DLL very sim ple. I t will only be used t o dem onst rat e COM+ int egrat ion. Telling the Time from Visual Basic 6 The DLL we creat e will expose one funct ion: TellTim e. The funct ion will accept one input param et er t hat will inst ruct it how t o form at t he current dat e and/ or t im e. I t will use t he Visual Basic Now funct ion t o ret urn t he current dat e and/ or t im e. First we will swit ch t o t he Visual St udio 6 developm ent environm ent and st art Visual Basic 6. From t he New Proj ect dialog, choose t he New t ab and select Act iveX DLL, t hen click on Open. Select Proj ect 1 from t he Proj ect Explorer and change it s nam e t o gasTellTim e in t he Propert ies t oolbox. Select Class1 and change it s nam e t o gasTim e. Now go t o t he code window and add t he code below t o t he class. Public Function TellTime(ByVal strFormat As String) As String TellTime = Format(Now, strFormat) End Function Th e code for t h e COM + sa m ple D LL is ve r y sim ple .

That is all t he code we need for our COM+ DLL. We sim ply t ell t he DLL what we want it t o report and it will ret urn a form at t ed st ring wit h t he request ed inform at ion. For inform at ion on t he codes used in t he form at t ing st ring, please refer t o t he Visual Basic docum ent at ion. Before we com pile our DLL, we first select gasTellTim e Propert ies... from t he Proj ect m enu and define t he DLL as running in Unat t ended Execut ion m ode by checking t he corresponding checkbox. Figure 9.7 shows what t he developm ent environm ent should look like by now. Aft er we have clicked OK on t he Proj ect Propert ies dialog, we save our work and choose Make gasTim e.dll... from t he File m enu t o com pile our DLL and writ e it t o disk. Registering the DLL on the .NET Machine Before we can use t he COM+ DLL in t he .NET environm ent , we m ust first copy it t o our Visual St udio .NET developm ent environm ent . Alt hough t he act ual file locat ion is unim port ant , in t his exam ple we will assum e t he DLL was copied t o t he 'D: \ gasTix\ gasTim e\ ' direct ory. As wit h any COM+ DLL, t he DLL will have t o be regist ered before it can be used. To do so open, a DOS Com m and Prom pt or select Run... from t he START m enu, and t ype regsvr32 d:¥gasTIX¥gasTime¥gasTime.dll

and press Ent er t o regist er t he DLL. A pop- up wit h t he t ext " DllRegist erServer in d: \ gasTI X\ gasTim e\ gasTim e.dll succeeded." will appear if t he DLL was regist ered successfully. I f t his is not t he case, check t hat t he DLL has been copied t o t he correct locat ion and t hat we reference it correct ly.

Figure 9.7 The COM+ DLL is alm ost finished.

Calling the DLL from a Web Service So far everyt hing has been rat her st raight forward. For m ost developers creat ing COM+ DLLs and regist ering t hem on a server is j ust a repet it ion of old t ricks. But don't worry. We are j ust about t o build a .NET Web Service around t he gasTim e DLL. Creating a RCW using TlbImp Before we can creat e t he act ual Web Service using Visual St udio .NET, we m ust m ake sure t he .NET Fram ework has som et hing t o call int o. For t his purpose we will first creat e a Runt im e- Callable Wrapper ( RCW) by hand. We will do so by using t he Type Library I m port er Ut ilit y ( TlbI m p) t hat ships wit h Visual St udio .NET. From t he " Visual St udio .NET Com m and Prom pt " , navigat e t o t he direct ory cont aining t he gasTim e DLL, t ype TlbImp gasTime.dll /out:gasRCW.dll

and press Ent er t o execut e t he com m and. Figure 9.8 shows how we can access t he Visual St udio .NET Com m and Prom pt .

Figure 9.8 The Visual St udio .NET Com m and Prom pt can be found as part of t he Visual St udio .NET Tools

The TlbI m p ut ilit y will respond wit h a m essage t hat should look like: TlbImp-Type Library to .NET Assembly Converter Version 1.0.2914.16 Copyright (C) Microsoft Corp. 2001. All rights reserved.

Type library imported to D:\gasTIX\gasTime\gasRCW.dll

Note: Use the Visu a l St u dio.N ET Com m a n d Pr om pt instead of the "standard" Command Prompt. The standard Command Prompt does not have the correct environment settings for calling the Visual Studio .NET tools from the command line. If you use the standard Command Prompt, utilities like TlbImp will not be recognized unless they reside in your working directory or you type the full path to these utilities.

Execut ing t he above com m and has creat ed a second DLL t hat ser ves as a runt im e- callable wrapper t o our original COM+ DLL. We can use t he / out : param et er of TlbI m p t o define t he nam e of t he result ing DLL. To learn m ore about t he opt ional param et ers of TlbI m p, t ype: TlbImp /?

or look in t he Visual St udio .NET docum ent at ion. Som e of t he m ost useful opt ions for our purposes are shown in Table 9.4. Table 9.4 Som e of TlbI m p's com m and line param et ers

Parameter

Description

/out:FileName

File name of the assembly to be produced

/namespace:Namespace

Namespace of the assembly to be produced

/asmversion:Version

Version number of the assembly to be produced. The version number must be specified as: Major.Minor.Build.Revision.

/silent

Suppresses all output except for errors

TE

AM FL Y

Now t hat we have creat ed t he RCW, what does it cont ain? Let 's have a look inside using t he I nt erm ediat e Language Disassem bler Ut ilit y ( I LDASM) . St art t he ut ilit y and open gasRCW.dll from t he File m enu. The result should look like Figure 9.9. When we open t he gasTim e obj ect , we not ice t hat gasRCW.dll exposes a m et hod called TellTim e. TellTim e t akes a st ring argum ent and produces a st ring result , j ust like our original DLL. This is because it is not hing m ore t han a wrapper funct ion t o our original DLL's TellTim e m et hod.

Figure 9.9 Our RCW exposes t he TellTim e m et hod j ust like our original DLL

Team-Fly®

Making a Simple Web Service We are now ready t o call t he RCW from a .NET assem bly. For dem onst rat ion purposes, we will creat e a sim ple Web Service around t he RCW DLL. We will t est t he Web Service using I nt ernet Explorer. First st art Visual St udio .NET and choose New Proj ect from t he St art Page. The New Proj ect dialog is shown in Figure 9.10. Select Visual Basic Proj ect s as Proj ect Type and choose t he ASP.NET Web Service t em plat e. Nam e our Web Service " gasWebService" and use 'ht t p: / / localhost ' as it s locat ion. Then click on OK.

Figure 9.10 The New Proj ect dialog in Visual St udio .NET m akes it easy t o creat e a new Web Service.

Go t o t he Solut ion Explorer and select Service1.asm x. I n t he propert ies t oolbox, change it s nam e t o " t ellTim e.asm x" . Then click on t he View Code icon in t he Solut ion Explorer t o see t he code associat ed wit h t ellTim e.asm x. Visual St udio .NET has already added a public class nam ed Class1 t o our proj ect . Change it s nam e t o " gasTellTim e" . I n order t o use t he RCW DLL from wit hin our Web Service, we m ust add a reference t o t he RCW t o our proj ect . To do so, go t o t he Proj ect m enu and click on Add Reference. This will display t he Add Reference dialog as shown in Figure 9.11.

Figure 9.11 We can use t he Add Refer ence dialog t o set a reference t o our RCW DLL in t he Web Service proj ect .

Because our RCW DLL is a .NET assem bly, we select t he .NET t ab in t he dialog. Since t he .DLL is not list ed in t he list of com ponent s, go t o Browse..., locat e gasRCW.dll and click OK. The file " gasRCW.dll" should now be added t o Select ed Com ponent s as shown in Figure 9.11. To finalize adding a reference t o t he com ponent , sim ply click OK. Tip: Since we have already created our RCW by using TlbImp, we can reference it from our project. However, Visual Studio .NET can set references to COM+ components directly by selecting them from the COM tab of the Add Re fe r e n ce dialog. Visual Studio .NET will then give us the option of having the wrapper class generated automatically. Please refer to the Visual Studio .NET documentation for more details.

We will now m ake t he Web Service expose t he TellTim e m et hod of t he RCW so t hat ot her applicat ions can use it . Go back t o t he code window of t ellTim e.asm x.vb and add t he code below t o t he gasTellTim e class. Public Function TellTime(ByVal strFormat As String) As String Dim myRcwDll As gasRCW.gasTime myRcwDll = New gasRCW.gasTime()

TellTime = myRcwDll.TellTime(strFormat) End Function

Because we have added a reference t o gasRCW.dll t o our Web Service proj ect , t he Visual St udio .NET developm ent environm ent is able t o use feat ures like Code Com plet ion and Aut o Quick I nfo ( I nt elliSense) while we t ype our code. An exam ple is shown in Figure 9.12. Aft er saving our proj ect , we can select Build from t he Build m enu. Visual St udio .NET t hen deploys our Web Service t o our developm ent com put er ( localhost ) or any ot her server t hat we have specified as locat ion for our Web Service. As has been said before, t he Web Service in our exam ple is j ust a sim ple wrapper used t o expose t he RCW t o t he web. The RCW it self is a wrapper around our original DLL.

Figure 9.12 Visual St udio .NET offers I nt elliSense for our RCW's obj ect s and m et hods.

I t 's t im e t o t est our Web Service. Let 's st art I nt ernet Explorer and navigat e t o localhost / gasWebService/ TellTim e.asm x. We will be present ed wit h a list of possible operat ions. The only operat ion available from our Web Service is "TellTim e" . When we click on t he " TellTim e" hyperlink, we will j um p t o a page t hat can be used t o t est t he TellTim e Web Met hod. The page also describes how t o call t he Web Met hod using SOAP or HTTP calls. We could have navigat ed t o t his page direct ly by using localhost / gasWebService/ TellTim e.asm x?op= TellTim e as t he page address. Note: We don't need to add a n y code to produce these pages. They are generated automatically when we try to invoke a Web Service without specifying its method or the method's parameters. For more information on building and consuming Web Services, see Chapters 3 "Enabling Inter-Application Communications" and 6 "Implementing .NET Business Services".

Ent er " MMM d yyyy" in t he t ext box for st rForm at 's value and press t he I nvoke but t on. A new browser window will appear as shown in Figure 9.13. When we look at t he new window's URL, we can clearly see how t he TellTim e Web Met hod m ay be called. We sim ply t ake t he URL for t he Web Service and add an ext ra forward slash, t he nam e of t he Web Met hod, a quest ion m ark and t he nam e/ value pairs of any param et er s we wish t o specify. I n our exam ple t his result s in 'ht t p: / / localhost / gasWebService/ TellTim e.asm x/ TellTim e?st rForm at = MMM+ d+ yyyy'. Not e t hat t he spaces in t he param et er st ring are encoded using plus signs ( as is st andard behavior when passing param et ers using t he HTTP GET m et hod) .

Figure 9.13 The .NET plat form m akes t est ing a Web Service easy.

Our web Service ret urns a short XML file t hat includes t he request ed inform at ion:

Jun 22 2001

More inform at ion about int er- applicat ion com m unicat ions using XML can be found in Chapt er 3 " Enabling I nt er- Applicat ion Com m unicat ions" .

Calling the DLL from a Visual Basic.NET Class Library

We have already seen t hat our RCW DLL int egrat es seam lessly wit h an ASP .NET Web Service. But what does it t ake t o m ake t he TellTim e m et hod available t hrough a Visual Basic.NET Class Library? Class Libraries are t he .NET equivalent of Act iveX DLLs. I n t his sect ion, we will creat e such a Class Library using Visual Basic.NET. Lat er in t his chapt er we will call t he Class Library from a Visual Basic 6 execut able j ust like a COM+ com ponent . Setting up the Class Library To st art creat ing t he Class Library, st art Visual St udio .NET and choose New Proj ect from t he St art Page. Select Visual Basic Proj ect s and choose t he Class Library t em plat e. Ent er " gasClass" as t he nam e for t he proj ect and define a locat ion ( t he default locat ion given by Visual St udio .NET will do) . Once t he proj ect is creat ed, select Class1.vb in t he Solut ion Explorer and change it s nam e t o " gasClass.vb" . Visual St udio .NET has already added a public class called Class1 t o our proj ect . Renam e t his class t o " gasTim e" . To be able t o use t he RCW's m et hod in our proj ect , we will have t o add a reference, j ust like we did for t he Web Service proj ect . Go t o t he Proj ect m enu and click on Add Reference. Once again t he Add Reference dialog will appear. Select t he .NET t ab, click on Browse... and locat e gasRCW.dll. Then click OK t o select t he DLL and click OK again t o add t he reference t o our proj ect . Go t o t he code window for gasClass.vb and add t he code below t o t he gasTim e class. Figure 9.14 shows what t he developm ent environm ent should look like. Adding t he TellTim e funct ion will call t he RCW's TellTim e m et hod and m ake it s result available t hrough t he Class Library's TellTim e m et hod. I f t his code looks fam iliar, t hat is probably because it is fam iliar! I t is an alm ost exact copy of t he code t hat was used t o creat e our Web Service. The only part t hat is m issing is t he < WebMet hod( ) > at t ribut e. Com pare t he snippet below t o t he previous snippet t o spot t he difference. Public Function TellTime(ByVal strFormat As String) As String Dim myRcwDll As gasRCW.gasTime myRcwDll = New gasRCW.gasTime()

TellTime = myRcwDll.TellTime(strFormat) End Function

Once again, creat ing t he proj ect is rat her st raight forward. Aft er saving our work we can select Build from t he Build m enu t o creat e t he gasClass assem bly. Calling the Class Library from a Visual Basic 6 Executable Now t hat we have creat ed t he Class Library, we are alm ost ready t o call it from a Visual Basic 6 execut able using t he COM+ int erface. But since Visual Basic 6 cannot add references t o assem blies, we will have t o export a t ype library first . Exporting the Type Library using TlbExp Visual St udio .NET ships wit h a ut ilit y called t he Type Library Export er Ut ilit y, or TlbExp for short . TlbExp does j ust t he opposit e of TlbI m p: it t akes an assem bly and export s a t ype library based on t he assem bly's cont ent s. To use it , we open t he Visual St udio.NET Com m and Prom pt and navigat e t o t he direct ory cont aining t he gasClass assem bly. Then we t ype TlbExp gasClass.dll /out:gasCCW.tlb

and press Ent er t o execut e t he com m and. The TlbExp ut ilit y will respond wit h a m essage t hat should look like sim ilar t o t he following: TlbExp-.NET Assembly to Type Library Converter Version 1.0.2914.16 Copyright (C) Microsoft Corp. 2001. All rights reserved.

Assembly exported to D:\gasTIX\gasClass\gasCCW.tlb

Execut ing t he above com m and has creat ed a t ype library t hat we can use t o set references t o t he assem bly from Visual St udio 6. We can use t he / out : param et er of TlbExp t o define t he nam e of t he result ing t ype library. To learn m ore about t he opt ional param et ers of TlbExp, t ype: TlbExp /?

or look in t he Visual St udio .NET docum ent at ion. Anot her param et er t hat is wort h m ent ioning, is t he / silent param et er t hat prevent s TlbExp from displaying m essages.

Figure 9.14 Our sam ple class library uses funct ionalit y found in our COM+ DLL.

Creating a Visual Basic 6 Executable I t is t im e t o swit ch back t o our Visual St udio 6 developm ent environm ent again. Copy t he t ype library t hat we produced in t he previous paragraph and place it som ewhere wit hin t he Visual St udio 6 environm ent . Next , st art Visual Basic 6 so t hat we

can creat e a sim ple t est applicat ion t o call t he Class Library t hrough a COM+ int erface. Select St andard EXE from t he New Proj ect dialog. Once t he proj ect has been creat ed, select Proj ect 1 from t he Proj ect Explorer and change it s nam e t o 'Test CCW' in t he Proper t ies t oolbox. Select Form 1 and renam e it t o 'MainForm '. Change it s capt ion t o 'Test CCW'. We will now add som e cont rols t o t he form . Figure 9.15 shows what t he form should look like.

Figure 9.15 Set t ing a reference t o t he t ype library export ed wit h TlbExp

We will add a Label cont rol and change it s capt ion t o " Form at : " . Then we will add a Text Box cont rol and change it s default Text propert y t o " m m m d yyyy" . The last cont rol t o be added is a Com m andBut t on. Change it s capt ion t o " Call t he .NET assem bly" , t hen change t o t he code window for MainForm . Before we can call t he assem bly, we will first have t o add a reference t o it s t ype library. To do so, w e select References... from t he Proj ect m enu. Click on Browse... and locat e t he t ype library t hat we have copied from t he Visual St udio .NET developm ent environm ent . This is shown in Figure 9.15. Click on Open and OK t o add t he reference t o our proj ect . Now add t he code of t he list ing below t o t he code window. This will creat e an inst ance of t he assem bly when t he user clicks t he Com m andBut t on and show it s result s in a MessageBox. Private Sub Command1_Click() Dim myAssembly As gasClass.gasTime Set myAssembly = New gasClass.gasTime

MsgBox myAssembly.TellTime(Text1.Text)

Set myAssembly = Nothing End Function Note: For more details on defining date/time formats, please refer to the Microsoft Visual Basic documentation. Some of the most useful options for our example are shown in Table 9.5. Ta ble 9 .5 Som e of t h e ch a r a ct e r s t h a t ca n be u se d t o de fin e t h e da t e for m a t

Character

Description

d

Display the day as a number without a leading zero (1 31).

dd

Display the day as a number with a leading zero (01 - 31).

m

Display the month as a number without a leading zero (1 - 12).

mm

Display the month as a number with a leading zero (01 12).

mmm

Display the month as an abbreviation (Jan - Dec).

mmmm

Display the month as a full month name (January December).

yy

Display the year as a 2-digit number (00 - 99)

yyyy

Display the year as a 4-digit number (0001 - 9999)

The only t hings left t o do in t he Visual St udio 6 environm ent are saving our work and building t he execut able. Copy t he execut able so we can t ransfer it t o t he Visual St udio .NET environm ent . Testing the Assembly from Visual Basic 6 Aft er m aking a final swit ch t o our Visual St udio .NET developm ent environm ent , we are j ust about ready t o st art our t est applicat ion. Copy Test CCW.exe t o any direct ory and copy bot h gasClass.dll and gasRCW.dll t o t he sam e direct ory. I n our exam ple we will assum e t hat t he files have been copied t o D: \ gasTI X\ Test CCW\ . There is j ust one t hing left t o do. I f we want t o call a COM+ com ponent , it needs t o be regist ered first . Ot herwise, COM+ won't be able t o locat e t he com ponent . I f we want t o call an assem bly j ust like it was a COM+ com ponent , it needs t o be regist ered as well. But since it is not a COM+ com ponent , we can't use regsvr32.exe t o do t he regist ering for us. We will have t o rely on anot her ut ilit y t hat ships wit h Visual St udio .NET, RegAsm . RegAsm is t o assem blies what regsvr32 is t o Win32 .DLLs. I t regist ers t he assem bly and allows it t o be called t hrough a COM+ int erface. To regist er gasClass.dll, st art t he Visual St udio.NET Com m and Prom pt , navigat e t o t he direct ory cont aining Test CCW.exe and t he t wo assem blies, and t ype

RegAsm gasClass.dll

and press Ent er t o execut e t he com m and. This will regist er t he assem bly and should respond wit h a m essage like: RegAsm-.NET Assembly Registration Utility Version 1.0.2914.16 CopyRight (C) Microsoft Corp. 2001. All rights reserved.

Types Registered succesfully

To learn m ore about t he opt ional param et ers of RegAsm , t ype: RegAsm /?

or look in t he Visual St udio .NET docum ent at ion. Som e of t he m ost useful opt ions for our purposes are shown in Table 9.6. Ta ble 9 .6 Som e of Re gAsm 's com m a n d lin e pa r a m e t e r s

Parameter

Description

/unregister

Unregister types

/tlb[:Filename]

Export the assembly to the specified type library and register it

/silent

Prevents displaying of success messages

Tip: Both TlbExp and RegAsm (with /tlb parameter) are capable of exporting type libraries. The difference is that RegAsm will also register the assembly, while TlbExp will not. If we want to register an assembly and export its type library at the same time, we can use RegAsm filename1 /tlb:filename2 as a shorthand form for RegAsm filename1 followed by TlbExp filename1 /out:filename2.

We are now ready t o st art our t est applicat ion. We can ent er any form at st ring in t he t ext box. When we press t he but t on, a MessageBox will show t he result . Figure 9.16 shows an exam ple using t he default st ring " m m m d yyyy". Please not e t hat as was originally shown in Figure 9.2 at t he beginning of t his chapt er, it is our original Visual Basic 6 DLL t hat produces t he act ual result . The call from Test CCW.exe is t ransferred int o t he CLR t hrough gasClass.dll and gasRCW.dll, and t hen out of t he CLR again t o gasTim e.dll. This clearly is not t he m ost efficient solut ion, but it dem onst rat es all t he m echanism s involved in COM+ int egrat ion.

AM FL Y

Figure 9.16

From Here

TE

Our t est applicat ion is finally running!

I n t his chapt er we have explored t he various ways in which m anaged and unm anaged code can coexist wit hin an applicat ion. We have looked at Runt im e- Callable Wrappers, COM- Callable Wrappers and various ut ilit ies t hat m ay be used t o int egrat e COM+ com ponent s wit h .NET code. For m ore inform at ion on t he t opics covered in t his chapt er, see: z

To learn more about Web Services and inter-application communications, see Chapter 3, "Enabling Inter-Application Communications."

z

For more information on performance testing, see Chapter 15, "Performance Testing in .NET."

z

For further information on the integration of .NET and COM+, see the Microsoft Visual Studio .NET documentation.

z

For more information on using Visual Studio 6, see "Building Enterprise Solutions with Visual Studio 6-The Authorative Solution", by Don Benage, Azam Mirza et al, ISBN 0-672-31489-4, Sams Publishing.

Team-Fly®

10 Event Searching The bulk of t he user experience on gasTI X is t he process of users searching for part icular event s for which t hey wish t o buy t icket s. We have st riven t o m ake it easy for users t o easily find event s for a specific Part icipant or Venue, as well as browse part icipant s based on cat egories and subcat egories. The Event Searching port ion of gasTI X is built as an ASP.NET proj ect writ t en in Visual Basic .NET. This chapt er exam ines t he process of building t hese pages and building reusable user cont rols which are inst ant iat ed on m any of t he ASP.NET pages. The overall goal of t he event searching services in t he gasTI X present at ion layer is t o allow users t o quickly and easily find inform at ion about event s and t icket s, and t o encourage browsers t o becom e buyers.

Building Presentation Services with ASP.NET I n order t o build t he gasTI X present at ion services, we first need t o ident ify t he goals of t he present at ion layer. We need t o ident ify how users will use t he websit e, what inform at ion t hey will search for, and how t o m ake t he whole process easy. The present at ion layer holds a special dist inct ion: I t is t he only code which produces out put direct ly viewed by t he user. As anyone who has built an e- com m erce sit e will t ell you, it doesn't m at t er how well a websit e can handle orders if t he websit e it self does not convince visit ors t o becom e buyers. I n order t o act ually build t he gasTI X present at ion services, we will use ASP.NET web form s and web cont rols. I n order t o effect ively use t hese, it is im port ant t o underst and t he ASP .NET obj ect m odel and t he base classes from which all ot her web cont rols are inherit ed from . This chapt er exam ines t he ASP.NET code t hat consum es t he gasTI X business obj ect s and offers event - searching capabilit ies t o t he user.

Presentation Service Goals There are num erous ways t o det erm ine and docum ent t he goals of a websit e; gasTI X used st andard UML use cases t o help in t his process. These are described in chapt er 2, " gasTI X: A Sam ple .NET e- Business." There are four prim ary goals t hat gasTI X users have when accessing t he sit e: z

Finding information or tickets for a specific event such as a concert or football game

z

Finding events for a specific performer or venue

z

Finding personalized events in which they may be interested

z

Making users feel comfortable enough to purchase tickets

Given t he goals of gasTI X users, t he event - searching capabilit y m ust allow quick access t o t icket inform at ion t hrough a variet y of search t echniques. Users m ay know t he nam e of t he event t hey would like t o purchase t icket s for, or sim ply t he nam e of t he perform er or venue. Searching on any of t hese pieces of inform at ion should allow gasTI X users t o locat e event s for which t hey m ay purchase t icket s.

Take t he exam ple of our fict ional bagpipe band, t he gasBAGS. I f our user knows t hat t he gasBAGS will be playing at t he gasAudit orium som et im e in t he next t wo m ont hs, he will likely want t o search for all upcom ing gasBAGS event s t o find t he part icular event t hey are int erest ed in. Or, since t hey know t he part icular venue in quest ion, t hey m ay search specifically for gasAudit orium . Or, perhaps t hey know t hat t his part icular event is j ust one event in t he " gasBAGS World Tour" and would like t o know about t he ot her event s scheduled for t his t our. I n order t o m inim ize t he am ount of effort required by t he user t o locat e t he appropriat e inform at ion, t he gasTI X websit e m ust m ake it possible t o easily locat e an event based on any of t he possible pieces of inform at ion. To t hat end, t he gasTI X websit e em ploys a " com bined search" feat ure, which allows users t o ent er a single piece of inform at ion and review m ult iple t ypes of inform at ion t hat m eet t he search crit eria. For inst ance, if a user t ypes " gas" int o t he com bined search ent ry box, a display sim ilar t o Figure 10.1 will display event s, t our, venues, and perform ers m at ching " gas" . Under t he Perform ers cat egory, we find t he gasBAGS bagpipe band and under t he Venues cat egory, we find t he gasAudit orium venue. Likewise, under t he Tour cat egory, we find a single ent ry, t he " gasBAGS World Tour 2002" , and under t he Event s cat egory, we find m ult iple event s t hat com prise t he world t our.

Figure 10.1 The com bined search feat ure of gasTI X displays sev eral t ypes of inform at ion for a single search ent ry. Note: The code for the "combined search" feature of gasTIX is discussed later in this chapter.

Since t he ent ry point for t he com bined search funct ionalit y is found on t he gasTI X hom e page ( and duplicat ed on each subsequent page) , users can quickly find inform at ion about all aspect s of gasTI X dat a. However, we also need t o consider t he possibilit y t hat t he user does not know t he specific nam e t hey are looking for when searching for inform at ion. Perhaps a user is visit ing a cit y and would like t o see what event s are t aking place in t hat cit y during t he next m ont h. gasTI X needs t o allow t he user t o view a list of venues in t he specified cit y as well as a list of event s for each venue. Or perhaps t he user cannot rem em ber t he nam e of our bagpipe band, and inst ead rem em bers only t hat t he band t hey are looking for plays t he best Celt ic m usic around. To assist t hese users, and t o allow user s t o find event s or perform ers sim ilar t o t hose t he user already knows of, gasTI X has organized all event s and perform ers int o a st ruct ured set of cat egories and subcat egories. gasTI X cat egories are high- level const ruct s like Music and Sport s, while subcat egories are furt her defined int o rock, count ry, or Celt ic m usic. Thus, since our user knows t hat he likes Celt ic m usic and finds t hat t he gasBAGS band is classified in t he Celt ic subcat egory of t he Music cat egory, he can find a band of int erest t o him . I n addit ion, t he user can find ot her bands t hat play Celt ic m usic. I f we successfully m eet t he first t hree goals of t he gasTI X present at ion layer, t hen m eet ing t he fourt h goal, t urning browsers int o buyers, should be easy. I f t he user can find t he t icket s he is looking for quickly and easily, he will be m uch m ore likely t o act ually m ake a purchase t han if he has t o spend t en m inut es searching t he sit e. Web users have a not oriously short pat ience level; if t hey can not find what t hey are looking for quickly, t hey are m ore likely t o look on anot her sit e, rat her t han keep searching deeper int o t he original sit e.

ASP.NET Web Controls One of t he excit ing new feat ures of ASP.NET is t he consist ent obj ect m odel shared by all web cont rols, cust om cont rols, and pages. The obj ect m odel is consist ent and independent of language, allowing all ASP.NET developers t o program against t he sam e obj ect s, propert ies, and m et hods. This allows t he developer t o focus m ore energy on t he business problem and less on how a part icular synt ax works. To underst and t he new obj ect m odel, we m ust first discuss t he basic obj ect s t hat all ASP.NET applicat ions will use. The Control Class As discussed in chapt er 3, web form s are t he basis of m ost ASP.NET applicat ions and serve as t he st art ing point for building t he gasTI X Event Searching services. Server- side web cont rols are added t o each web form and m anipulat ed eit her program m at ically or t hrough t he use of HTML t ag param et ers. For ASP.NET developers, t he m ost pert inent branch of t he .NET Fram ework obj ect m odel is t he Syst em .Web.UI nam espace. This nam espace cont ains all of t he user int erface ( UI ) cont rols built for use on t he web. The first class in t his nam espace t hat we will discuss is t he Cont rol class. The Cont rol class is t he base definit ion for all ASP.NET server cont rols and user cont rols. St andard cont rols include t he Text Box cont rol and Label, while user cont rols can be creat ed and dist ribut ed by any t hird- part y. See t he " User Cont rols" sect ion lat er in t his chapt er for m ore inform at ion. The Syst em .Web.UI .Cont rol class offers t he base propert ies t hat all visual web cont rols require, such as an I D, a reference t o a parent cont rol, and t he visibilit y of t he cont rol. All cont rols inherit t hese basic propert ies from t he Cont rol class, as well as basic m et hods like Get Type, FindCont rol, and ToSt ring. These

m et hods provide t he basic funct ionalit y all cont rols need t o det erm ine t heir t ype, t o find child cont rols, and t o ret urn a st ring represent at ion of t hem selves. A m et hod defined on t he Cont rol class is t he Dat aBind( ) m et hod. Dat aBind connect s t he cont rol t o a Dat aSource and resolves all dat a- binding expressions defined wit hin t he cont rol. This is t ypically used t o display m ult iple set s of inform at ion ret urned from a dat abase query, such as a list of st at es or regions in a drop- down box or a list of event s in a Dat aGrid. However, dat a binding can also be used t o creat e single- value dat a- bound form ulas. This m et hod can be used t o t ie t wo cont rols t oget her, so t hat t he value of t he second is always based on t he value of t he first . For m ore inform at ion on dat a binding, see t he sect ion ent it led " Using t he Dat aList Cont rol" lat er in t his chapt er. The Cont rol class also defines several event s which all cont rols inherit . These event s allow us t o execut e code when t he cont rol is first init ialized, when it is act ually loaded int o a web page, when it is unloaded from m em ory, and when it is dat a bound. The Syst em .Web.UI branch of t he .NET Fram ework obj ect m odel provides t he basic st ruct ure for all cont rols we will use in every ASP.NET proj ect and provides a uniform st ruct ure for our pages. The TemplateControl Class The Tem plat eCont rol class inherit s direct ly from t he Cont rol class and provides funct ionalit y t o t wo ot her classes ASP.NET developers should becom e very fam iliar wit h, Page and UserCont rol. The Tem plat eCont rol class subclasses t he Cont rol class and adds several m et hods. The prim ary funct ionalit y added is t he abilit y for Tem plat eCont rol class obj ect s t o cont ain ot her cont rols. This allows page obj ect s t o cont ain cont rols and user cont rols t o be nest ed wit hin ot her user cont rols. The Tem plat eCont rol class also adds an Error event , which occurs when an unhandled except ion is t hrown. This m akes it easy t o cat ch errors at eit her t he page level, or for any individual cont rol. The Tem plat eCont rol class get s it s nam e from t he fact t hat it can obt ain inst ances of an I Tem plat e int erface and apply t hat t em plat e t o t he cont rols cont ained by t he Tem plat eCont rol. This allows developers t o easily define grouping st yles for repeat able inform at ion in a single locat ion. For m ore inform at ion about using Tem plat es, see " Using t he Dat aList cont rol" sect ion lat er in t his chapt er. The first class derived from t he Tem plat eCont rol class is t he Page class. Page is t he class from which all web pages are inst ant iat ed. I n classic ASP, a page was sim ply a t ext file parsed by t he web server. I n ASP.NET, t he page is now a fully qualified obj ect , derived from Tem plat eCont rol, wit h it s own propert ies and m et hods. Each t im e t he web server receives a request for a web page, t he Page obj ect is inst ant iat ed and populat ed wit h t he inform at ion st ored in t he .aspx and code- behind files. The Page class exposes as propert ies t he st andard obj ect s t hat ASP developers are accust om ed t o: Applicat ion, Request , Response, Server, and Session. The Page class also offers new propert ies: Validat ors, which will be discussed in Chapt er 11, " Purchasing a Ticket " ; Trace, which will be discussed in Chapt er 14, " Debugging a .NET Applicat ion" ; and I sPost Back, which will be used on alm ost every gasTI X page. Com m only used propert ies of t he page class are shown in t able 10.1. Ta ble 1 0 .1

Properties

Description

Application,

Returns a reference to the current object of the same name

Request, Response, Server, Session Cache

Allows developers to store this page for later use

Controls

A collection of controls on the page

ErrorPage

The page to display if an unhandled exception occurs

IsPostBack

True if the page is being instantiated in response to an event on the page, false otherwise

Trace

Returns a reference to the TraceContext object

User

Returns information about the user who requested the page

Validators

Returns a collection of all validation controls on the page

Methods DataBind

Connects all databound controls on the page to their appropriate datasource.

LoadControl

Instantiates a UserControl from a user control (.ascx) file.

Validate

Executes all validation controls on the page.

The second class derived from t he Tem plat eCont rol is t he UserCont rol class. The UserCont rol class is t he class inst ant iat ed when a developer creat es a user- defined class st ored in an .ascx file. User cont rols ar e sim ilar t o Page obj ect s in how t hey are inst ant iat ed; however, UserCont rols require a parent Page obj ect in order t o be inst ant iat ed. UserCont rols cannot be creat ed, except wit hin t he cont ext of a page obj ect . For m ore inform at ion on UserCont rols, see t he " .NET User Cont rols" sect ion in t his chapt er. Other Key Classes There are several classes in t he Syst em .Web.UI .Cont rol nam espace t hat you should know as shown in Figure 10.2. These include: z

The WebControl class. This class provides a foundation for all server-side ASP.NET web controls, such as TextBox and Label. The WebControl class provides basic properties such as color and border, as well as events such as ApplyStyle. This ensures that all Web Controls share similar property names, a welcome improvement over classic ASP development.

z

The HTMLControl class. This class is inherited by all standard HTML element classes, such as and . This is an important point for developers learning ASP.NET. Using ASP.NET server controls does not exclude you from using standard HTML controls, and using standard HTML controls does not prevent you from having a well-defined object model. An ASP.NET page can instantiate a

System.Web.UI.HTMLControl.HtmlImage object and programmatically set its normal HTML parameters, such as the sr c and h e igh t parameters. z

The Re pe a t e r and Re pe a t e r I t e m classes. These two classes work in concert to provide base functionality for template items. The styles defined within the template will be repeated once for each row of bound data. Each Re pe a t e r object can have one or more Re pe a t e r I t e m s attached to it. The sections "Using our Repeater Control" and "Using the DataList Control" in this chapter offer examples of using objects derived from the Re pe a t e r class.

Figure 10.2 All ASP.Net cont rols deriv e from t he Cont rol class.

Building the ASP.NET Project Now t hat we have defined t he goal for t he search port ion of t he gasTI X websit e and learned m ore about t he ASP.NET obj ect m odel, we will j um p right int o building t he proj ect it self. The gasTI X solut ion was init ially developed as m ult iple Visual St udio .NET proj ect s by developers in geographically dist inct places. Much im proved proj ect int eroperabilit y is an im port ant benefit of ASP.NET, allowing developers t o work separat ely t owards a com m on goal. I n t his chapt er, we will creat e t he gasTI X_Search proj ect , allow ing users access t o t he gasTI X event searching feat ures. The next chapt er, " Purchasing a Ticket " will build t he second proj ect in t he present at ion services layer. These t wo proj ect s will t hen be com bined wit h t he business layer proj ect s t o form t he gasTI X solut ion.

The gasTIX Search Project To creat e t he gasTI X_Search proj ect , do t he following: z

Open Visual Studio .NET

z

Select New|Project

z

Under Project Types, select "Visual Basic Project" and highlight "ASP.NET Web Application" in the Templates window

z

Change the project Name to "gasTIX_Search" and ensure that the location specified points to your local machine.

Note: The presentation services require the use of the business objects created for gasTIX. Both the assemblies and source code are available for download at www.gasTIX.com. Alternately, the assemblies are available for web service consumption at www.gasTIX.com/Participant.asmx and www.gasTIX.com/Sale.asmx. You can reference them locally or through a web service. See Chapter 11, "Purchasing A Ticket," for more information on web services.

Figure 10.3 The New Proj ect dialog offers several languages and proj ect t ypes.

Aft er t he new websit e is creat ed, Visual St udio.NET present s us wit h a st andard ASP.NET applicat ion wit h a default web form called WebForm 1.aspx. For t his exam ple, renam e t he web form " Com binedSearch.aspx." The proj ect also creat es several ot her files, as det ailed here: z

Styles.css--This is a Cascading Style Sheets (CSS) document that describes styles used in the newly created application.

z

Global.asax--This file handles application-level events such as Application_BeginRequest and Application_Error.

z

web.config--This file holds configuration information about the application, such as default language and session state information.

z

AssemblyInfo.vb--This file contains information to be built into the assembly (.dll) such as version and trademark information.

I n order t o m ake use of t he gasTI X m iddle- t ier business obj ect s, we first need t o add a reference t o t hem . Right - click on References in t he Solut ion Explorer dialog and select " Add Reference" . Click " Browse" and locat e gasTI X.BusinessFacade.dll. The BusinessFacade assem bly provides t he basic business obj ect s of gasTI X: event s, part icipant s, venues, and so on. Each business obj ect has appropriat e m et hods and propert ies t hat will allow our gasTI X_Search pages t o ret rieve and display t he necessary dat a. Our ASP.NET proj ect is now init ialized and ready for us t o add som e pages. Tip: If you have downloaded the entire code for the business objects, instead of just the compiled assemblies, you will find it much easier to work with project references versus assembly references. Simply create a solution that contains all business layer projects and your gasTIX_Search project.

When you add references

to the business layer projects to your gasTIX_Search project, simply select the references from the "Projects" tab of the Add References dialog.

Separation of Code and Design in gasTIX One of t he m ost beneficial design im provem ent s of ASP.NET for building gasTI X is t he abilit y t o separat e code and design. As m ent ioned previously, a dist ribut ed t eam of designers and developers working all over t he Unit ed St at es and in Europe built gasTI X. The abilit y t o creat e t he design of t he websit e at a different t im e and locat ion from t hose creat ing t he code was crit ical t o t he developm ent cycle's success. gasTI X uses code- behind pages ext ensively for t his reason. ( For m ore inform at ion on code- behind pages, see chapt er 5, " I m plem ent ing .NET Present at ion Services." ) The developers were able t o drop t he cont rols t hey needed on t he web form and writ e t he code in t he code- behind page t o access t hose cont rols. They would t hen t ell t he designers what cont rols were necessary on any given page. The designers would t ake t hat inform at ion and design an aest het ically pleasing web form using t he cont rols dict at ed by t he developers. When t he designers were finished, t he developers sim ply replaced t he local web form s ( but not t he code- behind) wit h t he ones supplied by t he designers. Since all of t he applicat ion logic is st ored in t he code- behind files, t his did not break any of t he developers' code. Aft er designers and developers ident ified which pages were necessary, and which cont rols each of t hose pages would cont ain, each could work at t heir own pace and in an order t hat suit ed t heir needs.

Tip: The default code-behind file name changed between Beta 1 and Beta 2 of the .NET Framework to include the '.aspx' before the '.vb.' However, you can specify any name you like for the file, as long as the CodeBehind attribute of the Page or Control directive matches up.

This syst em , while m uch bet t er t han classic ASP developm ent , is not perfect yet . There are sit uat ions in which t he code and design st ill bleed t oget her. One such sit uat ion is using Dat aList cont rols. Many designers are not com fort able wit h creat ing t he necessary server- side code t o call t he Dat aBinder.Eval m et hod. Wit hout t his applicat ion code, t he Dat aList has no funct ionalit y. While t he abilit y t o use t em plat es and st yles does help separat e code and design, som e of t he design of it em s in t he Dat aList st ill needs t o go in t he I t em Tem plat e. Technically, t he I t em Tem plat e can be filled from t he code- behind page, but t his get s int o t he issues t hat have plagued ASP developers for years. When you have server- side code writ ing client - side HTML, it is bot h inefficient and ugly. I t is m uch easier t o design t he HTML code on t he HTML page, rat her t han t rying t o design it piece by piece in VB.NET code. Overall, however, t he abilit y t o separat e code from design allows m uch m ore product ivit y and efficiency.

Using ASP.NET Web Controls To st art building t he gasTI X_Search proj ect we will first creat e a sim ple Web Form t hat allows users t o perform t he " com bined search" discussed previously. We will build a single page t hat will have t wo sim ple com ponent s: a Text Boxes web cont rol and a But t on web cont rol. This sect ion describes how t o use sim ple Web Cont rols how t o add add t he cont rols t o a page. TextBox and Button Controls

The first t hing we need t o do t o build our page is add a Text Box cont rol t o our Web Form . This t ext box will allow our user t o ent er t he t ext t hey would like t o search for. Follow t hese st eps: z

1. Next, double-click CombinedSearch.aspx in the Solution Explorer to open it in design mode. Notice the

two tabs just below the design area in Figure 10.4. The "Design" and "HTML" views work interchangeably; when one is modified, the other is automatically updated to reflect the change. Tip: If you are creating your web forms in the HTML view, a quick way to validate your code is to click the Design tab, or press Control-Page Up on your keyboard. Before Visual Studio.NET switches to design mode, it will validate your code and notify you of any errors. z

2. To add a textbox web control to our form, we first need to locate the Toolbox menu. This is usually

docked on the left hand side of the screen and is identified by an icon of a hammer and a wrench. If the Toolbox is not visible, simply click the tab and it will unfurl on top of our web form. Locate the textbox control in the toolbox and double-click it to add one to the web form. Tip: The controls in the toolbox are arranged according to the order in which Microsoft believes they will be most commonly used. The items can also be arranged alphabetically by right-clicking the toolbox and choosing "Sort Items Alphabetically."

z

3. When you double-click the textbox control, a small text box appears in the upper, left-hand corner of the

web form. You can move this control to any location on the web form by simply dragging and dropping it. The control will stay wherever you put it because web forms, by default, are created with their pageLayout property set to "GridLayout." The GridLayout option allows you to graphically design the layout of the page and develops HTML to automatically support our design. Visual Studio.NET will create the necessary HTML tables or CSS absolute positioning tags to arrange the web controls on the page. For more information on positioning

TE

AM FL Y

web controls, see chapter 5, "Implementing .NET Presentation Services."

Figure 10.4 Visual St udio.NET offers bot h a design view and an HTML view of every Web Form . z

4. Change the name of the textbox control to "txtCombinedSearch." We also need a button control on our

form, so double click the button control in the toolbox and then move the button to an aesthetically pleasing location on the form. We will call the button "butSearch." In order to make the form a little more attractive, you can add a label next to the textbox. Change the caption to explain to the user the purpose of the textbox. The form now looks similar to the one shown Figure 10.5.

Team-Fly®

Figure 10.5 This sim ple web form now has t he cont rols we need t o perform a search on all event s, t ours, part icipant s, and venues.

Now t hat we have added t he t wo basic cont rols t o t he web form , t ake a m om ent t o look at t he HTML creat ed by t he design view. Click t he HTML t ab in t he bot t om left corner of t he screen. Find t he t ext box cont rol we added; t he HTML for it should look sim ilar t o t his:

There are t wo im port ant t hings t o not e about t his HTML t ag. The first is t he " RunAt " at t ribut e which is set t o t he value of " Server." This t ells t he ASP.NET engine t o handle t his cont rol on t he server, before t he page is sent t o t he client . This is what gives ASP.NET t he opport unit y t o exam ine t he browser m aking t he request and t hen creat e cust om ized out put based on t he browser level. The ot her int erest ing not e about t his HTML definit ion is t he prefix at t ached t o t he Text Box definit ion. The " ASP: " prefix t ells ASP.NET t hat t his t ext box cont rol is part of t he st andard ASP.NET cont rol collect ion. All web cont rols shipped wit h ASP.NET are part of t he ASP package. When we develop our own cust om cont rols lat er in t his chapt er, we will change t his prefix t o t ell ASP.NET t hat we are using cont rols from our own package. You now have everyt hing you need on t he web form t o allow a user t o perform a com bined search. Next , you need som e way t o display t he dat a ret urned from t he m iddle t ier obj ect s. For t hat , you will add a Repeat er t o t he web form .

Repeating Data Controls The Repeat er cont rol is one of several web cont rols shipped wit h Visual St udio.NET t hat allows developers t o easily display rows of dat a. Displaying dat a from a dat abase ( or XML file or any ot her dat a st ore) is a very com m on purpose for web pages, so ASP.NET is designed t o m ake t hese t asks easy. ASP.NET offers t hree cont rols designed t o m ake form at t ing dat a easy and reusable: z

Repeater Control

z

DataList Control

z

DataGrid Control

The repeat er cont rol is t he sim plest of t he repeat ing dat a cont rols, because it has no inherent user int erface. Thus, t he repeat er cont rols offer t he m ost out put flexibilit y. However, t he t rade- off for t his flexibilit y is m ore effort required on t he developer's part . The repeat er cont rol is very useful for any t ype of list ed dat a including spreadsheet - st yle t ables, bullet ed point list s, series of im ages, or even sim ple com m a- delim it ed list s. The dat alist is sim ilar t o t he repeat er cont rol, but is designed specifically t o handle t able- st yle dat a. The dat alist organizes each dat a record int o a single row of a st andard dat a t able form at . Developers have cont rol over bot h t he display of t he t able as a whole, and each row of dat a. I f you are looking for a way t o display a spreadsheet st yle t able of dat a, t he dat alist is a bet t er choice t han t he repeat er because it has t he t able st ruct ure built int o it . I f you use a repeat er cont rol, you need t o define t he t able st ruct ure using HTML t ables or sim ple < BR> t ags. On t he ot her hand, t he dat alist aut om at ically creat es t he necessary < TABLE> , < TR> , and < TD> t ags t o display t he dat a correct ly. Dat alist cont rols have full WYSI WYG edit ing in Visual St udio.NET, using t he design m ode t ools. For exam ple code, see t he " Using t he Dat alist Cont rol" sect ion lat er in t his chapt er. The dat agrid cont rol t akes t he concept of t he dat alist one st ep furt her. Like t he dat alist cont rol, dat agrid cont rols aut om at ically display dat a in a grid form at . Dat agrid cont rols add addit ional funct ionalit y: aut om at ic paging, dat a sort ing, dat a edit ing and delet ing. Like dat alist cont rols, dat agrid cont rols support full WYSI WYG edit ing in design m ode and t he design of t he grid is highly cust om izable. I f you are looking for a st andard t able layout wit h t he abilit y for t he user t o sort or edit t he dat a, t hen dat agrid is t he best choice. The aut om at ic paging funct ionalit y of t he dat agrid also m akes it an excellent choice when you hav e m ore dat a t o display t han will convenient ly fit on a single screen. I f, however, you want sim ple, read- only display of your dat a, t he dat alist or repeat er cont rols will be a bet t er choice as t hey incur less overhead. For t he gasTI X com bined search page, we will use four repeat er cont rols t o display t he dat a ret urned by t he m iddle t ier. Each repeat er cont rol will display a group of dat a relat ing t o one of our search cat egories: Event s, Tours, Part icipant s, and Venues. The repeat er web cont rol sit s in t he t oolbox like all st andard cont rols and can be easily double- clicked and added t o t he page. Creat e four repeat er cont rols and nam e t hem : rpt Event s, rpt Tours, rpt Venues, and rpt Part icipant s. Arrange t hem in a single colum n below t he t ext box and but t on already on t he page. Note: It is important that there be a valid datasource with at least one row of data bound to any repeater, datalist, or datagrid. If a repeating data control has no bound data, it will not be rendered on the page.

Using Repeater Controls Now t hat we have added a repeat er cont rol t o our web page, we need t o creat e som e t em plat es for ident ifying how t o lay out t he dat a. Every repeat er cont rol requires an < I t em Tem plat e> t ag t o display dat a. Addit ionally, each one can have several opt ional t em plat es as well, such as < Alt ernat ingI t em Tem plat e> and < HeaderTem plat e> . Repeating Data Control Templates There are five t em plat es available t o t he repeat er cont rol: z

ItemTemplate

z

AlternatingItemTemplate

z

HeaderTemplate

z

FooterTemplate

z

SeparatorTemplate

The I t em Tem plat e of a repeat er web cont rol ident ifies t he HTML code t hat will be displayed for each row of dat a bound t o t he cont rol. This HTML can be as sim ple as a com m a t o build a com m a- delim it ed list , or it can be part of a com plex t able st ruct ure used t o posit ion each row of dat a on t he page. The < I t em Tem plat e> t ag of one of our com bined search repeat er cont rols looks like t his:

Data Goes Here!

This is sim ple enough; for each row of dat a t hat is bound t o our repeat er cont rol, a new HTML t able row wit h a single colum n will be creat ed. HTML rows cannot be displayed wit hout a < t able> ..< / t able> definit ion. We could place t hese t ags on eit her side of our repeat er cont rol, but t here is a bet t er place, as we will discuss lat er in t his sect ion. There is one problem , however; for every row of dat a bound t o our repeat er cont rol, t he phrase " Dat a Goes Here! " will appear! Since t his is clearly not what we want , we need t o replace t hat phrase wit h t he act ual dat a. So t hat by using t he Eval m et hod of t he Dat aBinder obj ect . Replace " Dat a Goes Here! " wit h t he following:

This code t akes t he cont ainer obj ect and looks for a piece of dat a nam ed " Event _Nam e." I f t he dat a is found, t hen it replaces t he server- side code wit h t he dat a t hat will be displayed wit hin t he t able cell defined in our I t em Tem plat e.

Tip: Although Microsoft recommends developers use the previously mentioned method, you can also use the following shortcut to access a container object's data: Container.DataItem("Event_Name")

I n addit ion t o I t em Tem plat es, repeat er cont rols also support HeaderTem plat es and Foot erTem plat es. These t em plat es are only execut ed once per repeat er cont rol inst ead of once per row, like t he I t em Tem plat e. The HeaderTem plat e execut es j ust before t he first I t em Tem plat e and t he Foot erTem plat e execut es j ust aft er t he last row of dat a is displayed in t he I t em Tem plat e. Thus, we will place t he < t able> t ag in t he HeaderTem plat e and t he < / t able> t ag in t he Foot erTem plat e. I t m ay seem t hat t hese t wo opt ions are redundant since bot h achieve t he sam e effect on t he result ing web page. However, one of t he key concept s in t he .NET Fram ework is m odular obj ect s. By including t he < t able> t ags in t he repeat er cont rol, we m ake t he cont rol com plet ely m odular and easily m oved elsewhere on t he page or t o anot her page. The Repeat er cont rol is designed t o be com plet ely self- cont ained and t o require no support from it s parent page. We will add one m ore feat ure t o t he HeaderTem plat es of each repeat er cont rol for aest het ics. Just aft er t he < t able> t ag, we will add a t it le row using t he following code: Tours

We will replace " Tours" wit h t he appropriat e t it le ( Event s, Part icipant s, Venues) for each of our repeat er cont rols. There are t wo m ore t em plat es support ed by t he repeat er cont rol. The first is t he Separat orTem plat e, which is insert ed bet ween each pair of I t em Tem plat es. This is useful for creat ing sim ple list st ruct ures such as delim it ers bet ween dat a it em s. I t is im port ant t o not e t hat t he Separat orTem plat e is only used bet ween t wo I t em Tem plat es; if t here is only a single row of dat a, t he Separat orTem plat e will never be used. Two exam ples of using t he repeat er t em plat es are showing in figure 10.6. The repeat er cont rol can be used t o m ake com m a- delim it ed list s or organize dat a int o t ables. The final t em plat e available t o t he repeat er cont rol is t he Alt ernat ingI t em Tem plat e. This innovat ion is a huge t im esaver and product ivit y enhancer. The Alt ernat ingI t em Tem plat e is used for t he second row of dat a and every even num bered row t hereaft er. This m akes it t rivial t o creat e t he st andard t able st ruct ure where every ot her row is shaded different ly t o enhance readabilit y. I n order t o enhance our com bined search page wit h alt ernat ely shaded out put rows, we would add t he following t em plat e im m ediat ely below our I t em Tem plat e:





Figure 10.6

This creat es a t able in which every even- num bered row has a light - grey colored background, while t he odd- num bered row s have t he st andard whit e background, enhancing readabilit y. Note: If an AlternatingItemTemplate is not present, the ItemTemplate is used for every row of data.

Converting a Classic ASP Page to Use a Repeater Control As discussed in Chapt er 5, " I m plem ent ing .NET Present at ion Services," t here are m any reasons t o convert exist ing ASP pages t o ASP.NET pages. To convert an exist ing dat a- driven ASP page int o a new ASP.NET page, sim ply copy t he ASP code int o t he HTML view of t he ASP.NET page. Then, insert a < Repeat er> cont rol on t he page, wrapping t he ASP code for displaying dat a. Then, insert Header- and Foot erTem plat es for any code t hat is j ust out side t he loop t hat displays dat a. Finally, insert an I t em Tem plat e where you see t he following code: do while not rs.eof

You can det erm ine t he need for an Alt ernat ingI t em Tem plat e by checking t o see if t here is any code inside t he new I t em Tem plat e t hat looks likes t his: counter=counter+1 if counter mod 2 = 0 then else end if

The count er m od 2 = 0 check is a com m on t echnique used t o qualify every ot her line. I f we increm ent count er each t im e t hrough t he loop, t hen count er m od 2 will evaluat e t o t rue every ot her t im e. To use t he Repeat er cont rol, get rid of t he if..t hen const ruct and m ove t he first < t d> definit ion t o t he Alt ernat ingI t em Tem plat e. Then, copy all of t he rem aining display code in t he I t em Tem plat e t o t he Alt ernat ingI t em Tem plat e. There are t wo rem aining st eps necessary t o hook t he new repeat er cont rol up t o real dat a. The first is t o replace any exist ing references t o t he recordset nam ed rs in t he old ASP page t o t he Dat aBinder.Eval m et hod list ed in t he previous code. So we would convert :

int o

The last st ep is t o elim inat e t he code in t he ASP page t hat ret rieves dat a int o t he recordset . Now you need t o creat e new code t hat binds t he ent ire Repeat er const ruct t o a set of dat a pulled from t he dat abase. Web Control Event Handling You now have a web form wit h a t ext box, a but t on, and a repeat er cont rol t hat will serve as t he basis for t he com bined search funct ionalit y. However, you do not yet have any code t o accept input from t he user, or t o use t hat input t o ret rieve event s, t ours, part icipant s, and venues from t he dat abase. I n order t o run code t o bind t he repeat er cont rol t o a dat a set , you need a place t o run it from . That is, you need an event handler for t he but t on cont rol so t hat you can writ e code t hat will execut e only aft er t he user presses t he but t on. This calls for event - driven program m ing. The concept of event - driven program m ing is new t o ASP, but is very fam iliar t o applicat ion program m ers. An event - driven program is sim ply broken int o chunks of code t hat are only execut ed when cert ain event s occur, or are " fired." When an event fires, t he associat ed code is execut ed and t he applicat ion goes back t o wait ing for t he next event . I n t he world of ASP.NET, event s are fired when users press a but t on, select an it em from a list box, or click a link. The event handler code can t hen respond t o t he event by changing dat a on t he web form and redisplaying it , or by passing cont rol t o a new web form . The easiest way t o creat e an event handler for t he click event of t he but t on cont rol is t o sim ply double- click on t he but t on in design m ode. Visual St udio.NET aut om at ically creat es t he necessary code for t he handler and swit ches t he view t o t he code so t hat you can im m ediat ely st art writ ing t he code for t he handler. The event handler definit ion looks like t his: Public Sub butSearch_Click(ByVal sender as Object, ByVal e as System.EventArgs)

As we exam ine t he code page, we see t wo ot her event s handlers already defined and filled wit h code. The Com binedSearch_I nit and Com binedSearch_Load event s were aut om at ically creat ed by Visual St udio.NET when we creat ed t he Com binedSearch.aspx. These t wo event s handle creat ing t he cont rols on t he web form and allow t he developer t o execut e code before t he page is processed and sent t o t he client . The web form load ( Com binedSearch_Load) event is very useful for filling default values int o t ext boxes and binding cont rols t o dat a. I t also plays a large role in t he separat ion of design and code, as we will see lat er in t his chapt er.

I f you swit ch back t o t he Com binedSearch.aspx file by clicking t he t ab j ust above t he code area, and t hen swit ch t o HTML view, you can see t he code insert ed int o t he ASP: But t on cont rol t o t ie it t o t he click event handler. A new param et er has been added t o t he cont rol: onClick="butSearch_Click"

This t ells .NET t hat when t he user clicks t his but t on, t he cont rol should be passed back t o t he server t o run t he " but Search_Click" event handler. Any code found in t he event handler will be execut ed and t he page will be reloaded. We t alked about t he t wo event s creat ed aut om at ically for every ASP.NET page. I t is im port ant t o not e t hat , by default , t hese event s are " wired up" t o t he page, t hat is t hey will be executed every t im e t he page is loaded. I t is possible t o t urn t hese event s off, however, so t hat t hey do not execut e. Do t his by adj ust ing t he Aut oEvent Wireup at t ribut e of t he Page direct ive. The first line of t he Com binedSearch.aspx HTML is: Link

The downside of t his is t hat t he cont ext - swit ching t hat is required t o m ake t his link dynam ic is known t o have a negat ive perform ance im pact t o your sit e. Wit h t he Hyperlink server cont rol, t his cont ext swit ching is no longer necessary. All of t he processing is handled for you by t he ASP.NET fram ework. To get it s dat a, t he ViewSeat ingChart .aspx page can use t he Event I D t o access t he Event Dat a obj ect it needs t o ret rieve t he nam e of t he im age file t o show and t he nam e of t he venue it is displaying. This page is not crit ical t o t he operat ion of t he Websit e, but was provided as a usabilit y benefit t o a cust om er who was unfam iliar wit h t he part icular venue. The Seat ing Chart allows t hem t o get a bet t er view of t heir seat s so t hey can have a bet t er view of t he event . We designed our sit e so t hat t he Seat ing Chart is available from t he Event Det ails and t he Billing, Shipping, and Paym ent Collect ion pages.

The UI for Purchasing a Ticket At t his point in t he chapt er, we are going t o m ove t o purchasing a t icket from t he gasBAGS point of view. Recall t hat gasBAGS is a Websit e for t he fict ional gasBAGS band, which uses t he dat a from t he gasTI X search and purchase Web services. As m ent ioned previously, t he code for accessing business obj ect s is very sim ilar, whet her t he reference t o t hose obj ect s is local or m ade t hrough t he I nt ernet . The only difference will com e in how each sit e chooses t o design and display it s dat a. I n order t o highlight bot h t he differences bet ween t he Websit es and offer proof of t he seam less int eroperabilit y of t he languages, gasBAGS has been writ t en in Visual C# .NET. Figure 11.7 shows t he gasBAGS order page. Note: The Web services used in this chapter are built in Chapter 6, "Implementing .Net Business Services." Please see that chapter for an understanding of how Web services are created.

Figure 11.7 Order page for t he gasBAGS Websit e as seen from I nt ernet Explorer.

List ing 11.9 dem onst rat es how t o bind t he dat a ret urned from t he Web service t o our server cont rols. List in g 1 1 .9 Bin din g a D r opD ow n List t o a n XM L W e b Se r vice

namespace gasBAGS {

using System; using System.Web.UI; using System.Data; using System.IO; using gasTIX.ParticipantProxy;

//-- define the Order class public class Order : Page {

protected System.Web.UI.WebControls.DropDownList bill_to_region_id; … //-- this function initializes the pages controls with default values protected void LoadPageControls() { try { //-- get reference to the gasTIXWeb Participant assembly Participant ParticipantProxy = new Participant();

//-- get regions DataSet dsRegions = new DataSet("Regions");

AM FL Y

StringReader srRegions = new StringReader(ParticipantProxy.ListRegions()); dsRegions.ReadXml(srRegions);

//-- load regions for bill to

bill_to_region_id.DataSource = dsRegions.Tables[0].DefaultView; bill_to_region_id.DataTextField = "short_name";

TE

bill_to_region_id.DataValueField = "region_id";

Not ice t hat t he Part icipant class can be inst ant iat ed like any ot her class. Once we have a Part icipant obj ect , we can call t he List Regions( ) m et hod, which ret urns an XML st ring of dat a represent ing all regions where gasTI X current ly sells t icket s. We can t hen bind our bill_t o_region_id Text Box Web cont rol direct ly t o t hat XML st ring. Let 's walk t hrough how t his is accom plished. Participant ParticipantProxy = new Participant();

Here we creat e our reference t o t he Part icipant .DLL, which is our proxy for Web service dat a. I nst ead of using Server.Creat eObj ect as in ASP, we j ust need t o creat e a reference t o t he DLL locat ed in t he bin direct ory of our Websit e. //-- get regions DataSet dsRegions = new DataSet("Regions"); StringReader srRegions = new StringReader(ParticipantProxy.ListRegions()); dsRegions.ReadXml(srRegions);

I n t he above code snippet we m ake t he call t o our Web service ( im plem ent ed via t he SOAP prot ocol) t o get a list ing of regions t o load in our dat a set . Since our dat a is being t ransm it t ed as St ring dat a over t he SOAP

Team-Fly®

prot ocol we use t he St ringReader call t o serialize t he dat a. We t hen call t he ReadXML m et hod on t he dat aset t o load our st ring dat a. //-- load regions for bill to 5) bill_to_region_id.DataSource = dsRegions.Tables[0].DefaultView; 6) bill_to_region_id.DataTextField = "short_name"; 7) bill_to_region_id.DataValueField = "region_id"; 8) bill_to_region_id.DataBind();

From here, we j ust need t o assign t he dat aset as t he dat a source t o our DropDownList cont rol and specify which fields in t he dat aset t o use. Finally, we call t he BindDat a m et hod t o bind t he dat a source t o t he server cont rols. All of t he preceding code is found in t he code- behind page for Order.aspx. List ing 11.10 shows how we wir e up t he server cont rols using page direct ives and server side cont rol definit ions. List in g 1 1 .1 0 Th e H TM L Ve r sion of t h e Or de r .a spx Pa ge .



… State:



The @Page direct ive in List ing 11.10 inform s t he ASP.NET parser t o include our code behind page specified by t he SRC at t ribut e. I n t his exam ple, t he SRC at t ribut e point s t o t he ORDER.CS page discussed earlier. Not ice t hat t he form and server cont rols have t he RUNAT at t ribut e set t o " Server" . We specify t he ASP: DROPDOWNLI ST cont rol t o display a list of t he regions support ed by gasTI X. Not ice t hat we do not have any inline code for filling t he drop down cont rol. I n " old st yle" ASP, we would have em bedded script logic t o fill t he cont rol or inst ant iat ed a COM obj ect and t hen looped t hrough building all of t he OPTI ONS for t he cont rol. Now, all of t he logic is in t he ORDER.CS page. This m akes for a clean division bet ween t he present at ion and logic layers of t he Web applicat ion.

Tools When using Web services we can use t he feat ures in Visual St udio or t he com m and line t ools t hat are also included. However, using t he environm ent in Visual St udio offers a richer developm ent experience when building web services since Visual St udio offers all of t he " I nt elliSense" and synt ax checking feat ures.

Visual Studio .NET This will probably be t he preferred m et hod for m ost developers when using XML Web services. We can add references t o XML Web services not only from local m achines, but also from t he following rem ot e locat ions where: z

The developer is explicitly aware of the XML Web service being exposed and knows the URL for accessing that XML Web service. This is derived from a file with the .DISCO file extension or the SDL contract from a file with the ASMX extension. For more information, see Discovery of Web Services (DISCO) at msdn.microsoft.com/xml/general/disco.asp.

z

The XML Web service is exposed via UD D I (Universal Discovery, Description and Integration). UDDI is a distributed directory of published XML Web services. For more information, see Universal Discovery, Description and Integration at www.uddi.org.

Since Visual St udio .NET is t ight ly int egrat ed wit h t he .NET Fram ework, t his is our t ool of choice when building t he XML Web services for t he gasBAGS Websit e. When a Web service is built using Visual St udio .NET, a file wit h t he .vsdisco ext ension is added t o t he proj ect . I n Managed Visual C+ + .NET, t his file ext ension is .disco. When we built our exam ples for using XML Web services from t he gasBAGS Websit e earlier in t his chapt er, we added Web references from Visual St udio .NET t hat discovered t he m et hods available by int errogat ing t he .vsdisco files. Let 's get st art ed wit h Visual St udio .NET and im plem ent t he logic t o use an XML Web service from gasTI X. I n t his exam ple we are going t o connect t o t he gasTI X server and discover t he Web services available for use in t he gasBAGS Websit e. To begin, right - click on t he proj ect nam e in t he Solut ion Explorer and select " Add a Web Reference." The dialog is shown in figure 11.8.

Figure 11.8 The Visual St udio .NET Add a Web Reference page.

From t his dialog, we can ent er t he URL of t he Web server where t he XML Web service is locat ed, or we can select one of t he UDDI direct ories provided by Microsoft . For t he gasBAGS exam ple, ent er t he nam e of t he gasTI X Web server, www.gasTI X.com / . Aft er j ust a m om ent , we should see a list of XML Web services t hat are available for consum pt ion. Select t he gasTI X Discovery service. This will display a list of XML Web services available t o us. We will click " Add Web Reference" t o add a reference t o t his XML Web service and copy a proxy of t he .DLL t o our local m achine. I n your proj ect you should now see a Web References node, and under t hat a node referencing t he gasTI X Web server. When t he gasTI X server node is expanded you will see a WSDL file and a DI SCO file. The WSDL file is t he XML file describing t he m et hods available in t he XML Web service. The DI SCO file is t he discovery file used by Visual St udio .NET when searching for XML Web services. Tip: It is important to remember that a copy of the XML Web service assembly is copied to our local machine. If the Web service is updated, we need to refresh our Web reference to update our local copy with the new definition.

Now t hat we have a reference t o our XML Web service, we need t o wire it up in code before we can ut ilize it . I n our exam ple, double- click on t he WebForm 1.ASPX file. Right - click on t he WebForm 1.ASPX file and click " View Code" . We need t o add a reference t o t he XML Web service t hat we referenced from t he gasTI X XML Web server. I nsert t he following st at em ent : Using gasBAGS_Demo.gasTIX;

Now, insert t he following code int o t he Page_Load event :

DataSet ds = new DataSet("Events"); Participant Events = new Participant(); StringReader sr = new StringReader(Events.ListEvents()); ds.ReadXml(sr); DataGrid1.DataSource = ds.Tables[0].DefaultView; DataGrid1.DataBind();

As you can see, we only need t o creat e a reference t o t he obj ect and t hen we can call t he m et hods of t his Web service. Select View Designer on t he WebForm 1.ASPX page. I f t he page is not in Design m ode, select ed t hat opt ion at t he bot t om of t he page. Drag t he Dat aGrid cont rol ont o t he page layout . Since t he default nam e of t he cont rol is Dat aGrid1, we have already referenced t his in t he Page Load event . Hit t he F5 key t o build t he exam ple solut ion. Once t he solut ion is built , a browser window should pop up and display t he list of event s available from t he gasTI X Web server. As you have seen, finding and using XML Web services t hrough Visual St udio .NET is sim ple. Once you have a Web reference m ade, developm ent is com plet ed as t hough t he com ponent was local.

Command-Line tools The .NET Fram ework also supplies com m and line t ools for building ASP.NET pages t hat use XML Web services. Each language has it s own com m and- line com piler ( csc.exe is t he com piler for Visual C# .NET) , and t he WSDL.exe ut ilit y allows developers t o build XML Web service proxy DLL's. Tip: Please refer to the .NET SDK documentation for complete details on all of the command-line options available with the above tools.

Aft er an XML Web ser vice proxy .DI L is generat ed and copied t o our bin direct ory using t he WSDL ut ilit y, we can inst ant iat e and call any of t he public m et hods included in t he class. So, before we can im plem ent t he logic in our ASPX pages, we need t o creat e our m odule. Open a com m and prom pt and t ype t he following: wsdl /n:gasTIX.ParticipantProxy /protocol:SOAP http://www.gasTIX.com/Participant.asmx?SDL

The above com m and line locat es t he gasTI X Part icipant XML Web service and creat es a C# proxy class on our m achine. We could have specified a different out put filenam e wit h t he / out : param et er. Before we com pile our source t o creat e our m odule, let 's discuss t he com m and line param et ers used. They are: z

/n--Gives a namespace to our module

z

/protocol:SOAP--Specifies to use the SOAP protocol

z

http://www.gasTIX.com/Participant.asmx?SDL-- Specifies the URL where we can find the WSDL contract for this XML Web service.

Also, we did not specify t he language on t he com m and line, since C# is t he default . Now t hat we have generat ed t he source code file, we need t o com pile it and m ove in t o our BI N direct ory. To com pile t he source file, execut e t he following line at t he com m and prom pt : csc /t:library /r:system.Web.services.dll /r:system.xml.serialization.dll PARTICIPANT.CS

Aft er execut ion, our m odule ( DLL) is produced and ready t o be copied int o t he BI N direct ory. As wit h t he WSDL com m and- line t ool, we will exam ine t he com m and line argum ent s used t o generat e t he m odule.

/t:library

This opt ion specifies t hat we are generat ing a LI B file. /r:system.Web.services.dll and /r:system.xml.serialization.dll

For each .NET Fram ework class t hat we reference, we need t o include t he nam e of t he class.

/ r argum ent followed by t he

PARTICIPANT.CS

Finally, we specify t he nam e of t he source file t o be com piled. While it is not as easy or convenient as using Visual St udio .NET, t here are cases where it m ay be im port ant t o be able t o reference XML Web services and build C# proj ect s from t he com m and line. The .NET Fram ework gives you t he t ools t o do so.

From Here Purchasing a t icket on t he gasTI X Websit e involved a series of st eps t hat each capt ured a different segm ent of t he order. Som e of t he t hings t hat we accom plished in t his sect ion were t o: z

use the new Server Controls to efficiently display and collect information from our Customer

z

use the DataSet object to easily retrieve and record our customer's information

z

use the Session object to persist our DataSet order form from page to page

z

successfully interface with a business layer of code that was entirely written in a different programming language: Visual C# .NET

Visual St udio .NET and Visual Basic .NET provide a st rong foundat ion for building t he next generat ion of Web applicat ions. Visual Basic .NET is now a world- class, obj ect - orient ed developm ent language wit h m any new and im proved feat ures. Visual St udio .NET provides a rich, st able environm ent for developing applicat ions wit h t he new .NET fram ework t echnologies. Also, t he im provem ent s t o t he ASP Session obj ect , and t he opport unit y t o use code- behind pages t o sim plify our coding, m akes ASP.NET t he prem ier Web developm ent t echnology. All wrapped up t oget her, t he .NET fram ework provides t he best suit e of t ools and t echnologies t o build prem ier ent erprise applicat ions. I n t his chapt er we t ook a det ailed look at t he purchasing process from t he gasTI X server. We covered t he UI , how t he dat a is rem ot ed t hrough t he m iddle t ier, and finally com m it t ed t he t ransact ion t o t he dat abase. We also int roduced t he COM, WebMet hod, and Transact ion at t ribut es. Wit h t he advent of .NET and t he nat ive XML form at , consum ing dat a via XML Web services from any plat form is m uch easier, wit h a m inim al am ount of code.

1 2 Fu lfillm e n t N ot e : Th is ch a pt e r a ssum e s t h a t t he r e a de r h a s som e fa m ilia r it y w it h BizTa lk Se r ve r ( BTS) . A fu ll discu ssion of in st a llin g a n d im ple m e n t in g BTS is be yon d t h e scope of t h is ch a pt e r .

This chapt er dem onst rat es how we im plem ent ed t he gasTI X fulfillm ent process. I n our sam ple applicat ion, t his involves print ing and shipping t icket s t o a cust om er. I n m any real- wor ld organizat ions, and in part icular e- Business st art - ups, t here are not sufficient resources t o handle fulfillm ent needs in- house. For t his reason, t asks such as warehousing, packaging, shipping, and print ing are rout inely out sourced t o vendors called t hird- part y adm inist rat ors ( TPA) . A TPA who focuses exclusively on t hese t asks can be far m ore effect ive and efficient in deliver ing services. While out sourcing solves one problem , it int roduces anot her. The syst em which was previously self- cont ained now m ust int eract wit h anot her syst em over which it has no cont rol. The challenges here can be num erous. St at e of com m unicat ions wit h t he vendor's syst em m ust be t racked. The st at us of your dat a in t he vendor's syst em m ust be t racked. The t ransact ions wit h t he vendor m ay be spread out over a significant t im e. The com m unicat ion channels m ay not be reliable ( bot h bet ween com put er syst em s and t he am ong t he syst em s int egrat ors t hem selves) . I n spit e of t hese pot ent ial challenges, it is a pat h t hat is chosen every day. And rat her t han buy all of t he print ers and post age m achines, gasTI X also chose t o out source our fulfillm ent operat ions. The business requirem ent s for fulfillm ent are very sim ple. Once an order for t icket s has been placed, t he appropriat e t icket s need t o be print ed, st uffed in an envelope and shipped t o t he cust om er in a t im ely m anner via t heir shipper of choice. gasTI X also needs t o be able t o provide t he cust om er wit h t he st at us of t heir t icket s at any given t im e. The t icket print ing service we chose ( which we'll sim ply refer t o as TPA) can provide us all of t hese funct ions. Since we also creat ed t he TPA for our sam ple app, we m ade sure t he t echnologies fit our needs as well. Com m unicat ion wit h t he TPA is via ht t p over t he int ernet and t he m essages are encoded in XML. Not e t hat for t he purposes of our sam ple applicat ion, t icket s are em ailed rat her t hen act ually print ed and m ailed. There are dozens of t echnologies we could have chosen from t o im plem ent fulfillm ent in t he .NET world given t he loose requirem ent s above. The possibilit ies ranged from hard coding everyt hing using old I SAPI calls t o using brand new web services. We t hought t hat fulfillm ent gave us a perfect opport unit y t o use Microsoft 's lat est weapon in t he bat t le of syst em s int egrat ion, BizTalk Server 2000 ( BTS) , a part of t he .NET fam ily of servers. BTS has t wo processing engines, m essaging and orchest rat ion. BizTalk m essaging provides dat a t ransform at ion and dat a t ransport services. BizTalk orchest rat ion provides workflow processing and applicat ion int egrat ion. We've used bot h engines t o handle com m unicat ion wit h t he TPA.

Com m unica t ion Ove r vie w Let 's look at t he com m unicat ions involved bet ween t he gasTI X sit e and our TPA and t hen get int o t he specifics of each port ion. There are processes running on t hree physical layers t hat m ust com m unicat e. First t here is t he business layer cont aining all of our .NET com ponent s which resides on our gasTI X web servers. This layer is responsible for kicking off a chain of event s when a new order is placed. I t m ust also provide an int erface t hat allows t he st at us of orders t o be updat ed. The second layer is t he TPA which will be running on it s own set of servers; it will provide a web int erface for subm it t ing orders t o be print ed. I t will also fulfill t hose orders ( via em ail) and not ify t he request or ( via an HTTP post ) as t he st at us of t he order changes during t he fulfillm ent process. The final layer is BTS which is inst alled on it s own server and will serve as t he coordinat or bet ween t he ot her t wo layers. I n a m ore serial fashion, illust rat ed in Figure 12.1, here are t he st eps of order fulfillm ent :

z

ga sTI X m iddle t ie r com ple t e s a n or de r a n d n ot ifie s BTS

z

BTS t r a n sfor m s t h e or de r t o t h e TPA for m a t

z

BTS se n ds t h e or de r t o t h e TPA

z

TPA a ck n ow le dge s a n or de r w a s r e ce ive d.

z

TPA pr oce sse s t h e or de r

z

TPA sh ips t h e or de r , if possible

z

TPA se n ds t h e pr in t or de r st a t u s ba ck t o ga sTI X

z

BTS r e ce ive s t h e st a t u s a n d n ot ifie s t h e m iddle t ie r .

.Net middle tier

Middle Tier will update database with status and take other appropriate actions.

gasTix Order

1) Send order to Biztalk

gasTIX Order

gasTix Biztalk 2) Transform order to TPA format

If no acknowledgement recieved, Biztalk will resend message.

8) Notify middle tier of updated order status

TPA Order

3) Send order to TPA

TPA

TPA Order

Figu r e 1 2 .1 Overview of com m unicat ions involved in fulfillm ent .

4) Acknowledge order

5) Process order

7) Return Order Status

6)If Success, Send Tickets to customer

N ot ifica t ion of N e w Or de r s Once t he cust om er has finished purchasing t heir t icket s, t he m iddle t ier gat hers and processes all of t he inform at ion. Now t he fulfillm ent process begins by passing a m essage cont aining an order t o BTS. There ar e t hree decision point s surrounding t he passing of a m essage from t he m iddle t ier t o BTS. These are: z

Th e for m a t of t he m e ssa ge

z

Th e pr e cise st r u ct u r e of t h e m e ssa ge

z

Th e t e ch n iqu e for t r a n spor t in g t h e m e ssa ge

Wit h gasTI X, t he first t wo decisions are easy as we w ill see.

M e ssa ge For m a t a n d St r u ct u r e BTS can handle m essages in several form at s, including XML, com m a- separat ed files, and various EDI form at s. XML is t he nat ive form at for inform at ion in BTS; if a m essage is received in any ot her form at , BTS im m ediat ely convert s it t o XML for int ernal processing. Since t he m iddle t ier is already holding an XML version of t he com plet ed order in a dat a set , t his is t he m ost nat ural form at t o use. This answers t he second quest ion as well. The sim plest solut ion is t o use t he st ruct ure of t he XML dat a set t hat t he m iddle t ier is alr eady using. One of t he m arquee feat ures of BTS is dat a t ransform at ion. BTS can t ake any inbound m essage it receives and convert it t o our t rading part ner's st ruct ure and form at . And conversely it can t ake our t rading part ner's st yle and form at and convert it t o a form at our syst em s can underst and. So using t he dat a or m essage st ruct ure t hat we're already working wit h is t he generally t he best solut ion. The caveat is t hat all necessary inform at ion for our t rading part ner m ust be included or derivable from t he set of inform at ion we're sending. Also, fut ure changes t hat are likely t o be incorporat ed by our t rading part ner ( or perhaps a fut ure t rading part ner) should be considered. Som e ext ra inform at ion in t he m essage is not a bad t hing and m ay act ually save effort in t he fut ure. I f t here is an ext ra field in t he m essage t hat our part ner m ay need in six m ont hs, we've reduced our fut ure m iddle t ier work and lim it ed changes t o t he BTS layer, at very lit t le cost t o us now. N ot e : Tr a din g pa r t ne r is a t e r m u se d in BizTa lk docum e n t a t ion t o ge ne r ica lly de scr ibe a n e nt it y t ha t you ’r e doin g bu sin e ss w it h e le ct r on ica lly. I n ou r sa m ple a pplica t ion , t h e TPA is a t r a din g pa r t ne r .

M e ssa ge Tr a n spor t The final decision t o m ake is what m et hod is used t o send t he m essage t o BTS. Two choices are available: receive funct ions and direct COM+ calls. Receive funct ions allow BTS t o m onit or Microsoft Message Queue ( MSMQ) and file syst em locat ions for m essages. A BTS service runs, wait ing for an appropriat e m essage t o be det ect ed. When t his happens, it is picked up and rout ed t o t he appropriat e BizTalk channel for processing. For MSMQ receive funct ions, a queue nam e m ust be specified. For file receive funct ions, a file nam e, wit h wildcards allowed, and a direct ory m ust be specified as shown in Figure 12.2. Eit her t ype allows rout ing direct ives t o be provided t o BTS as shown in Figure 12.3. Receive funct ions are set up and m aint ained from t he BTS adm inist rat ion console.

Figu r e 1 2 .2 Dialog for adding a receive funct ion.

The ot her opt ion for sending m essages t o BTS is a COM+ call. This int erface is provided via t he I I nt erchange class. The Subm it m et hod of t his class allow s an asynchronous t ransm ission of t he m essage. Once t he m essage has been subm it t ed t o t he BTS m essaging engine, execut ion can cont inue. There is also a Subm it Synch m et hod t hat allow s you t o wait for t he BTS engine t o successfully process and t ransport t he m essage. The form at of t he Subm it m et hod is: Submit( OpennessFlag, Document, DocName, SourceQualifier, SourceID, DestQualifier, DestID, ChannelName, FilePath, EnvelopeName, PassThrough)

AM FL Y

Figu r e 1 2 .3

TE

Dialog for specifying rout ing direct ives for a receive funct ion.

N ot e : I t is w or t h n ot in g t h e sim ila r it ie s be t w e e n t h e pa r a m e t e r s in t h e Su bm it ca ll a n d t h e r out in g opt ion s a va ila ble on t h e pr ope r t ie s of a r e ce ive fu n ct ion. Th e r e is r e a lly n o loss of fu n ct ion a lit y by ch oosin g on e m e t h od ove r t h e ot h e r sin ce bot h ca n t a k e on va lue s lik e ch a n n e l, docu m e n t t y pe , a n d de st in a t ion I D . Th e se pa r a m e t e r s cont r ol h ow a m e ssa ge is pr oce sse d in BTS m e ssa gin g. Eve r y m e ssa ge w ill t a k e one pa t h t hr ou gh BTS ca lle d a ch a n ne l. W h e n su bm it t in g a m e ssa ge t o BTS, t h e se pa r a m e t e r s ca n a id BTS in it s ch a n n e l se le ct ion pr oce ss. Ve r y spe cific in for m a t ion ca n be pr ov ide d su ch a s t h e e x a ct ch a n n e l t o u se . M or e ge n e r a l in for m a t ion ca n be pr ov ide d, su ch a s a de st in a t ion or ga n iza t ion or a docu m e n t n a m e , t o a id BTS in se le ct in g a ch a nn e l. I f n o in for m a t ion is pr ovide d, BTS w ill look for sim ila r in for m a t ion e m be dde d in side of t he m e ssa ge , m a k in g t h is a se lf- r ou t in g docu m e n t . Fin a lly, la ck in g a n y ot h e r in for m a t ion , BTS w ill a t t e m pt t o de t e r m in e t h e for m a t of t he m e ssa ge a n d se le ct a cha nn e l a ccor din gly. Th e r e is a gr e a t de a l of fle x ibilit y on h ow m e ssa ge s ca n be r ou t e d t h r ough BTS. Tip: I f n o r ou t in g in for m a t ion is pr ovide d t o BTS, be ca r e fu l t o e n su r e you r BTS se t u p is n ot a m bigu ou s ( in ot h e r w or ds, on ly on e ch a n ne l is va lid for a n y a n t icipa t e d in bou n d m e ssa ge ) . N ot e : BTS cla im s t o a cce pt m e ssa ge s fr om a m y r ia d of sour ce s su ch a s H TTP, FTP, SM TP, a n d M SM Q, but t h e r e is a ct u a lly som e sle igh t of ha n d goin g on t o a ccom plish t h is. Cu r r e n t ly, t he on ly t h r e e m e t h ods of ge t t in g a m e ssa ge in t o BTS a re via a dir e ct fu nct ion ca ll v ia I I nt e r ch a n ge , a n M SM Q r e ce ive fu n ct ion , or a file r e ce iv e fu n ct ion . The ot h e r t r a nspor t s a r e " im ple m e n t e d" by u sin g t h e t hr e e m e n t ione d. For e x a m ple , FTP is su ppor t e d by ha v in g a file r e ce ive fu n ct ion m on it or t h e FTP t a r ge t dir e ct or y. An H TTP post is su ppor t e d by u sin g a n ASP pa ge t ha t pa sse s t he m e ssa ge a lon g t hr ough e it h e r M SM Q or I I nt e r cha n ge . SM TP is su ppor t e d by usin g e ve nt s or e ve n t sink s on spe cific folde r s t ha t pa ss t h e ir m e ssa ge t o BTS via

Team-Fly®

I in t e r cha n ge . Th e ba r e m in im u m w a s in clu de d in t h e fir st r e le a se of BTS, w it h pla n s for ot h e r pr ot ocols be in g dir e ct ly su ppor t e d in fu t ur e r e le a se s.

For gasTI X, we chose t o m ake a direct asynchronous call from t he m iddle t ier. The reason for t his choice was t hat we want ed t o m ake direct use of a COM+ com ponent from .NET code. The call we m ade is shown in List ing 12.1. List in g 1 2 .1 Fu n ct ion t o Su bm it M e ssa ge t o BizTa lk Private Function SubmitOrderToBTM(ByVal orderXML As String) As String Dim objSubmit As BTSInterchangeLib.IInterchange = New BTSInterchangeLib.Interchange() Dim strHandle As String Dim strChannel As String strChannel = "gasTIXOrderToOrchestration" Try strHandle = objSubmit.Submit( BTSInterchangeLib.BIZTALK_OPENNESS_TYPE.BIZTALK_OPENNESS_ TYPE_NOTOPEN, orderXML, "", "", "", "", "", strChannel, "", "", 0) Catch e As Exception Throw e End Try Return strHandle End Function

A reference t o t he I I nt erchange obj ect was m ade available t o t he web/ m iddle t ier box by inst alling an applicat ion proxy reference t o t he BizTalk Server I nt erchange Applicat ion t hat resides in COM+ on t he BTS box. When t he reference was set , t he Visual St udio.NET I DE act ually generat ed a .NET wrapper library for t he COM+ obj ect . The t opic of COM+ int eroperabilit y is discussed in det ail in Chapt er 9, “ COM+ I nt egrat ion” . A m ore det ailed look at t he Subm it m et hod and it s param et ers is provided lat er t his chapt er in t he sect ion on Correlat ion. A call is m ade using t he Subm it funct ion, which will allow t he m essage t o be processed asynchronously. The only param et ers passed t o t he subm it funct ion are t he openness t ype, t he XML m essage cont aining t he order it self, and t he channel nam e. I n our exam ple, we knew exact ly which channel we want ed t o go t o and specified it . More specific inform at ion is alw ays bet t er when it 's available. Aft er t he call t o subm it is m ade, t he m iddle t ier can exit it s processing for t his order. BTS can t ake over t he fulfillm ent processing.

M e ssa ge Tr a n spor t Alt e r n a t ive s Of t he t hree decision point s t hat were m ent ioned previously, only t he last one is quest ionable. Using XML was an easy choice since t he inform at ion was readily available in t hat form at from our sit e and it is t he nat ive form at for BTS. The st ruct ure of t he m essage was also easy t o select , since t he m essage already exist ed and cont ained all of t he inform at ion we needed. The decision t o pass t he m essage as a direct call, however, could be argued. One good alt ernat ive would have been t o subm it t he or der m essage t o MSMQ inst ead of direct ly t o BTS. This would solve som e reliabilit y problem s, since connect ivit y t o t he BTS m achine would never be an issue. I n t he case of server or net work problem s, t he m essage sit s in t he queue, wait ing unt il syst em condit ions allow it t o be delivered. This m akes t he int erface wit h BTS t ruly asynchronous. Using a m essage queue also prevent s t he web/ m iddle t ier servers from being t ied direct ly t o part icular BTS servers. Being t ied t o a part icular m achine int roduces reliabilit y and scalabilit y problem s. Finally, using MSMQ also elim inat es t he need t o inst all t he applicat ion proxies t o BTS on t he web/ m id t ier m achines.

One variat ion of t he direct call would be t o use Applicat ion Cent er's com ponent load balancing. By load balancing BTS across m ult iple m achines, several problem s are elim inat ed. Scalabilit y is no longer an issue. I n fact , using Applicat ion Cent er m akes scalabilit y as good as, and possibly bet t er t han, MSMQ. Since MSMQ receive funct ions are t ied t o a single m achine, only one m achine will read m essages from a m essage queue ( alt hough any server in t he group could act ually process t he m essage once it was read) . Mult iple m achines could each have a MSMQ receive funct ion reading from a queue, alt hough load balancing by having m ult iple readers of a queue is generally discouraged. We didn’t at t em pt t o load balancing receive funct ions on Applicat ion Cent er t o see how t hey behave. Applicat ion Cent er load balancing also provides som e reliabilit y benefit s, in t hat single server failures aren't a problem . I n t he case of general net work errors, however, MSMQ m ay have an advant age over Applicat ion Cent er. Ult im at ely, reliabilit y m ay be a m oot point as well. as m ost real syst em s will have a m echanism in place t o det ect unfulfilled orders ( or in our case- - unshipped t icket s) . So if a call fails, it will be handled event ually. This will be discussed m ore in a follow ing sect ion. There are several fact ors t o consider in select ing m essage- passing solut ions for any given sit uat ion. Regardless of t he solut ion we select , t he m essage will end up in work queue for BTS and be prepared for delivery t o t he TPA.

Se n din g t he Or de r Aft er BTS has received a m essage, t he next st ep from t he point of Figure 12.1 is t o t ransform our m essage int o t he TPA's form at . For t he m om ent , we will skip ahead t o St ep 3 and look at how BTS sends t he m essage t o t he TPA for processing. Just as we did wit h t he receive funct ions m ent ioned above, we have several t ransport opt ions for sending t he order t o t he TPA. Regardless of which t ransport opt ion we use, t hough, t here are m any t hings com m on about t he way BTS processes m essages. BTS has int roduced a lot of new t erm inology t hat is im port ant for t he explanat ion so let 's quickly review som e im port ant definit ions. Obj ect s represent ed by t hese definit ions can all be creat ed by t he BizTalk Messaging Manager

BTS M e ssa gin g D e fin it ion s D ocum e n t - - This is a specificat ion for a m essage t hat will be processed by BizTalk. Or ga n iza t ion- - To BizTalk, an organizat ion represent s a specific t rading part ner. Typically, an organizat ion is specified as t he source and/ or dest inat ion for each m essage. H om e Or ga niza t ion - - A default organizat ion t hat represent s our BizTalk server. However, hom e organizat ion m ay not be specified as t he source or dest inat ion for m essages. I nst ead subdivisions called applicat ions are used. Applica t ion - - An applicat ion is a virt ual ent it y t hat m ay or m ay not correspond t o a real soft ware applicat ion. I t is essent ially a point of at t achm ent t hat allows us t o connect t o t he hom e organizat ion. There are a couple of im port ant not es on applicat ions and organizat ions. Applicat ions are assum ed t o be int ernal t o our net work and, as such, are allowed t o dir ect ly int erface wit h BTS Orchest rat ion XLANG schedules. Ot herwise, t he select ion of specific applicat ions or organizat ion is m ost ly im por t ant for organizat ion and t racking of m essages. And as not ed previously, specifying t his inform at ion can also be used by BTS for rout ing m essages. Por t - - This is t he dest inat ion for our m essage. This could be an act ual organizat ion in w hich case you're required t o provide t he specific t ransport . Transport s available include HTTP, SMTP, FTP, COM+ com ponent s, and XLANG schedules am ong ot hers. Or t his could be a virt ual organizat ion for which t he specifics are det erm ined at runt im e, in which case t he port is considered t o have an open dest inat ion.

Cha n n e l- - As previously defined, t his is t he part icular pat h t hat a m essage t akes t hrough BTS. A source organizat ion or applicat ion m ay be designat ed for a channel. Ot herwise it is considered t o have an open source. The channel specifies t he inbound and out bound docum ent definit ions. The channel also handles any dat a t ransform at ions t hat need t o occur; t hese t ransform at ions use m ap files, which we will cover in t he next sect ion. Enve lope - - A wrapper for inbound and out bound docum ent s. Aids in form at t ing t he m essage t o and from XML. Som e form at s include X12, EDI FACT and Reliable. Reliable w ill be used lat er in t he t he sect ion on BTS Fram ework Reliable Delivery.

Cr e a t in g t h e Or ga n iza t ion s a n d Applica t ion s We need t o creat e an organizat ion t o represent t he TPA so we have som e place t o hook in our port . To do t his, sim ply open t he BizTalk Messaging Manager and select " creat e a new organizat ion" from t he file m enu. This will display t he dialog shown in Figure 12.4. All we need t o do is give a nam e, so w e will j ust call it TPA. I f we had needed t o program m at ically ident ify t he TPA by som e ot her m et hod t han organizat ion nam e, we could have specified addit ional inform at ion on t he “ I dent ifiers” t ab. You can define your own ident ifiers or use st andard one such as I SO 6523 Organizat ion I dent ificat ion, Am erican Bankers Associat ion num ber or DUNS ( Dun & Bradst reet ) num ber.

Figu r e 1 2 .4 Dialog for adding t he TPA organizat ion.

As st at ed before, t he hom e organizat ion is a bit different from ot her organizat ions because it represent s our BizTalk Server. Though we could j ust at t ach channels direct ly t o ot her organizat ions t hat represent us, t his is not recom m ended. I nst ead, t o keep t hings organized, we want t o creat e an applicat ion off of our hom e organizat ion t o represent our m iddle t ier. Creat ing an applicat ion is easily done by edit ing t he hom e organizat ion as shown in Figure 12.5. Not ice t hat t he hom e organizat ion has a t hird t ab t hat ot her organizat ions don't . This is where we can add our applicat ion t hat we will call WebOrder.

Figu r e 1 2 .5 Applicat ions t ab for t he Hom e Organizat ion propert y sheet .

Cr e a t in g t h e Por t Port s are t he endpoint of m essage so we need t o creat e a port for t he TPA, which we will nam e Print OrderToTPA. To do t his, open t he BizTalk Messaging Manager and select New Messaging Port t o an Organizat ion from t he File m enu. Nam e t he port Print OrderToTPA, and configure t he dest inat ion organizat ion as shown in t he Figure 12.6. Not e t hat we've select ed t he TPA organizat ion t hat we j ust creat ed and specified a prim ary t ransport as an HTTP URL where our TPA server is locat ed. There are a num ber of port opt ions only som e of which are show n in Figure 12.6. im port ant ones: z

z

z

z

List ed here are som e of t he

Tr a n spor t - - W e se le ct e d H TTP for t his opt ion . Ot h e r t ra n spor t opt ion s a va ila ble a r e : Applica t ion I n t e gr a t ion Com pon e n t ( AI C) , w h ich is a COM + obj e ct w it h a t ha t im ple m e n t s a spe cific BizTa lk in t e r fa ce ; file ; H TTP; H TTPS; loopba ck , w h ich r e t u r n s t o t h e ca llin g XLAN G sch e du le or com pon e n t ; M SM Q; a n d SM TP. For por t s t ha t a r e a t t a che d t o a n a pplica t ion , XLAN G sch e du le s a r e a lso a n opt ion a s discu sse d in t h e se ct ion on Act iva t in g Or ch e st r a t ion fr om BTS M e ssa ging. W e ca n a lso se le ct a ba ck u p t r a nspor t t ha t w ill be u se d if t h e pr im a r y t r a n spor t fa ils. Se r vice W indow - - You ca n r e st r ict t h e a v a ila bilit y of t h e por t t o ce r t a in h ou r s. M e ssa ge s r e ce ive d out side of t h ose h our s w ill be qu e u e d u n t il t he se r v ice w in dow is ope n . Th is opt ion w a s n ot u se d in ou r e x a m ple . En ve lope s- - W e ca n a ssocia t e a n um be r of diffe r e nt e n v e lope s ( ED I FACT, X1 2 , fla t file , e t c.) . Th e se ca n be u se d t o for m a t ou r m e ssa ge s so t h e y ca n be r e a d by e x ist in g n on - XM L sy st e m s. Th is opt ion w a s n ot u se d in ou r e x a m ple . En codin g, e n cr ypt ion , a n d sign a t u r e - - The se opt ion s a llow you t o M I M E e n code t he docu m e n t if it con t a ins n on - t e x t da t a a n d t o u se Se cu r e M I M E if you ne e d pu blic- k e y e n cr ypt ion . Th is opt ion w a s not u se d in ou r e x a m ple .

Figu r e 1 2 .6 Dialog for configuring t he dest inat ion organizat ion.

Once you click Finish, t he new m essaging port will show up in t he list of configured port s. Not e t hat t he channel count for t he port is zero, so now we need t o creat e a channel t o allow t he port t o receive our m essage. Tip: A port ca n h a ve a ny n u m be r of ch a n n e ls con n e ct in g t o it . Con ve r se ly, w e ca n se n d a m e ssa ge t o m u lt iple por t s u sin g a sin gle ch a nn e l by gr oupin g t h ose por t s int o dist r ibut ion list s.

Cr e a t in g t h e Ch a n n e l A channel runs from an applicat ion or organizat ion t o a port . Since our m essage is com ing from an int ernal source, we will creat e a channel from an applicat ion. Recall from our previous discussion t hat t his is done t o help us keep our connect ions organized. To do t his, sim ply right - click t he port ( Print OrderToTPA in t his case) and select " New Channel From an Applicat ion" from t he cont ext m enu. This brings up t he new channel/ channel propert ies wizar d, which is where we'll nam e t he channel gasTI XOrderToOrchest rat ion. Now we need t o t ell t he channel where it will be get t ing it s dat a from ; we do t his in t he second page of t he channel wizard. Since we only have one applicat ion- - webOrder- - assigned t o our hom e organizat ion, we will select webOrder as our source as shown in Figure 12.7. Even t hough we select ed a specific applicat ion as our source, dat a can com e from anyone who correct ly subm it s a docum ent t o t his channel.

Figu r e 1 2 .7 Dialog for configuring t he source applicat ion.

Figu r e 1 2 .8 Dialog for configuring t he inbound docum ent .

Aft er t elling t he channel where t o get it s dat a from , we now need t o select t he inbound and out bound docum ent specificat ions. For t he inbound docum ent , sim ply select t he gasTI XOrder specificat ion on t he t hird page of t he channel wizard as shown in Figure 12.8. We’ll assum e t hat t he TPA has it s own specificat ion for print orders. For t he out bound specificat ion, we will select t he TPAPrint Order specificat ion. Since we are using different inbound and out bound specificat ions we now m ust also select a m ap file. We will discuss t he det ails of m apping lat er; for now, sim ply browse t o t he m ap file gasTI XToTpaR1.xm l. Figure 12.9 shows t he com plet ed dialog. Since we have no part icular logging requirem ent s in t his case, accept t he default s on t he rem aining dialogs. N ot e : For t he pu r pose s of t h is e x a m ple , w e a r e a ssu m in g t ha t w e a lr e a dy ha ve t he TPA docu m e n t spe cifica t ion in h a n d. I n pr a ct ice , w e w ou ld lik e ly obt a in t h is spe cifica t ion t h r ough t h e BizTa lk .or g r e posit or y or t h r ou gh UD D I .

Figu r e 1 2 .9 Dialog for configuring t he out bound docum ent .

Channels provide several opt ions t hat we did not ut ilize such as t he abilit y t o log part icular fields or ent ire docum ent s. Channels also provide t he abilit y t o sign and recognize digit al signat ures and filt er what inbound docum ent s are processed based on t he cont ent s of part icular fields. ( Channels also provide t he specificat ion of ret ry opt ions, but t his will be covered in m ore det ail in t he sect ion, Receipt s.) The pat h from our schedule t o t he TPA is now com plet e. Figur e 12.10 sum m arizes t he rout e our m essage t akes.

ChannelToTPA channel

Transormation

Submitted from an application GasTixOrder specification

gastixToTpaR1.xml

PrintOrderToTPA Messaging Port

HTTP to TPA

TPAPrintOrder specification

Figu r e 1 2 .1 0 This diagram dem onst rat es t he m essage flow t o TPA.

For m a t t in g t he Or de r As we discussed before, t he TPA has it s own docum ent specificat ion for form at t ing a t icket order, so we need t o t ransform our order docum ent t o m at ch t heir specificat ion. To do t his we will use BizTalk Mapper. This creat es a m ap file will t hat t hen be at t ached t o our channel as previously dem onst rat ed and will aut om at ically be applied whenever we send a docum ent t hrough t hat channel. BizTalk Mapper is a t ool t hat let s us visually creat e a t ransform at ion bet ween t he dat a elem ent s of t wo docum ent specificat ions. This allows us t o see t he propert ies of individual dat a, and also shows t he relat ionship bet ween t he t w o specificat ions. Figure 12.11 illust rat es our m ap from t he gasTI X order form at t o t he TPA form at .

Figu r e 1 2 .1 1 BizTalk Mapper creat es a visual m ap for t ransform ing a gasTI X order t o a TPA order.

Table 12.1 list s t he m appings bet ween our order docum ent and t he TPA docum ent . Most of t hese are sim ple one- t o- one m appings, so all w e need t o do is drag t he field from our docum ent t o t he corresponding field in t he TPA docum ent and t he connect ion is done. Som e of t hese m appings, however, are far m ore com plex and t hat 's where funct oids com e int o play. Ta ble 1 2 .1 ga sTI X Fie lds M a p D ir e ct ly t o TPA Fie lds

D e st ina t ion / Pr int Or de r / Cu st om e r / / Pr int Or de r / Cu st om e r / / Pr int Or de r / Cu st om e r / ou n t / Pr int Or de r / Cu st om e r / / Pr int Or de r / Cu st om e r / Se ct ion / Pr int Or de r / Cu st om e r / Row / Pr int Or de r / Cu st om e r / Se a t / Pr int Or de r / Cu st om e r / ve nt Code / Pr int Or de r / Cu st om e r / Adm ission Pr ice / Pr int Or de r / Cu st om e r / ax / Pr int Or de r / Cu st om e r / ot a l

Sour ce Or de r N u m be r D a t e St a m p Or de r D e t a ils/ D ocu m e n t C

/ ga sTI X_ or de r / sa le / sa le _ id / ga sTI X_ or de r / sa le / da t e _ of_ pu r ch a se Cou n t of / ga sTI X_ or de r / se a t

Or de r D e t a ils/ Sh ipM e t h od Or de r D e t a ils/ D ocu m e n t /

/ ga sTI X_ or de r / sa le / shippin g_ m e t h od_ id / ga sTI X_ or de r / sa le / se ct ion _ na m e

Or de r D e t a ils/ D ocu m e n t /

/ ga sTI X_ or de r / sa le / r ow _ n a m e

Or de r D e t a ils/ D ocu m e n t /

/ ga sTI X_ or de r / se a t / se a t _ na m e

Or de r D e t a ils/ D ocu m e n t / E

/ ga sTI X_ or de r / sa le / e ve n t _ id

Or de r D e t a ils/ D ocu m e n t /

/ ga sTI X_ or de r / sa le / pr e _ t a x _ t ot a l divide d by cou n t of ga sTI X_ or de r / se a t / ga sTI X_ or de r / sa le / t a x _ ch a r ge d divide d by cou n t of ga sTI X_ or de r / se a t / ga sTI X_ or de r / sa le / pr e _ t a x _ t ot a l divide d by cou n t of ga sTI X_ or de r / se a t

Or de r D e t a ils/ D ocu m e n t / T Or de r D e t a ils/ D ocu m e n t / T

PLUS

/ Pr int Or de r / Cu st om e r / Or de r D e t a ils/ D ocu m e n t / E ve nt D a t e / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ D ocu m e n t / E ve nt Spla sh / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / Add r e ss/ N a m e / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / Add r e ss/ Addr e ss1 / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / Add r e ss/ Addr e ss2 / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / Add r e ss/ Cit y int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / Addr e ss / St a t e / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / Add r e ss/ Zip / Pr int Or de r / Cu st om e r / Or de r D e t a ils/ I n voice / I n v oice Te x t / Lin e / Te x t / Pr int Or de r / Cor r e ct ion/ D a t e Tim e St a m p

/ ga sTI X_ or de r / sa le / t a x _ ch a r ge d divide d by cou n t of ga sTI X_ or de r / se a t St r in g con ca t e na t ion of e v e nt _ da t e a n d e ve nt _ t im e fie lds fr om t h e da t a ba se , r e t r ie ve d by / ga sTI X_ or de r / sa le / e ve n t _ id Va lu e of e ve n t _ na m e fie ld fr om t h e da t a ba se , r e t r ie ve d by / ga sTI X_ or de r / sa le / e ve n t _ id St r in g con ca t e na t ion of / ga sTI X_ or de r / sa le / bill_ t o_ fir st _ n a m e a n d / ga sTI X_ or de r / sa le / bill_ t o_ la st _ na m e / ga sTI X_ or de r / sa le / ship_ t o_ a ddr e ss / ga sTI X_ or de r / sa le / ship_ t o_ a ddr e ss2 / ga sTI X_ or de r / sa le / ship_ t o_ cit y / ga sTI X_ or de r / sa le / ship_ t o_ r e gion _ id / ga sTI X_ or de r / sa le / ship_ t o_ post a l_ code Er r or t e x t r e t ur n e d fr om da t a ba se qu e r y / ga sTI X_ or de r / sa le / da t e _ of_ pu r ch a se

Fu n ct oids Funct oids are essent ially funct ion obj ect s t hat perform m any of t he funct ions we would t ypically associat e wit h a program m ing language. There are funct oids t hat concat enat e st rings, m ult iply and divide num bers, and apply specific form at t ing. We can also use funct oids t o ext ract dat a from a dat abase. I f none of t he st andard funct oids fit our requirem ent s, we can even creat e cust om funct oids writ t en in VBScript . I f we need st ill m ore flexibilit y, we can go so far as t o writ e a com plet ely cust om funct oid obj ect , using t he I Funct oid int erface, t hat is t hen added t o our funct oid palet t e. Given t he effort involved in t hat t ask, however, we generally t ry t o m ake use of t he exist ing funct oids when we can. N ot e : Ke e p in m in d t h a t t h e e nd r e su lt of com pilin g t h e m a p file is a n XSLT st yle sh e e t . W h ile m a n y of t h e fu n ct oids com pile t o st a n da r d XSLT e le m e nt s, som e of t h e m ( in clu din g cu st om scr ipt fu n ct oids) a dd scr ipt e le m e n t s t o t he st y le sh e e t . Sin ce scr ipt e le m e n t s a r e n ot or iou s for slow in g dow n st yle sh e e t pr oce ssin g, w e n e e d t o be ca r e fu l t o e n su r e t ha t our fu n ct oid u sa ge doe s n ot cr e a t e pe r for m a n ce issu e s. For t he sa m e r e a son s, w e n e e d t o t h in k ca r e fully be for e w r it in g la r ge block s of scr ipt or in v ok in g COM + com pon e n t s fr om scr ipt . Ju st be ca u se w e ca n do e ve r yt h in g in t h e m a p doe s n ot n e ce ssa r ily m e a n w e sh ou ld. Tip: I t is possible t o m a n u a lly e dit t h e com pile d- m a p, XLST st y le sh e e t t o a dd t h in gs t h a t t h e I DE doe s n ot a llow . H ow e ve r t h is ca n be t e dious a s t h e m a ppe r w ill r e pla ce t h e se e le m e n t s e ve r y t im e you r e com pile .

AM FL Y

Now we w ill t ake a closer look at t he specific funct oids used in our m ap. Funct iods are visually represent ed in t he m apper by boxes in t he cent er of t he m apper form as show n in Figure 12.11. The first funct oid we use is t he St ring Concat enat ion funct oid, which t akes st rings as input s and concat enat es t hem t oget her for t he out put . Any num ber of input s can be used wit h t he funct oid- - t o add an input , sim ply drag t he field t o t he funct oid. I n t his case w e are using t his funct oid t o concat enat e t he cust om er's first and last nam es.

TE

The next funct oid is t he Record Count funct oid, which sim ply count s t he num ber of occurrences of one it em ( and only one) in t he docum ent . For exam ple, if we connect t he event _id field t o a Record Count funct oid, t he out put will be t he num ber of seat s cont ained in t he docum ent . We use t his funct oid t o count t he num ber of seat s ( and t herefore t he num ber of t icket s) ordered. I n addit ion t o being st ored in t he out put docum ent direct ly, t his count will be used lat er t o calculat e t ax and price per t icket . That brings us t o t he various m at hem at ical funct oids, which provide everyt hing from m inim um and m axim um value calculat ions t o addit ion, subt ract ion, and square root s. For t he gasTI Xt oTPAr1 m apping, we are sim ply using t he division and addit ion funct oids t o convert t he t ot al t icket price and t ot al t ax list ed in t he t icket order t o t he per- t icket values t hat t he TPA want s t o see. These calculat ions work off of t he t icket count t hat we discussed earlier. All of t hese funct oids so far have operat ed solely on dat a t hat is already present in t he source docum ent . Now let 's address t he need t o pull som e addit ional dat a from t he dat abase t hat isn't present . There are t hree dat abase funct oids t hat are built for t his t ask. I n a t ypical dat abase m apping all t hree of t hem will be used. The first st ep is always t o m ake a connect ion t o t he dat abase using t he Dat abase Lookup funct oid, which t akes t he follow ing four param et ers: z

t h e da t a ba se t o u se ( " dsn = ga sTI X;u id= sa ;pw d" = " )

z

t h e t a ble w e a r e qu e r yin g ( " e v e n t " " )

z

t h e colu m n w e a re qu e r yin g a ga in st " e ve n t _ id"

z

t h e va lue w e a r e u sin g t o qu e r y t he da t a ba se ( m a ppe d fr om t h e ga sTI X e v e n t _ id. )

Not e t hat we have used const ant s for all of our values except event _id. Figur e 12.12 shows t he param et ers we used for our m apping.

Team-Fly®

Figu r e 1 2 .1 2 Dialog for t he Dat abase Lookup funct oid.

Once we have est ablished a dat abase connect ion, we need t o ext ract t he dat a. We do t his using t he Value Ext ract or funct oid, which allows us t o specify one dat abase field t o ret rieve. We will use t hree of t hese t o ret rieve t he event _nam e, event _t im e, and event _dat e field, which t hen m ap t o t he Event Dat e and Event Splash fields in t he out put docum ent ( not e t hat anot her St ring Concat enat ion funct oid is used t o com bine t he t im e and dat e) . I f we have t rouble set t ing up our dat abase queries, we can use t he Error Ret urn funct oid. I t hands out a st ring t hat cont ains any error m essages creat ed during dat a access. N ot e : W e n e e d t o e m ph a size t ha t a t r a de - off e x ist s be t w e e n e x t r a ct in g da t a fr om a da t a ba se a n d r e qu ir in g t ha t it in st e a d be su pplie d in t h e in bou n d docu m e n t . I n m ost ca se s w e w ou ld pr e fe r t o h a ve t h e da t a a lr e a dy pr e se n t in t h e docu m e n t . By doing so, w e do n ot ha ve con ce r n s a bou t t h e da t a ba se se r ve r be in g in a cce ssible , pe r for m a n ce pr oble m s fr om a n ove r loa de d da t a ba se se r ve r , or ot h e r pr oble m s t h a t ca n occu r a n y t im e a n a ddit iona l sy st e m is a dde d t o t h e m ix . On t h e ot h e r h a n d, r e t r ie vin g da t a fr om t h e da t a ba se r e du ce s t h e bu r de n on t he su pplie r of t he docu m e n t , a n d in som e ca se s, t h e t r a din g pa r t n e r or le ga cy a pplica t ion w e a r e in t e gr a t in g w it h sim ply m igh t n ot h a ve a cce ss t o t he da t a w e ne e d.

Alt e r n a t ive s Most people not e when first consider ing BizTalk m essaging and m apping t hat t here is not hing special about BizTalk t hat couldn't be developed wit h m ore t radit ional m et hods. And generally speaking t hat is t rue. We could have coded t he m iddle t ier t o direct ly generat e int o t he TPA's form at . But what happens when t he TPA changes t heir form at ? Or we change t o a different TPA? Or if we decide t o use different TPA's for each shipping m et hod? That is when using BizTalk really st art s t o pay dividends. Perhaps we will not always see t he benefit s for t he first t rading or second t rading part ner but by t he t hird or t hirt iet h t rading part ner, we will.

Pr oce ssing t h e Or de r Now t hat t he order has been form at t ed and sent t o t he TPA for processing, we need t o t ake a look at t he im plem ent at ion of t he TPA it self. The TPA will receive our order t hrough an ASP page and a BizTalk channel. For our sam ple, t he TPA will t hen issue t icket ( s) t o t he cust om er via st andard em ail. A success or failure st at us

will also be sent back t o t he t rading part ner t hat sent t he order, which is gasTI X in t his case. We have used a BTS orchest rat ion schedule t o help process t he orders. We are assum ing t hat t he TPA m ost ly uses legacy syst em s but is st art ing t o m odernize and is using BTS orchest rat ion for sim ple applicat ion int egrat ion. N ot e : The t e r m sch e du le h a s be e n u se d r a t he r loose ly in t h is ch a pt e r . Bu t in a ll ca se s it r e fe r s t o t h e ou t pu t of t he BizTa lk Applica t ion de sign e r t ool. Th is is t h e t ool t ha t is u se d t o de sign w or k flow or ch e st a r t ion a n d a pplica t ion in t e gr a t ion w it h in BizTa lk . To be pr e cise XLAN G ( pr on ou n ce d ‘sla n g’) sch e du le s a r e t he com pile d ou t pu t of t he t ool t h a t ca n be e x e cut e d by t h e XLAN G ( or or ch e st r a t ion ) e n gin e / se r v ice . Bu t t h e t e r m s XLAN G sch e du le , or ch e st r a t ion sch e du le , a n d sche du le h a ve be e n u se d in t e r cha n ga bly.

Re ce ivin g t h e Or de r We know t hat t he TPA received t he order as an HTTPHTTP post t o an ASP page. We are using a slight ly m odified version of an ASP page, provided wit h t he BTS SDK, t o process t he m essage. This page ext ract s t he m essage from t he post ed dat a. We could have chosen t o act ivat e t he XLANG schedule direct ly and hand it t he m essage. But inst ead we send t he m essage t hrough BTS m essaging, which allows us t o dem onst rat e addit ional feat ures. The ASP code uses t he sam e subm it m et hod t o invoke m essaging, j ust like t he m iddle t ier code on gasTI X. The nam e of t he channel we will use is I ncom ingPrint Order.

Act iva t in g Or ch e st r a t ion fr om BTS M e ssa gin g Most of t he set up for BTS m essaging is sim ilar t o what was done for t he gasTI X side, in t erm s of creat ing organizat ions, channels and port s. Only t wo t hings differ. First , t he docum ent is not m apped, m eaning t hat t he channel's inbound docum ent is ident ical t o t he out bound docum ent . Second, we let BTS m essaging inst ant iat e t he orchest rat ion schedule and hand it t he m essage. The m essaging port we have creat ed is a port t o an applicat ion and is called Port ToPrint houseSchedule. The second st ep of t he wizard ( shown in Figure 12.13) shows t hat we are act ivat ing a new XLANG schedule ( as opposed t o using som e ot her t ransport ) . This gives t he physical locat ion of t he com piled XLANG schedule and specifies t he port nam e, I ncom ingPrint Order. The rest of t he param et ers are t he t ypical ones for m essaging port specificat ion. Aft er t he channel execut es, cont rol is handed over t o t he BTS orchest rat ion engine. N ot e : The por t , I n com in gPr in t Or de r , r e fe r r e d in t h e dia log in Figu r e 1 2 .1 4 is a n or ch e st r a t ion por t , n ot a BTS m e ssa gin g por t . An or ch e st r a t ion por t is e sse n t ia lly a logica l e n t r y poin t t o t h e XLAN G sch e du le a s w e w ill se e sh or t ly . Unfor t u n a t e ly , M icr osoft ch ose t o ove r loa d t h e t e r m por t w he n de v e lopin g BTS.

TPA Or ch e st r a t ion Ove r vie w The TPA is a separat e organizat ion wit h it s own servers and business processes. We will creat e an XLANG schedule t o coordinat e it s part of t he fulfillm ent process. Using t he BizTalk Orchest rat ion Designer, we have creat ed a schedule nam ed " Tpa1.skv" . Figure 12.14 shows t he com plet e orchest rat ion schedule t hat we use in t his sect ion. The left port ion of t he diagram is t he abst ract business process. The right hand side represent s physical im plem ent at ion of m essage sources or dest inat ions. Current ly four t ypes of im plem ent at ions are allow ed t o int eract wit h BTS orchest rat ion: BTS m essaging, MSMQ, COM+ com ponent s and script ing com ponent s. The m iddle represent s port s and m essages. Port s are logical ent ry point s t o t he schedule. Messages are shown inside of port s and are t he dat a elem ent s which m ove in and out of t he schedule.

Figu r e 1 2 .1 3 The Messaging Port Propert ies dialog allows us t o specify a new XLANG schedule.

Figu r e 1 2 .1 4 Com plet e TPA schedule.

Cr e a t in g t h e Bu sin e ss Pr oce ss The business process, show n in Figure 12.15, t heoret ically is creat ed by a business analyst t o sum m arize t he work flow t hrough t he syst em . For our TPA, t he business process blocks represent m oving m essages bet ween different syst em s. We need t o at t ach t hese t o specific im plem ent at ions on t he right side of t he diagram t o designat e where t he dat a goes. We also need t o im plem ent a bit of logic by adding a decision block. This decision block ( represent ed by a rect angle) has one rule called " Check Address" . The script for t his logic can be ent ered or edit ed by using t he propert ies of t he decision block ( see Figure 12.16 for t he com plet e dialog) and is as follow s: ValidateAddress_Out.ValidateAddress=TRUE

This rule is very sim ple; it checks t he boolean ret urn value of a funct ion t o see if t he address validat ion succeeds. Rules can be, however, very com plex. We will cover t he address validat ion port lat er in t his chapt er.

Figu r e 1 2 .1 5 TPA Business process.

Figu r e 1 2 .1 6 Dialog for t he address validat ion rule.

Re ce ivin g M e ssa ge s in t h e Sch e du le Everyt hing else we do will be based on t he docum ent com ing int o t he schedule. First , we creat e a new BizTalk im plem ent at ion shape. The wizard/ propert ies t hen prom pt you t o creat e a new port or at t ach t o an exist ing one. We creat e a new port , nam ed I ncom ingPrint Order, which m at ches t he nam e we specified in t he BTS m essaging port . On t he subsequent t abs, we set it t o Receive ( Figure 12.17) , and specify t hat t he channel act ivat es a new XLANG schedule ( Figure 12.18) . Then we connect "Receive I nbound Order" block of t he business process t o t he I ncom ingPrint Order port . This brings up t he XML com m unicat ion wizard which let s us specify t he m essage. The wizard is st raight forward and allows us t o specify m essage nam e, t ype and associat e it wit h a specificat ion. We associat e it wit h t he sam e specificat ion t hat we used in BTS m essaging channel, TPAPrint Order.xm l. N ot e : I t 's im por t a nt t o be ca r e fu l w h e n spe cifyin g t he m e ssa ge t ype . I t m u st m a t ch t h e na m e of t h e r oot n ode na m e w h e n t h e m e ssa ge is com in g fr om BTS or t he la be l w he n t h e m e ssa ge is fr om M SM Q. I f n ot a m ildly in for m a t ive e r r or w ill be r a ise d.

Figu r e 1 2 .1 7 BizTalk Messaging Binding Wizard- - Com m unicat ion Direct ion.

Figu r e 1 2 .1 8 BizTalk Messaging Binding Wizard- - XLANG Schedule Act ivat ion I nform at ion.

Cr e a t in g t h e Com pon e n t s Now t hat we have shown how t o get a m essage int o our orchest rat ion schedule, we’ll show how t o int egrat e som e exist ing com ponent s t o process t hat m essage. We have t w o exist ing COM+ applicat ions t o int egrat e wit h: t he address validat or and t he TPA sender. The address validat or com ponent ( TPAAddressValidat or2) provides a wrapper for services t o verify a cust om er's address dat a. The im plem ent at ion for t he sam ple sit e doesn’t act ually do anyt hing except ret urn t rue, but it would be easy t o expand it t o perform validat ion. I t could even call a web service t o do t he j ob, supplied by t he post al service or anot her com pany. For our purposes, t he im plem ent at ion of t he address validat or is not im port ant ; all t hat concerns us is t he result ing St at us, as shown in List ing 12.2. Not e t hat t he int erface m ust ret urn a boolean, since t he rule in t he Decision block expect s t his ret urn value ( t he Validat eAddress value specified in our Decision block above) . List in g 1 2 .2 Visu a l Ba sic 6 .0 de cla r a t ion for t he Va lida t e Addr e ss Fu n ct ion Public Function ValidateAddress( ByVal Document as String, Status as Long) as Boolean

The next com ponent is t he TPASender, which has several funct ions, t he m ost im port ant of which ( at least t o t he cust om er) is sending t he t icket . The SendTicket m et hod ( see List ing 12.3) is used t o int erface wit h an exist ing syst em for print ing and m ailing paper t icket s as well as deliver ing elect ronic t icket s. For dem onst rat ion purposes, however, our im plem ent at ion j ust form at s a sim ple em ail m essage based on inform at ion in t he order docum ent and sends it t o t he cust om er via SMTP. Next , t he SendSuccess and SendFail m et hods ( list ing 12.3) ret urn t he appropriat e st at us t o t he calling client in t he form of a st andard XML docum ent . We could have accom plished t his wit h BizTalk m essaging as we did t o send t he order t o t he TPA, but we elect ed t o t ake anot her approach and generat e t he new XML docum ent ent irely in code. This is a som ewhat arbit rary choice, but it serves t o illust rat e an alt ernat e t echnique t hat m ay be very useful in m any circum st ances. For exam ple, a developer who is not yet versed in t he int ricacies of BizTalk Mapper and who is under a short deadline m ight prefer t his t echnique because of it s fam iliar it y. One of t he best reasons t o use t his t echnique, however , is t o m ake use of an exist ing code base. Bot h m et hods are ident ical in our im plem ent at ion except for t he st at us t hat is ret urned. The funct ions underlying t hese t wo m et hods const ruct a new XML DOM docum ent aft er ext ract ing som e key inform at ion from t he inbound order ( such as t he URL t o send t he st atus t o) . The st at us docum ent is t hen post ed t o t he dest inat ion URL using t he server- side XMLHTTP obj ect . Not e t hat t he st at us could also be sent t hrough a BizTalk channel using t he I I nt erchange int erface, but a direct HTML Post is adequat e for our needs since we do not need receipt s and are not im plem ent ing any t ransact ions for t he t ransm ission of t he st at us. N ot e : The in t e r fa ce s sh ow n in t h e list in gs a r e cle a r ly in t e r fa ce s t o COM com pon e n t s a n d n ot t o .N ET a sse m blie s. Th is is sim ply be ca u se BizTa lk Se r v e r h a s n ot ye t be e n u pda t e d t o fu lly u n de r st a n d .N ET a sse m blie s. As a r e su lt , COM / .N ET in t e r ope r a bilit y ( se e ch a pt e r 9 , " COM + I n t e gr a t ion " ) be com e s a fa ct or for t h e sh or t t e r m . List in g 1 2 .3 Visu a l Ba sic 6 .0 de cla r a t ion for t he Se n dSu cce ss, Se n dFa il, a n d Se n dTick e t Fu n ct ion s Public Sub SendSuccess( Document as String) Public Sub SendFail( Document as String) Public Sub SendTicket( Document as String)

Cr e a t in g t h e Com pon e n t Por t s Now t hat t he com ponent s have been creat ed, we need t o creat e t he port s t o at t ach t hem t o our schedule. Since we have t wo com ponent s we w ill need t wo com ponent port s t o validat e t he cust om er address, send t he t icket s, and send appropriat e st at us m essages back t o t he t rading part ner ( gasTI X in t his case) . Since t he process for creat ing each port is ident ical, we'll briefly discuss t he Com ponent Binding Wizard in general. The following st eps out line t he process: 1.

N a m e t h e por t .

2.

Se le ct t h e t y pe of com pon e n t in st a nt ia t ion . W e 'll se le ct St a t ic, w h ich m e a n s t ha t t h e XLAN G sch e du le is r e spon sible for cr e a t ing com pon e n t in st a n ce s.

3.

N e x t se le ct t h e com pon e n t t o u se . W e 'll be u sin g r e gist e r e d com pon e n t s, so sim ply se le ct t h e a ppr opr ia t e cla ss for t h e list .

4.

Se le ct t h e in t e r fa ce t ha t you w a nt t o u se . ( N ot e : Th is is on ly n e ce ssa r y if you h a ve m u lt iple in t e r fa ce s, w h ich is r e qu ir e d t o ope r a t e w it h in M TS.)

5 . Th e fin a l dia log con sist s of se v e r a l a dva n ce d opt ion s r e la t in g t o se cu r it y, st a t e m a n a ge m e nt , a n d t r a n sa ct ion su ppor t . W e w on 't be u sin g a n y of t he se for t h e TPA, so w e ca n j u st a cce pt t h e de fa u lt s.

Once we have creat ed bot h com ponent port s we need t o at t ach each m et hod t o t he appropriat e obj ect s in t he business process. We sim ply drag a connect ion from t he business process obj ect t o t he m et hod we want on t he com ponent port and select a synchronous call. I f t he com ponent has m ult iple m et hods we also select t he m et hod t o use and we are done. Next , we will handle t he m apping of t he dat a.

M a ppin g t h e D a t a Now associat e t he various pieces of dat a t hat are at t ached t o t he port s as shown in Figure 12.19. The m appings of t he TPA are relat ively sim ple; t he XML docum ent represent ing t he t icket order is sim ply passed from port t o port , so it is m apped t o a st ring param et er in each com ponent m et hod. The only ot her piece of dat a- - t he boolean t hat is ret urned from t he address validat or- - is used by t he decision block and discarded, so we don't need t o do anyt hing else wit h it . The dat a t ab of t he BizTalk Orchest rat ion designer will be covered in m ore det ail in t he next sect ion. Looking at t he TPA's logic has given us a quick overview of orchest rat ion and given us a t ast e of int egrat ing orchest rat ion and m essaging, but we did t he bulk of t he work in pre- exist ing COM+ com ponent s. This is one of t he sweet spot s for orchest rat ion: workflow and int egrat ion of exist ing applicat ions. We'll see m ore advanced feat ures in upcom ing sect ions, but it is clear how easy it is t o m ove sim ple pieces of dat a bet ween different applicat ions or syst em s. Using BTS m essaging t o t ransform or augm ent dat a as it m oves bet ween syst em s can furt her enhance t his abilit y. Now, we will get back t o t he gasTI X side and st art t ying up som e loose ends.

Figu r e 1 2 .1 9 Dat a m apping diagram for t he TPA schedule.

D e live r y Re ce ipt s Even t hough an advant age of BTS is t hat it allows us t o be loosely coupled wit h our t rading part ner, we st ill need t o know t hat t hey successfully received our m essage and if t hey were able t o process it . There are several different levels of acknowledgem ent , som e of which com e wit h no expense when BTS is used. BTS also has built in ret ry funct ionalit y for m essages lost in t he m ail.

Tr a n spor t Ack n ow le dge m e n t a n d Re t r y The first level of acknowledgem ent is t hat t he m essage was successfully t ransport ed. Obviously, t he m echanics of t his vary by t he t ransport used. For exam ple, when using an Applicat ion I nt egrat ion Com ponent ( AI C) t ransport , an error raised by t he com ponent will be caught by BTS and indicat es failure t o BTS. Our exam ple uses an HTTP t ransport t o send t he m essage t o t he TPA. BTS expect s a " 200 OK" response from t he TPA's web server. Any ot her response will be int erpret ed by BTS as a failure and t he m essage will end up in t he ret ry queue. Based on t he port set t ings, fut ure redelivery at t em pt s will be m ade. I f all at t em pt s fail, t he m essage will be placed in t he suspended queue.

BTS Qu e u e s At t his point , we need t o discuss t he queues t hat BTS m essaging uses. There are four queues im plem ent ed by BTS in SQL server. Each one is a separat e t able in t he shared queue dat abase. The queues are:

z

z

z

z

Th e w or k qu e u e con t a in s docu m e n t s t ha t a r e cu r r e n t ly be in g pr oce sse d by BTS. Un le ss v e r y la r ge a nd/ or com plica t e d docu m e n t s a r e be in g pr oce sse d, t h e y w ill n e ve r be se e n in t h e qu e u e be ca u se of t h e spe e d of pr oce ssin g. Th e sche du le d qu e ue con t a in s it e m s t ha t ha ve be e n pr oce sse d a n d a r e a w a it in g t r a n sm ission du e t o a se r vice w in dow con st r a in t . Th e r e t r y qu e u e con t a in s it e m s t h a t h a ve fa ile d t r a n sm ission a n d a r e w a it in g for t he ir r e t r y w in dow t o ope n u p. Also, docu m e n t s a w a it in g r e lia ble de liv e r y r e spon se s a r e in t h is qu e u e . Th e r e t r y pa r a m e t e r s a r e se t in t h e ch a n n e l t h a t t h is m e ssa ge w a s su bm it t e d fr om . Bot h t h e r e t r y cou n t a n d in t e r va l ca n be se t . Th e su spe nde d qu e u e con t a in s it e m s t h a t ha ve fa ile d a t som e st a ge of pr oce ssin g. I t e m s t ha t fa ile d du r ing t he t r a n sm ission ph a se m a y be r e su bm it t e d.

All of t hese queues m ay be viewed and m anually m anaged from t he BizTalk Adm inist rat ion console. Tip: I t is a good ide a t o se t u p m on it or in g on t he suspe n de d qu e u e . This is fa ir ly st r a ight for w a r d sin ce a ll of BTS's que u e s a r e im ple m e n t e d in SQL Se r ve r . Th e I I nt e r ch a n ge obj e ct a lso pr ov ide s a m e t h od for m on it or in g BTS qu e u e s. Sin ce it is a n a ut om a t e d m on it or in g, it a llow s fa ilu r e s t o be de a lt w it h in a t im e ly m a n n e r - - r a t h e r t ha n w a it in g for t he cu st om e r t o in for m us t h a t t h e r e is a pr oble m .

Re ce ipt s

AM FL Y

Just because a m essage was successfully t ransport ed does not m ean t hat it was seen by BTS. As previously discussed, t his im plem ent at ion of BTS does not direct ly receive any m essage ( from a FTP or HTTP server) . BTS has t wo m et hods t o ensure t hat t he rem ot e server has seen our m essage. For X12, EDI FACT and ot her m essages wit h cust om parsers, you can use receipt s. A receipt in t his cont ext is t he act of aut om at ically request ing and delivering an acknowledgem ent m essage t hat can be associat ed ( t he BTS t erm is correlat ed) wit h t he original request . Bot h t he source and dest inat ion servers m ust be set up appropriat ely, but BTS does provide m ost of t he t ools needed t o im plem ent receipt s. An oversim plified descript ion of t he logical st eps is as follows: Spe cify t h e docu m e n t de fin it ion for t h e r e ce ipt

z

Se t u p t h e sou r ce ch a nn e l. Spe cify t h a t t h e cha n n e l e x pe ct s r e ce ipt s a n d se t a t im e ou t va lu e .

z

TE

z

Se t u p t h e de st ina t ion ch a n ne l. Spe cify t ha t it sh ou ld ge n e r a t e a r e ce ipt a n d spe cify a n ot he r ch a n ne l t ha t it w ill u se t o ge n e r a t e t h e r e ce ipt .

z

Th e r e ce ipt ch a n n e l on t h e de st ina t ion ge n e r a t e s a r e ce ipt a n d r e t ur ns it t o t he sou r ce .

z

Se t u p a r e ce ive m e ch a n ism t o de live r t h e r e ce ipt t o t h e BTS pr oce ssin g e n gin e on t h e sou r ce .

z

I f a r e ce ipt is n ot r e t u r ne d t o t he sou r ce w it h in t h e t im e lim it , t he pr e viou sly de scr ibe d r e t r y m e ch a n ism k ick s in .

BTS Fr a m e w or k Re lia ble D e live r y Oddly enough, t he receipt m echanism im plem ent ed in BTS does not work for XML m essages. For t his, BTS t urns t o t he BizTalk Fram ework 2.0 m essaging st andard. The BizTalk Fram ework does not concern it self wit h t he det ails of any part icular business like t he Roset t anet PI P's and ot her st andards do. Rat her t he BizTalk Organizat ion ( largely driven by Microsoft ) developed a fram ework t hat is concerned wit h reliable and st andardized delivery of any t ype of business m essage ( w it h t he assum pt ion t hat t he business m essage is XML) . This is largely accom plished by using SOAP based envelopes t o wrap t he m essages.

Team-Fly®

Tip: You ca n fin d ou t m or e a bout t h e BizTa lk Fr a m e w or k 2 .0 a n d t h e BizTa lk Or ga n iza t ion a t w w w .bizt a lk .or g.

The elem ent of t he fram ework t hat we are concerned wit h is reliable delivery; BTS uses reliable delivery for XML m essages in a m anner sim ilar t o receipt s for other form at s of m essages. However, set t ing up reliable delivery of XML m essages is m uch easier. This will work wit h any BizTalk Fram ework 2.0 com pat ible server, but if BTS is used, t here is m inim al set up on t he source and no set up required on t he dest inat ion. This w as one of t he reasons we chose for t he TPA ASP page t o pass it s m essage t hrough BTS m essaging. The first st ep on t he server is t o set up a global propert y on t he BizTalk group, via t he BTS adm inist rat ion t ool. On t he general t ab of t he group's propert ies, t here is an ent ry called " Reliable Messaging Reply- t o- URL." This is used t o specify t he address t o which reliable delivery acknowledgem ent s will be sent . Typically t his is an ASP page t hat will forward t he m essage t o t he BTS m essaging engine, but ot her t ransport s can be used as well. Second, we will creat e an envelope in BTS m essaging m anager t hat has a t ype of " Reliable" . Finally for each m essage t hat we want a reliable delivery receipt for, we sim ply select t he reliable envelope t hat we j ust creat ed for our out bound port . BTS will w rap your m essage in t he reliable delivery envelope and populat e t he receipt dest inat ion w it h t he address t hat was specified in t he group propert ies. That 's it . When t he dest inat ion BizTalk server get s t he envelope, it will know t o creat e a receipt and ret urn it t o t he reply address. This is t he t echnique t hat was init ially im plem ent ed for gasTI X. For our reply dest inat ion, we used a sim ple ASP page nam ed " St andardReceipt .ASP" t hat ships wit h BTS in t he SDK sam ples sect ion. This ASP page sim ply reads t he XML m essage sent and dum ps it t o t he BTS m essaging engine t o be processed. The envelope t ype allows t he BTS m essaging engine t o decide which channel t o rout e it t o. BTS has im plicit channels defined t o process reliable deliver y m essages. And finally, if t he receipt is not ret urned, t he ret ry cycle is init iat ed.

Lim it a t ion s Bot h receipt s and reliable delivery are an im provem ent over sim ply knowing t he m essage w as deliver ed. But under cert ain circum st ances t hese t echniques st ill fall short . Wit h reliable delivery, we know t he BTS engine received t he m essage but we don't know if processing was com plet ed. This st ill requires a definit ive acknowledgem ent from our t rading part ner's processing engine. I n m any business scenarios, confirm at ion inform at ion is expect ed as a ret urn value, like an invoice num ber. We can use t his " receipt " as part of an enhanced not ificat ion/ r et ry st rat egy t hat we will explore in t he next sect ion.

Or de r St a t u s Re t u r n a n d En ha n ce d Re ce ipt s We previously explained how BTS provided several m echanism s for guarant eeing t hat t he m essages it sends are act ually delivered. I n real business scenarios, it is not sufficient t o know t hat your t rading part ner's com put er or m essage- processing engine received t he m essage. That is a good st art , but we need t o confirm t hat t hey act ually underst ood and processed t he m essage. This will require som e coordinat ion ( m uch like t he developm ent of t he int erface t o send t he m essage) and som e developm ent on bot h sides, but it is an essent ial part of business. I n som e scenarios ( such as t he Microsoft 's purchase order exam ple for BizTalk) , several m essages will go back and fort h wit h a t rading part ner. I n our case, one sim ple receipt is enough. We will only get a not ificat ion aft er t he TPA print s and ships t he t icket s. The TPA will send a m essage if it encount ers an error processing an order. So t he inform at ion we need is very sim ple: order num ber, order st at us, shipping dat e, and possibly a t racking num ber if t he shipper provides one. We can use t his inform at ion t o updat e our dat abase. Tradit ional m et hods will handle t his scenario j ust fine. We could set up an ASP page t o process t he acknowledgem ent ; or we could invoke a COM+ or .NET com ponent t o do so. We could allow a web service t o

handle t he acknowledgem ent dir ect ly, but we are assum ing a st andard solut ion from t he TPA. We chose t o use BTS orchest rat ion t o correlat e and process t he acknowledgm ent s. This was probably overkill for our sim ple scenario, but it allowed us t o dem onst rat e som e feat ures t hat will be ext rem ely useful in m ore elaborat e business sit uat ions. So inst ead of our original processing diagram on t he gasTI X side, we have som et hing t hat looks like Figure 12.20. We are now including orchest rat ion on t he gasTI X side. BTS m essaging hands t he order t o our orchest rat ion schedule. Orchest rat ion uses BTS m essaging t o send t he order t o t he TPA. Finally on t he ret urn t rip, m essaging hands t he order back t o t he orchest rat ion schedule, which will updat e t he m iddle t ier. This m ay seem like ext ra w ork but we are using t he com plem ent ary abilit ies of t he m essaging and orchest rat ion engines t o achieve t he desired effect as we’ll discuss in t his sect ion. Middle Tier will update database with status and take other appropriate actions.

gasTix Order

.Net middle tier

1) Send order to Biztalk

8) Notify middle tier of updated order status

Orchestration gasTIX Order

TPA Order

2a)Orchestration starts Transaction and adds Reply address

If transaction times out, Order is resent to TPA.

8a) If Transaction fails, Middle tier is notified

gasTix Biztalk 2) Transform order to TPA format

TPA Order

TPA Order + replyAddress

3) Send order to TPA

If no acknowledgement recieved, Biztalk will resend message.

4) Acknowledge order

TPA

7a)Messaging Sends to correlated instance of schedule.

7) Return Order Status to reply address

5) Process order

6)If Success, Send Tickets to customer

Figu r e 1 2 .2 0 Updat ed diagram using orchest rat ion t o process enhanced receipt s.

The BTS m essaging engine does t he original t ransform at ion of t he gasTI X m essage int o t he TPA m essage. Then it hands t his m essage int o a BTS schedule t hat it kicks off. This is ident ical t o how t he TPA orchest rat ion schedule was init iat ed. Now t hings get m ore com plicat ed as we use t wo of t he less obvious feat ures of BTS orchest rat ion: t ransact ions and correlat ion. Transact ions inside of an orchest rat ion schedule are used t o relat e a set of act ions. Correlat ion m eans t hat responses t o m essages sent from a schedule w ill be ret urned t o t he exact sam e schedule. We will discuss schedule t ransact ions first and t hen see how we use t hem .

Tr a n sa ct ion s in Or ch e st r a t ion Sch e du le s

Given t he t ypical com put ing definit ion of t ransact ion, it is fairly obvious t hat t he purpose of grouping act ions in a schedule is for t he purpose of det erm ining success or failure of t hat group as a whole. There are t hree t ypes of t ransact ions in schedules: short lived ( DTC st yle) , t im ed, and long- running. Short - lived t ransact ions will be fam iliar t o COM+ and dat abase program m ers. They have t he four ACI D at t ribut es and upon failure, t he DTC will rollback all act ions t hat were init iat ed in t he schedule. W a r n ing: Act ivit ie s t ha t a r e in h e r e n t ly n on - t r a n sa ct iona l su ch a s u sin g BTS m e ssa gin g t o do a n H TTP post a r e n ot a ffe ct e d by t r a n sa ct ions t h a t r ollba ck .

Tim ed and long- running t ransact ions lack t he abilit y t o use t he DTC t o roll back t heir act ivit ies. Tim ed t ransact ions and short - lived t ransact ions have t he addit ional abilit y t o set a t im e lim it for success. Ot her t han t im e lim it failures, all t hree t ypes fail for t he sam e reasons: explicit failures ( because t he " Abort " shape is execut ed) or because of failure in t ransact ional resources or ot her obj ect s. Transact ions provide t he abilit y for ret ry count s and back off t im es, sim ilar t o t he BTS m essaging m anager and port s. All t hree t ypes also share t he abilit y t o t ake alt ernat ive act ion upon failure, which we will use in t he our applicat ion. Tip: Or che st r a t ion sch e du le s a lso ha ve t h e a bilit y t o n e st t r a n sa ct ion s. On ly on e le v e l of t r a nsa ct ion m a y be n e st e d a n d t h e out e r la ye r m a y n ot be sh or t live d. Also if a n e st e d t ra n sa ct ion fa ils, a n a lt e r na t e a ct ion m a y be pe r for m e d. Th is is ca lle d com pe n sa t ion a n d is m e ch a n ize d sim ila r t o t he a ct ion u pon fa ilur e .

We chose t o use a t im ed t ransact ion t o ensure t hat t he TPA has processed our order. We've assum ed t hat cont ract ually t he TPA will send out t he t icket s wit hin a cert ain period of t im e. Tim ed t ransact ions will w ork w ell for gasTI X because of t his lim it . I f t he TPA hasn't sent us a receipt wit hin t he accept able t im e fram e, we will resend t he order m essage. I f we do not receive a response wit hin t he expanded ret ry t im e, we will updat e t he dat abase ( via t he m iddle t ier com ponent which w e will cover in t he next sect ion) and set t he st at us of our print request t o failed. Here is a look at t he orchest rat ion schedule t hat we produced t o fulfill t his acknowledgem ent need. Figure 12.21 shows t he business process view of our orchest rat ion schedule. As explained earlier in t he chapt er, t his view gives a logical flow of t he m essage( s) t hrough t he syst em . The left side shows t he decisions and act ions t hat occur. These are com m only referred t o as act ions or shapes. The right side shows t he ent ry and exit point s from t he schedule t hat t he im plem ent at ion m et hods chose. I n t he m iddle are t he connect ions bet ween t he act ions and t he im plem ent at ions called port s ( not t o be confused wit h BTS m essaging port s) . I nside of each port are one or m ore m essages which represent t he act ual dat a t raveling in and out of t he syst em . The sam e m essages are also represent ed in Figure 12.22; it det ails all of t he m essages t hat t ravel in and out of t he schedule and how t heir dat a is relat ed. However t his view does not give any clues as t o when t he dat a is m oved bet ween m essages. To get a com plet e view of t he syst em , bot h views m ust be used. This dat a view will be m or e fully explained short ly. The page cont ains st andard inform at ion such as t he nam e of t he t ransact ion and t he t ype. The t im eout and ret ry values are also specified on t his form . What we are m ost int erest ed in is t he creat ion of failure act ions which is also done from t his page in t he " On Failure" fram e. I nit ially t he but t on is labeled " Add Code" . Aft er select ing t his and closing t he form by clicking "OK" , anot her sheet will be creat ed in t he BizTalk Orchest rat ion Designer nam ed " On Failure of Transact ion 1" . There is also an " Enabled" checkbox in t he fram e t hat allows t he failure act ivit ies t o be disabled. Select ing t he new failure sheet reveals a set of port s and im plem ent at ion shapes t hat is ident ical t o t hat on our original business process sheet . However no m essages or act ions are displayed. That is t he left side of t he business process diagram will be blank but t he right side will be ident ical. This list of port s and im plem ent at ions is t he sam e for every sheet . We can eit her add new dest inat ions for our m essages if t hey are required or use exist ing ones. The sam e is t rue for m essages. Our com plet ed " On failure of Transact ion 1" sheet is shown in Figure 12.24.

Figu r e 1 2 .2 1 gasTI X Orchest rat ion Business Process view.

Since we want t o not ify t he m iddle t ier of a failure, we will reference t he sam e port t o t he m iddle t ier obj ect t hat t he success pat h used. Only a single act ion t hat not ifies t he m iddle t ier of t he failure is required. When we connect t he act ion t o t he port , a new m essage is creat ed aut om at ically because wit h COM+ com ponent s( and script s) , exist ing m essages m ay not be reused. Each ent ry t o t he port is unique. We nam ed t his m essage " Save_St at us_in_2" . Now t hat we have int egrat ed wit h t he COM+ com ponent , only one t hing is m issing; where do we set t he param et ers t hat we will pass t o t he m iddle t ier ? The answer is in t he dat a view represent ed in Figure 12.22. As st at ed earlier, each m essage on t he dat a view corresponds t o a m essage displayed inside of a port on t he business process view . The m essages in our schedule are as follows: z

" Sa ve St a t us" is t h e m e ssa ge t o t he COM + m iddle t ie r for t h e su cce ss pa t h .

z

" Sa ve St a t us2 " is t h e m e ssa ge t o t h e COM + m iddle t ie r for t h e fa ilu r e pa t h .

z

" Or de r I n " is t h e m e ssa ge or igin a lly pa sse d t o t h e sch e du le by BTS m e ssa gin g.

z

" Or de r Out " h a s t he sa m e st r u ct u r e a s " Or de r I n" bu t is u pda t e d by t he sch e du le ( m or e on t h is la t e r ) a n d se nt t o t he TPA.

z

" St a t u sI n " is t h e a ck n ow le dge m e nt m e ssa ge t h a t is r e ce iv e d fr om t he TPA.

z

" Con st a n t s" is a se lf- e x pla na t or y, syst e m m e ssa ge pr e se n t on e v e r y da t a vie w .

z

" Por t Re fe r e nce s" is a lso a sy st e m m e ssa ge w hich w ill be e x pla in e d in a la t e r se ct ion .

Figu r e 1 2 .2 2 gasTI X Orchest rat ion dat a view.

Figu r e 1 2 .2 3 Propert y page for Transact ion 1.

Figu r e 1 2 .2 4 Orchest rat ion Business process failure page.

Looking at t he dat a view, we can see t hat t he docum ent for t he " OrderI n" m essage is linked t o t he docum ent of t he " OrderOut " m essage. This m eans t hat t he ent ire docum ent is being copied. Looking at t he business process view, we can see t hat t his will happen every t im e t he schedule is execut ed. When an acknowledgem ent is received from t he TPA ( our success case) , we want t o sim ply send what t he TPA gives us. To do t his, we creat ed links from t he " St at usI n" st at us field t o t he corresponding param et er in t he " SaveSt at us" m essage. Previously, we did t he sam e t hing wit h t he order num ber. I n t he case of t ransact ion failure, we do not have any m essage from t he TPA. So in our failure pat h, we link t he order num ber from t he " OrderI n" m essage t o t he order num ber of t he " SaveSt at us_2" m essage. A const ant , st ring value is linked t o t he st at us. We know t he st at us is always " Fail" for t his pat h. So w hen t he schedule execut es and hit s t he failure pat h, it will grab t he const ant st ring of "Fail" and t he order num ber from t he " OrderI n" m essage and call t he COM+ com ponent . An arbit rary decision on our part ( and an incorrect one upon furt her review) raises an int erest ing point . For t he failure pat h, we grabbed t he order num ber from t he original m essage because we had no choice. For t he success pat h, we grabbed t he order num ber from t he acknowledgem ent m essage inst ead. I t appears t o not m at t er since t hey will be t he sam e; t hat assum pt ion holds unless t he TPA has a problem and sends an erroneous m essage. There should have been a check t o see t hat t he ret urned order num ber m at ched t he sent order num ber.

This discussion brings out som et hing t hat was im plied before t hat should now be explicit ly st at ed: in our sam ple applicat ion, t he acknowledgem ent m essage ret urns t o t he exact sam e orchest rat ion schedule t hat sent t he order ( i.e t he m essage and t he acknowledgem ent are correlat ed) . I f t his did not happen, we could not use orchest rat ion t ransact ions t o wait for t he corresponding acknowledgem ent s. Given t he sim plicit y of gasTI X fulfillm ent requirem ent s, t here are easier m et hods we could have used t o det erm ine if t he TPA processed an order and updat e t he st at us. Use of correlat ion is not crit ical for t his sim plified exam ple. How ever t here are m any cases, especially when int egrat ing t hird part y and legacy applicat ions, where correlat ing t o t he correct schedule will be invaluable, so we will dem onst rat e t his t echnique next .

Cor r e la t ion Once again, correlat ion is associat ing an inbound m essage as a response t o a previously t ransm it t ed m essage. BTS Orchest rat ion facilit at es correlat ion by generat ing a unique address for m essages t o ret urn t o. The address is im plem ent ed by using a privat e queue t hat has a GUI D in t he nam e. I n our exam ple, we are using HTTP as our t ransport prot ocol t o com m unicat e wit h our t rading part ner. As t he schedule is execut ing, it creat es a ret urn address and insert s it in t he out bound m essage. The ret urn address is an URL t o send t he acknowledgem ent . The URL has t wo param et ers in t he query st ring: t he unique queue nam e and t he BTS channel t hat will ret urn t he acknowledgem ent back t o t he schedule. Since t he queue nam e is unique for each inst ance of t he schedule, it m ust be set by t he schedule rat her t han hard coded. This is why t he ret urn address is set by t he schedule. An exam ple of a generat ed ret urn address is: HTTP://gasTIXsql.gasullivan.net/receiveresponse.asp ?channel=orderstatusfromtpa&; qpath=gasTIXsql.gasullivan.net\private$\orderstatusfromtpa{2ef8c2ed-8a67-4670-9837-c80c7f401873}

All t hree elem ent s of t he ret urn address( t wo query st ring param et ers and t he URL) can be found by looking at t he propert ies of t he BTS m essaging im plem ent at ion shape nam ed " OrderSt at usfrom TPA" ; t his is where t he schedule receives t he TPA's acknowledgem ent m essage. The propert y page of t his shape is act ually t he BizTalk m essaging binding wizard. The nam e is displayed on t he first page, while t he second page displays t he com m unicat ion direct ion as shown in figure 12.25. Since we are set t ing propert ies for acknow ledgem ent , t he schedule w ill receive a m essage. There is, however, a crypt ic m essage on t his t ab which clouds t hings a lit t le. The m essage st at es t hat " This wizard only enables receive t hrough HTTP URLs. For all ot her URLs use a m essage queue in bet ween BizTalk m essaging and t he schedule." The confusion arises from t he reference t o HTTP URLs w hen we are set t ing up t he schedule t o get a m essage from BTS. The reason for t his will becom e clear lat er in t his sect ion. The t hird page has t he XLANG schedule act ivat ion inform at ion as shown in Figure 12.26. The quest ion asked is: " Should BTS m essaging creat e a new schedule?" We have select ed " yes" previously in launching bot h t he gasTI X and TPA schedules. But t his t im e since we want t o re- ent er t he sam e schedule, we’ll select " no" inst ead. The final page is channel inform at ion which is show in Figure 12.27. Here we specify t he BTS m essaging Channel Nam e which is " OrderSt at usFrom TPA" . This t ab also provides a t ext box t o ent er an HTTP URL address where t he BizTalk m essaging service receives docum ent s. For our exam ple we have used gasTI Xsql.gasullivan.net / ReceiveResponse.asp. We can now see where t he URL and t he channel in t he reply address com e from . The final elem ent of t he reply address is not shown, but it is im plicit ly generat ed because of our select ions of " receive" and act ivat ion of new schedule set t o " no" . This forces a uniquely nam ed queue t o be creat ed for each inst ance of t he schedule. So we know where all of t he elem ent s of t he ret urn address com e from , but t he quest ion rem ains: how do t hey get int o t he out bound order m essage " OrderOut "?

Figu r e 1 2 .2 5 BizTalk Messaging Binding Wizard pg2 – Com m unicat ion Direct ion.

Figu r e 1 2 .2 6 BizTalk Messaging Binding Wizard pg3 – XLANG Schedule Act ivat ion I nform at ion.

Figu r e 1 2 .2 7 BizTalk Messaging Binding Wizard pg4 – Channel I nform at ion.

The answer is again on t he dat a view. Not ice in Figure 12.22 t hat t he Get From TPA sect ion of t he "Port References" m essage is linked t o t he Ret urnURL of t he " OrderOut " m essage. When t he schedule is execut ed, t he ret urn address is dynam ically generat ed and copied t o t he Ret urnURL. Previously we skipped over t he Port References m essage but we can now see t hat , in t his inst ance, it can be used t o provide a reference t o a port allow ing it t o be accessed ext ernally. N ot e : Por t r e fe r e n ce s ca n a lso r e ce ive da t a , w hich w ou ld m a k e t h e m dy n a m ic. For e x a m ple a que u e na m e or a poin t e r t o a n in st a n t ia t e d COM + obj e ct cou ld be pa sse d in .

The ot her pieces of t he puzzle t hat m ake correlat ion work are found out side of t he orchest rat ion schedule. The first is t he act ual ASP page t hat is t he t arget of t he reply t o URL. Rem em ber t hat t his was specified on t he propert ies dialog of t he " OrderSt at usFrom TPA" obj ect in t he schedule. This ASP page ships wit h t he BTS SDK and can be found under t he SDK at " SDK\ m essaging sam ples\ receive script s\ receiveresponse.asp" . The ASP page uses t he query st ring param et ers t o det erm ine bot h t he channel nam e and t he queue nam e t o use as shown below: queuepath = Request.QueryString("qpath") queuepath = "queue://Direct=OS:" & queuepath channelname = Request.QueryString("channel")

Not ice t hat t he t ransport t ype and queue rout ing t ype are appended t o t he queue nam e. Next it ext ract s t he m essage body from t he body of t he response obj ect . Then using t he I I nt erchange obj ect , it subm it s t he m essage t o BTS m essaging using t he follow ing line: call interchange.submit (4,PostedDocument,,,,,queuepath,channelname).

Only four param et ers are filled in. The first param et er is t he openness t ype, which indicat es t he openness t ype of t he BTS m essaging port . Openness is a concept t hat indicat es if a m essage can be sent or received from any locat ion or only from a specified locat ion. The value four corresponds t o a const ant BI ZTALK_OPENNESS_TYPE_DESTI NATI ON. This sim ply m eans t hat t he associat ed m essaging port has no specific dest inat ion. The second param et er is t he post ed docum ent . I t cont ains t he act ual m essage body t hat we've received, in t his case t he acknowledgem ent m essage from t he TPA. The t hird param et er is t he dest inat ion I D. Since t he openness t ype was specified as dest inat ion open, t his param et er is filled in t o indicat e t he act ual address where BTS m essaging should send t he m essage. Here we've filled in t he queue pat h t hat was creat ed earlier. This value was originally derived from t he propert ies of t he schedule's receive port . N ot e : The de st ina t ion I D fie ld h a s a lt e r na t e u se s de pe n ding on t h e va lu e of t he ope n n e ss t y pe . I n ca n be u se d in con j u n ct ion w it h t h e pr e ce din g pa r a m e t e r , de st ina t ion qua lifie r , t o se le ct a de st ina t ion or ga n iza t ion .

The final param et er is t he channel nam e. This specifies t he channel t hat t he m essage is subm it t ed t o. Again in our case, t his is again a value t hat is originally derived from t he propert ies of t he schedule's receive port . So as t he ASP page subm it s t he docum ent t o BTS m essaging, we have alm ost m ade t he round t rip. For our exam ple, t he channel does no m apping; we chose t o use t he TPA's acknowledgem ent form at direct ly. Therefore t he m essage goes t o t he port t hat will subm it t he m essage back t o t he originat ing schedule.

AM FL Y

The port is t ied t o a BTS applicat ion, which we have seen allows us t o int eract wit h schedules. Looking at t he propert ies of t he m essaging port , t he second t ab displays t he dest inat ion applicat ion. I n previous exam ples we have select ed a new XLANG schedule. This t im e we will select running XLANG schedule. I t m ay be unclear how BTS will invoke a schedule since no inform at ion is provided here, not even t he schedule nam e. We will assum e for t he m om ent t hat t his does send t he m essage back t o t he appropriat e schedule and m ore precisely t o t he exact inst ance of t he schedule t hat sent t he request . Finally, t he acknowledgem ent ends up back in t he schedule which can inform t he m iddle t ier of t he order st at us. But how does t his work when only scat t ered pieces of seem ingly unrelat ed inform at ion are provided?

TE

We will st art wit h what appears t o go on under t he covers and t ie t his back t o t he original overview. For all t he t alk of HTTP t ransport s in t he schedule set up, t he bot t om line is t he BTS m essaging com m unicat es wit h a running schedule via a m essage queue. I n orchest rat ion schedules, m essage queue im plem ent at ion shapes can be explicit ly creat ed or can be dynam ic- - t hat is t hey always generat e a new queue for each inst ance of t he schedule. They do t his by appending a GUI D t o t he queue nam e. When you specify a BizTalk im plem ent at ion shape t o receive a m essage int o an exist ing schedule, t he sam e t hing is done im plicit ly. This is why a queue nam e is handed out as part of t he ret urn address from t he port descript ion. On t he ret urn t rip t he ASP page does not do anyt hing special eit her. I t sim ply uses t he I I nt erchange subm it m et hod t o invoke BTS m essaging w it h an open dynam ic dest inat ion. When we look at t he propert y page of t he m essaging port t hat t arget s a running XLANG schedule, it gives us no clues as t o how BTS m essaging will com m unicat e wit h t he schedule, but again im plicit work is being done. Behind t he scenes, t he t ransport used is MSMQ, and t he dest inat ion was defined t o BTS m essaging by t he Dest inat ion I D param et er. And t his was init ially supplied by our original schedule's port definit ion. So all t he m yst ery can be sum m ed up like t his: when you want t o ret urn t o an exist ing correlat ed schedule, a uniquely nam ed m essage queue is creat ed and t he nam e is handed out by t he schedule. Then t he ret urning BTS m essaging port ( in conj unct ion wit h t he ASP page) is set up t o writ e back int o an open dest inat ion- - t hat sam e m essage queue t hat was handed out . The fact t hat unique user int erfaces are provided for t his case and t he available docum ent at ion is subt le at best m akes t his t opic rat her confusing- - when it is act ually rat her st raight forward.

Team-Fly®

Tip: H in t s w e r e dr oppe d in t h e pr e ce din g e x pla n a t ion t ha t BTS m e ssa gin g is n ot t he on ly w a y t o cor r e la t e t o a r u n n in g in st a n ce of a sch e du le . Th e on ly cr it ica l e le m e n t is t ha t a m e ssa ge qu e u e im ple m e n t a t ion sh a pe w it h dyn a m ic qu e u e na m e s be u se d. The n t h is qu e ue pa t h na m e ca n be h a nde d out . Aft e r e x e cut ion flow le a ve s t h e sche du le , t h e on ly im por t a nt t hin g is t ha t t h e r e t ur n m e ssa ge e n ds u p ba ck in t h e qu e u e ( pr ope r ly for m a t t e d a nd w it h t h e cor r e ct m e ssa ge na m e , of cou r se ) .

I m ple m e n t a t ion Ch oice s Aft er a rat her roundabout t rip, we are able t o verify t hat acknowledgem ent s com e back in t im e or t hat we have updat ed our order st at us in t he dat abase t o t he cont rary. Was t his t he best way t o achieve t his obj ect ive? Probably not . All of t his effort could have been accom plished wit h a sim ple set of logic t hat scanned our dat abase periodically looking for orders wit hout acknowledgem ent s. I t could have resubm it t ed an overdue m essage t o BizTalk again and m ark t hose t hat have passed som e m axim um t im e lim it t o failed. Bot h t his process and our schedule could have t aken ot her act ions besides sim ply updat ing st at us. The orchest rat ion im plem ent at ion m ay have been bet t er suit ed t o handle addit ional workflow t asks t hat could involve cust om er service or I T depart m ent s in t rying t o t rouble- shoot t he failed m essages. This orchest rat ion solut ion, however, also com es at a higher resource cost . All of t hese inst ances of t he schedules hang around unt il t hey have com plet ed a pat h. BTS does have a solut ion for t his called hydrat ion. Essent ially, schedules t hat m eet cert ain crit eria and have been idle for a while will be dehydrat ed- - t heir st at e saved and st ored t o a SQL server dat abase. When t he appropriat e request com es in, t hey are rehydrat ed- - pulled from t he dat abase, rest ored t o t heir prior st at e and execut ed where t hey left off. This eases t he resource load as all of t he schedules do not have t o be act ive at t he sam e t im e. However t here are st ill pot ent ially a large num ber of schedules act ive at one t im e. We have not perform ed resource t est ing t o gauge t he im pact - - but it will definit ely be m ore t han a sim ple dat abase rout ine run periodically. Again, m ore com plicat ed scenarios will clearly show benefit s of using t his schem e over sim pler im plem ent at ions.

Upda t in g Or de r St a t u s The last sect ion of t his chapt er deals wit h act ually updat ing t he st at us of t he order in t he dat abase. We could invoke a Visual St udio 6 com ponent t o updat e t he dat abase, but t hat is not t he t opic of t his book. We cannot direct ly invoke a .NET com ponent because, as we have previously not ed, t he m essaging and orchest rat ion engines only recognize COM+ com ponent s at t his point in t im e. So t he choices t hat are left are t o m ake a .NET com ponent look like a COM+ com ponent or t o direct ly invoke t he web service wit h a SOAP m essage. We select ed t he easiest pat h and placed a COM+ wrapper around a .NET com ponent . A bet t er reason for t his approach is t hat we are on t he sam e net w ork as t he m iddle t ier, so t here is no need t o act ually incur t he overhead of using a web service. Tradit ionally DCOM would be used t o fit t his need but .NET int roduces a new t echnique called rem ot ing. Sim ilar t o DCOM, t his allows execut ion of an assem bly t o t ake place on a rem ot e server. For m ore inform at ion on rem ot ing, see chapt er 13 “ .NET Rem ot ing” . N ot e : BizTa lk Se r ve r 2 0 0 0 is st ill a m a t u r in g pr odu ct but t he r e a r e st ill se ve r a l good r e sour ce s a va ila ble : z

Th e in clu de d h e lp syst e m is a ct u a lly ve r y good.

z

Th e Officia l M icr osoft sit e a t w w w .m icr osoft .com / bizt a lk h a s a n u m be r of w h it e - pa pe r s a n d t e ch n ica l a r t icle s

z

z

Th e M icr osoft pu blic n e w s gr ou ps a t m sn e w s.M icr osoft .com h a ve se ve r a l good discu ssion s on BizTa lk . in clu de M icr osoft .pu blic.bizt a lk .ge n e r a l, M icr osoft .public.bizt a lk .or ch e st r a t ion , a n d M icr osoft .pu blic.bizt a lk .se t u p Th e r e a r e a n u m be r of BizTa lk a r t icle s a n d t ut or ia ls on t he TopXM L.com n e t w or k . w w w .vbx m l.com / b2 b/ .

Th is is loca t e d a t

Pa t h s

Fr om H e r e I n reviewing t he im plem ent at ion of our fulfillm ent process, we have seen m any different t echniques. We have m ade heavy use bot h of BizTalk's m essaging and scheduling engines. The feat ures and t heir set up are not always so clear, but t heir power and flexibilit y are. For a first generat ion product , BizTalk is really quit e good. I t will undoubt edly becom e an invaluable t ool for int egrat ing w it h t rading part ners- - bot h int ernal and ext ernal. I t will also prove it s wort h in orchest rat ing work flow and t ying legacy and forward leaning applicat ions t oget her int o sm oot hly flowing process. Again, it m ay seem easier t o lean on exist ing m et hods for perform ing all of t hese t asks t hat BTS is perform ing, but once a m inim al level of com fort is reached wit h t he product , it will be fast er and easier t o use BTS. I t will becom e an essent ial t ool in every developm ent shop's bag of t ricks. To read a m ore det ailed discussion of Rem ot ing in .NET, see Chapt er 13, ".NET Rem ot ing." Chapt er 3, " Enabling I nt er- Applicat ion Com m unicat ion", discusses t he various ways in which dist ribut ed .NET syst em s pass dat a.

1 3 .N ET Re m ot ing One area of .NET t hat has int roduced significant changes from t he Windows DNA world is t he concept of rem ot ing. You can use .NET Rem ot ing t o enable different applicat ions t o com m unicat e wit h one anot her, whet her t hose applicat ions reside on t he sam e com puter, on different com put ers in t he sam e local area net work, or across t he world in very different net works. The rem ot ing infrast ruct ure abst ract s t he com plexit ies of int erprocess com m unicat ion. Rem ot ing is sim ilar t o Web Services in som e ways, but rem ot ing is m uch m ore ext ensible. While web services allow for high- level access t o obj ect s via SOAP, rem ot ing allows for low- level cont rol over t he act ual com m unicat ion. The rem ot ing infrast ruct ure is m ore ext ensible t han web services because it allows developers t o choose t he prot ocol and form at for m essage t ransport , or t o build t heir own prot ocols and form at t ers. Using rem ot ing, developers can int ervene nearly at any st age wit hin t he com m unicat ion cycle. I n t his chapt er we discuss rem ot ing by covering som e key concept s, including: applicat ion dom ains, rem ot able obj ect s, channels, and serializat ion. We t hen discuss how t o configure applicat ions for rem ot ing and show how t o const ruct a host and client for t he gasTI X AddressValidat ionService. Finally, we cover t he concept of act ivat ion and lifet im e cont rol.

Applica t ion D om a ins A .NET applicat ion is defined by one or m ore assem blies t hat run inside an Applicat ion Dom ain, or AppDom ain. An assem bly is essent ially a collect ion of t ypes represent ed as an execut able ( .exe) or a dynam ic link library ( .dll) file t hat share a com m on securit y and version boundary. I t is a fundam ent al building block of all .NET applicat ions. Typically operat ing syst em s and runt im e environm ent s provide som e sort of applicat ion isolat ion. AppDom ains provide a way t o isolat e assem blies from ot hers in t he sam e operat ing syst em process as well as t hose loaded in different processes. The isolat ion of .NET assem blies allows for im proved scalabilit y, secure applicat ions, and bet t er fail- over capabilit ies. By definit ion, all .NET applicat ion is t ype- safe. This allows assem blies t o be grouped t oget her along specific securit y policies, configurat ion set t ings, and runt im e behavior. These charact erist ics define t he boundary of an AppDom ain. N ot e : A t ype is a cla ssifica t ion of sim ila r ca t e gor ie s or va r ia ble s w h ich sh a r e t h e sa m e da t a a n d be ha v ior . I t ca n r e pr e se n t a cla ss, in t e r fa ce , a r r a y , e n um , or int e gr a l t ype s su ch a s in t e ge r , lon g, or de cim a l. Th e com m on t ype syst e m pr ovide d by t h e .N ET fr a m e w or k e na ble s cr oss- la n gua ge int e gr a t ion a n d a t ype - sa fe e n vir on m e nt .

An AppDom ain can be t hought of as a " logical process." . Windows OS processes can cont ain m ore t han one AppDom ain w it h t he sam e isolat ion level as if t he m ult iple AppDom ains are in separat e processes. The AppDom ains do not share inform at ion, such as global variables, st at ic funct ions, et c. This isolat ion ext ends t o t ype discovery. A t ype can discover t he ot her t ypes in it s AppDom ain and call t hem direct ly; however, it cannot discover t ypes out side it s AppDom ain. .NET rem ot ing is t he way in which AppDom ains com m unicat e wit h each ot her. By using rem ot ing, an obj ect in one AppDom ain can access an obj ect in anot her AppDom ain. Let 's t ake a det ailed look at how rem ot e obj ect s are called.

Re m ot a ble ve r su s N on - Re m ot a ble Obj e ct s Since t ypes are not shared across AppDom ains, .NET rem ot ing enables an obj ect t o be m arshaled from one AppDom ain t o anot her AppDom ain. I n t his way, an obj ect wit hin specific AppDom ains can expose it s funct ionalit y t o ot her obj ect s out side it s own AppDom ain. .NET rem ot ing draws a dist inct ion bet ween obj ect s wit hin an AppDom ain. An obj ect t hat can only be accessed by ot her obj ect s wit hin it s AppDom ain is said t o be non- rem ot able. The funct ionalit y provided by t he obj ect is only available from wit hin it s AppDom ain. An obj ect t hat can be accessed from wit hin it s AppDom ain and by ot her obj ect s out side it s AppDom ain is said t o be rem ot able. There are t w o different t ypes of rem ot able obj ect s: Marshal- By- Value and Marshal- By- Reference.

M a r sh a l- By- Va lu e A rem ot able obj ect whose inst ance dat a is copied from one AppDom ain t o anot her is considered a Marshal- by- Value ( MBV) obj ect . When an MBV obj ect is request ed from a rem ot e AppDom ain, .NET rem ot ing creat es a new inst ance of t he rem ot e obj ect . The value or current st at e of t he obj ect is t hen copied back t o t he calling AppDom ain where a new local inst ance is creat ed. Any subsequent call t o t he obj ect is handled by t he local inst ance of t he obj ect . I n order for an obj ect t o be copied, t he .NET rem ot ing m ust know how t o serialize t he obj ect , i.e. writ e t he current st at e of an obj ect t o a form t hat can be t ransport ed t o anot her AppDom ain. By adorning an obj ect wit h t he SerializableAt t ribut e as seen in List ing 13.1, t he syst em will serialize t he obj ect st at e aut om at ically. An obj ect can also cont rol it s own serializat ion by im plem ent ing t he I Serializable int erface. List in g 1 3 .1 Cr e a t in g a M a r sha l- by- Va lu e Obj e ct [Serializable] public class GASException : ApplicationException { private GASErrors _errorList = null; public GASException(GASErrors errorList) { errorList = errorList; } /// /// Property ErrorList returns the error list from the Exception /// public GASErrors ErrorList { get { return _errorList; } } } Ca ut ion: Ca r e sh ou ld be t a k e n w h e n de t e r m in in g w h ich obj e ct s a r e su it a ble t o be m a r sh a le d- by- va lu e . I t is n ot good pr a ct ice t o de cla r e la r ge obj e ct s a s m a r sh a l- by- va lue , or obj e ct s t ha t r e qu ir e fe w r ou n d t r ips a cr oss a n e t w or k w h ich sh ou ld be de cla r e d a s m a r sha l- by- r e fe r e n ce .

N ot e : On ce a n obj e ct is m a r sha le d- by - va lu e , it s st a t e is n e v e r r e t u r ne d t o t h e or igin a t in g AppD om a in . An y ch a n ge s t o t h e st a t e of t h e obj e ct in t h e ca lling AppD om a in a r e n ot com m u n ica t e d ba ck t o t h e sou r ce AppD om a in . Th e obj e ct , a n d h e n ce it s st a t e , ca n be r e t u r ne d t o t he or igin a t in g AppD om a in on a su bse qu e nt m e t h od ca ll.

M a r sh a l- by- Re fe r e n ce The second t ype of rem ot able obj ect is a Marshal- by- Reference ( MBR) obj ect . This t ype of obj ect is not copied t o t he caller, but exist s in a rem ot e AppDom ain. When a client request s a new inst ance of a MBR obj ect , a proxy is creat ed in t he local AppDom ain and a reference t o t hat proxy is ret urned t o t he caller. When t he client m akes a call t o t he proxy, .NET rem ot ing forwards t he call t o t he act ual obj ect in t he rem ot e AppDom ain. An obj ect is considered m arshal- by- reference if it is derived from Syst em .MarshalByRefObj ect or one of it s children as shown in List ing 13.2. The AddressValidat ion obj ect ( List ing 13.2) is used by t he gasTI X syst em t o validat e addresses. This obj ect runs on a physically separat e m achine t han t he gasTI X sit e. Since it is desired t o run t his obj ect on t his separat e m achine, it m ust be an MBR. MBR obj ect s are very useful when t he st at e and funct ionalit y of t he obj ect should st ay in t he originat ing AppDom ain, such as a dat abase connect ion, file handle, or anot her dependent applicat ion running on t he sam e m achine. List in g 1 3 .2 Addr e ssVa lida t ion M a r sh a l- by - Re f Cla ss using System; public class AddressValidation : System.Web.Services.WebService { public AddressValidation() { //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent(); } /// /// Validate the passed-in address using default connection string. /// [WebMethod] public string ValidateAddress(string addressLine1, string addressLine2, string addressLine3, _ string city, string region, string postalCode, string country) { . . . } /// /// Lookup address in the database /// private void LookupAddress(string connectionString, string addressLine1, string addressLine2, _ string addressLine3, string city, string region, string postalCode, string country, _ Address addressData) { . . . } }

Cha nne ls A channel is used t o send m essages across rem ot ing boundaries. Rem ot ing boundaries occur bet ween AppDom ains, processes, or m achines. A channel's prim ary responsibilit y is t o convert m essages t o and from specific prot ocols. A channel list ens on an endpoint for inbound m essages t o be processed or out bound m essages t o forward t o anot her endpoint , or bot h. This design allows you t o plug any channel, using a wide range of prot ocols, int o t he rem ot ing fram ework. When a client invokes a m et hod on a rem ot e obj ect , t he call inform at ion, such as param et ers and ot her m et adat a, are t ransport ed t o t he rem ot e obj ect t hrough a channel. Aft er t he rem ot e obj ect has com plet ed t he request , t he result s are ret urned t o t he caller in t he sam e way ( see Figure 13.1) .

Figu r e 1 3 .1 Com m unicat ion using a channel across an AppDom ain boundary.

All channel obj ect s im plem ent t he I Channel int erface. The I Channel int erface provides a m eans t o get inform at ion about a channel such as t he nam e or priorit y. For channels designed t o list en on an endpoint for a specific prot ocol im plem ent t he I ChannelReceiver int erface. Channels designed t o send m essages t o anot her endpoint im plem ent t he I ChannelSender int erface. Most channels included in t he .NET fram ew ork im plem ent bot h int erfaces. The server m ust regist er one or m ore channels before any rem ot e obj ect s can be used by a client . Channels are specific t o an Applicat ion Dom ain and m achine. For exam ple, t wo AppDom ains running on a single m achine cannot regist er t he sam e channel t o list en on t he sam e port . A client can select any channel provided by t he server t o com m unicat e wit h a rem ot e obj ect . On t he server, several rem ot e obj ect s can share a single channel. I n order for a channel t o handle m ult iple client request s concurrent ly, each connect ion is handled in it s own t hread.

The .NET fram ework includes t wo channels, t he Ht t pChannel and t he TcpChannel, t o t ransm it m essages using different prot ocols. The HTTPChannel class provides a way t o com m unicat e wit h a rem ot e obj ect using t he SOAP prot ocol by default . Before a m essage is sent , it is form at t ed int o XML and serialized for delivery. A binary form at can also be used t o serialize a m essage. I n t his case, t he st ream is t ransport ed using t he HTTP prot ocol. The TcpChannel class provides t he m eans t o t ransm it a m essage using t he TCP prot ocol. A binary form at t er is used by default t o serialize t he m essage for t ransport . Message serializat ion and form at t ing are discussed in t he following sect ion.

Se r ia lizin g M e ssa ge s As you have seen, m essages t hat cross rem ot ing boundaries are serialized before t hey are sent and de- serialized upon delivery. Serializat ion is t he process of writ ing inform at ion int o a t ransport able form . .NET fram ework provides classes t o serialize m essages. The SoapForm at t er class is used t o serialize and deserialize m essages in t he SOAP form at . The BinaryForm at t er class is used t o com plet e t he sam e t ask in a binary form at . As described in t he sect ion " Marshal- by- Value" earlier in t his chapt er, an obj ect adorned wit h t he Serializable at t ribut e is aut om at ically serialized by t he fram ework when request ed by eit her t he SoapForm at t er or BinaryForm at t er. An obj ect can cont rol it s own serializat ion by im plem ent ing t he I Serializable int er face. At t he t im e of serializat ion, t he form at t er calls t he Get Obj ect Dat a m et hod which provides t he dat a required t o represent t he rem ot e obj ect . N ot e : You ca n t h in k of t h e se r ia liza t ion cla sse s a s plu g- in s for a ch a n ne l. I t is possible for a n H t t pCha n ne l t o u se t h e Bin a r yFor m a t t e r cla ss a n d a TcpCha n n e l t o u se t h e Soa pFor m a t t e r cla ss.

Re m ot ing Configur a t ion Opt ions I n t he DCOM world, configurat ion for COM com ponent s occurred in t he regist ry. The regist ry cont rolled where com ponent s were creat ed by default , using a key t hat specified t he nam e of t he server on which t o creat e a t ype. Unlike DCOM, which required t hat only t he endpoint be specified, rem ot ing requires t hat t he prot ocol, form at t er and endpoint be specified in order t o rem ot e a request t o a t ype in anot her applicat ion dom ain. The .NET fram ework provides for t wo m echanism s t o specify t he rem ot ing configurat ion on bot h t he host and t he client applicat ion: program m at ic configurat ion and configurat ion files. Program m at ic configurat ion involves coding int o your rem ot ing host or client t he appropriat e configurat ion inform at ion. List ing 13.3 shows code t hat is used t o program m at ically configure a rem ot ing host . List in g 1 3 .3 Pr ogr a m m a t ic Re m ot ing Configur a t ion Code HttpChannel hChannel = new HttpChannel(8095); TcpChannel tChannel = new TcpChannel(8096); ChannelServices.RegisterChannel(hChannel); ChannelServices.RegisterChannel(tChannel); RemotingConfiguration.RegisterWellKnownServiceType( typeof(gasTIX.AddressValidation.AddressValidation), "AddressValidation", WellKnownObjectMode.SingleCall);

The list ing shows t hat t he rem ot ing host will accept rem ot ing calls on bot h t he TCP channel 8096 and HTTP channel 8095. The code regist ers t hose channels and regist ers t he rem ot ed t yped wit h t he endpoint " AddressValidat ion."

Program m at ic configurat ion m ay not be t he m ost flexible way t o configure your applicat ion. Changes in t he rem ot ing configurat ion, for exam ple, using a different channel or using a separat e form at t er, would require recom pilat ion and redeploym ent of bot h t he rem ot ing client and t he rem ot ing host . A bet t er way t o specify rem ot ing configurat ion would be t o load t he configurat ion inform at ion from a file. The use of Rem ot ing Configurat ion files for bot h t he rem ot ing client and a rem ot ing host allow for flexibilit y when it is uncert ain which form at t er and channel will be used for rem ot ing. I f t he rem ot ing channel needs t o be changed, rem ot ing configurat ion files allow for a configurat ion file t o be deployed. No recom pilat ion and redeploym ent of an applicat ion is necessary. I n order t o load t he rem ot ing configurat ion file, t he follow ing code m ust be placed in bot h t he rem ot ing client and rem ot ing host : Rem ot ingConfigurat ion.Configure( filenam e) ; Microsoft st rongly recom m ends ( but does not require) t hat configurat ion files be nam ed app.exe.config in order t o prevent nam ing collisions. This recom m endat ion applies in all cases except when t he rem ot ing host or rem ot ing client are r un inside I I S's process space. I n t his case, rem ot ing configurat ion inform at ion m ust be placed in t he web.config file. List ing 13.4 is t he equivalent rem ot ing configurat ion t o List ing 13.3 specified in a configurat ion file rat her t han in code. List in g 1 3 .4 Re m ot in g Con figu r a t ion File



node, t he bcp ut ilit y is called t o load in t he dat a in t he t able from t he respect ive t ext file. List in g 1 6 .6 Th e ga st ix .da t a _ loa d.x m l file in st r u ct s t he D a t a Loa d m e t hod w h ich t e x t file s t o bcp in t o t h e da t a ba se .





I n order t o provide a basic level of securit y, we hash t he dat abase passwords ent ered by t he user at inst all t im e. To do t his, we use a sim ple algor it hm shown in List ing 16.7, det ailed on MSDN. We m ake a slight m odificat ion by adding 70 t o t he Xor st at em ent so t hat we only receive t ext charact ers, and no cont rol charact ers. List in g 1 6 .7 Th e sim ple En cr y pt m e t h od pr ovide s a h a sh of t h e da t a ba se pa ssw or ds so t h e y w ill n ot a ppe a r in cle a r t e x t . Public Shared Function Encrypt(ByVal secret, ByVal password) As String ' This simple string hash encryption routine was found ' on MSDN. It has been modified to add 70 to the Xor ' so that only text characters result. ' secret = the string you wish to encrypt or decrypt. ' password = the password with which to encrypt the string. Dim encryptData = secret & ENCRYPT_PADDING Dim R As String Dim L As Integer = Len(password) Dim X As Integer Dim myChar As Integer For X = 1 To Len(encryptData) myChar = Asc(Mid(password, (X Mod L) - L * ((X Mod L) = 0), 1)) R = R & Chr(Asc(Mid(encryptData, X, 1)) Xor myChar + 70) Next Return R End Function

The gasTI X applicat ion is configured so t hat t he dat abase connect ion st rings are locat ed in t he obj ect const ruct or st rings of t he m iddle- t ier gasTI X.Dat aAccess com ponent s. I n order for t he applicat ion t o find t he gasTI X dat abase successfully, we need t o change t hese const ruct or st rings at inst allat ion based on t he opt ions ent ered by t he user. The ModifyDat aAccessConst ruct orSt ring m et hod perform s t his work for us, wrapping t he legacy COMAdm in com ponent t o provide t he funct ionalit y needed t o m anipulat e COM+ Applicat ions, com ponent s, and propert ies. By sim ply adding a reference t o t he COMAdm in.dll ( norm ally locat ed in C: \ WI NNT\ syst em 32\ Com ) , .NET allows us t o int eroper at e wit h t his legacy DLL. For m ore inform at ion on COM+ I nt eroperabilit y, see Chapt er 9, " COM+ I nt egrat ion." List ing 16.8 dem onst rat es t he m et hod t hat perform s t his work. List in g 1 6 .8 A m e t h od is in clu de d in t h e ga sTI X in st a ll pr ogr a m t o a cce ss a n d m odify COM + con st r u ct or st r in gs for da t a ba se con n e ct iv it y . Private Sub ModifyDataAccessConstructorString()

Log( " Modifying COM+ const ruct or st rings for gasTI X.Dat aAccess com ponent s." )

Dim Cat alog As New COMAdm in.COMAdm inCat alog( ) Dim Applicat ions As COMAdm in.COMAdm inCat alogCollect ion Applicat ions = Cat alog.Get Collect ion( "Applicat ions" ) Applicat ions.Populat e( ) Dim oApplicat ion As COMAdm in.COMAdm inCat alogObj ect Dim gasTI XDat aAccess As COMAdm in.COMAdm inCat alogObj ect For Each oApplicat ion I n Applicat ions I f oApplicat ion.Nam e = " gasTI X.Dat aAccess" Then gasTI XDat aAccess = oApplicat ion End I f Next I f gasTI XDat aAccess I s Not hing Then Throw New Applicat ionExcept ion( " Failed t o find gasTX.Dat aAccess in COM+ cat alog." ) End I f Dim oCom ponent As COMAdm in.COMAdm inCat alogObj ect Dim Com ponent s As COMAdm in.COMAdm inCat alogCollect ion Com ponent s = Applicat ions.Get Collect ion( " Com ponent s" , gasTI XDat aAccess.Key) Com ponent s.Populat e( ) Dim st rgasTI XObj Const = St ring.Form at ( CONNECTI ON_STRI NG, _gasTI XServer, _gasTI XDat abase, _ _gasTI XUser, _gasTI XPassword) For Each oCom ponent I n Com ponent s oCom ponent .Value( " Const ruct orSt ring" ) = st rgasTI XObj Const Next Com ponent s.SaveChanges( ) End Sub

Because t he AddressValidat ion rem ot ing host is not a COM+ applicat ion, it s dat abase connect ion st ring is st ored in a different locat ion. We have chosen t o place t he connect ion st ring in a .config file, found in t he \ AddressValidat ion\ AddressValidat ionSvc\ AddressValidat ionSvc.exe.config file. This is an XML file, so t he relevant m et hod in List ing 16.9 enables us t o find and m odify t he appropriat e XML node in t his file. List in g 1 6 .9 An ot h e r m e t h od is in clu de d in t h e ga sTI X in st a ll pr ogr a m t o a cce ss a nd m odify t h e Addr e ssVa lida t ion da t a ba se con n e ct ion st r in g in a n XM L con figu r a t ion t e x t file . Private Sub ModifyAddressValidationConnectionString() Log("Modifying connection string for the AddressValidation database.") Dim strAVS As String = _TargetDir & ADDRESS_VALIDATIONSVC_BIN Dim xmlFile As New XmlDocument() xmlFile.Load(strAVS & "AddressValidationSvc.exe.config") Dim myNodeList As XmlNodeList = xmlFile.SelectNodes("configuration/appSettings/add") Dim myNode As XmlNode For Each myNode In myNodeList If myNode.Attributes("key").Value = "dsn" Then myNode.Attributes("value").Value = String.Format(CONNECTION_STRING, _AddressServer, _

_AddressDatabase, _AddressUser, _AddressPassword) xmlFile.Save(strAVS & "AddressValidationSvc.exe.config") Exit For End If Next xmlFile = Nothing End Sub

The m et hod t o pre- JI T t he gasTI X assem blies is nam ed HandlePreJI T, and is shown in List ing 16.10. The m ult iple use of Chr( 34) in t his m et hod and t hroughout t he I nst allClass.vb class exist s so t hat a double- quot e wraps each com m and- line argum ent . This is needed in case t he user is inst alling t o a file locat ion wit h a space, such as t he default C: \ Program Files\ G.A. Sullivan\ . Wit hout a double- quot e Chr( 34) , t he com m and- line NGen ut ilit y will not be able t o read t he argum ent s wit h em bedded spaces, and will produce unexpect ed result s. List in g 1 6 .1 0 Th e H a n dle Pr e JI T m e t h od w ill in v ok e t he N Ge n u t ilit y t o pr e - JI T t h e ga sTI X a sse m blie s if t h e u se r ha s se le ct e d t h is opt ion . Private Sub HandlePreJIT(ByVal executeInstall As Boolean)

If _PreCompile = PRECOMPILE_OPTION Then Log("Running Ngen utility on gasTIX assemblies.") Dim args As New StringBuilder() If executeInstall Then args.Append(Chr(34) & _TargetDir) args.Append(ADDRESS_VALIDATIONSVC_BIN & "AddressValidation.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(ADDRESS_VALIDATIONSVC_BIN & "AddressValidationSvc.exe" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.Business.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.BusinessFacade.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.Data.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.DataAccess.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.Security.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.UI.dll" & Chr(34) & " ") args.Append(Chr(34) & _TargetDir) args.Append(GASTIX_WEB_BIN_DIR & "gasTIX.SystemFramework.dll" & Chr(34) & " ") Else args.Append("/delete ") args.Append("AddressValidation ") args.Append("AddressValidationSvc ") args.Append("gasTIX.Business ") args.Append("gasTIX.BusinessFacade ") args.Append("gasTIX.Data ") args.Append("gasTIX.DataAccess ") args.Append("gasTIX.Security ")

args.Append("gasTIX.UI ") args.Append("gasTIX.SystemFramework ") End If InstallUtil.ExecuteProcess(Chr(34) & _DotNetDir & "ngen.exe" & Chr(34), _ args.ToString()) End If End Sub Tip: An y Visu a l St u dio .N ET de ploym e n t pa ck a ge sh ou ld be t e st e d on m a n y diffe r e n t pla t for m s, a n d e a ch pe r m u t a t ion of e a ch inst a lla t ion opt ion sh ou ld be t r ie d. W e sh ou ld t e st ou r in st a lls t o r e m ot e m a ch in e s a n d t o folde r s w it h spa ce s in t h e folde r na m e . Pu r pose fu lly t r y t o cr a sh t h e in st a lla t ion pr ogr a m . Aft e r in st a ll, r e m ove pie ce s of t h e a pplica t ion t o se e if t h e u n inst a ll pr ogra m ca n st ill pr oce e d. Th e ove r a ll goa l is t o m a k e t h e in st a ll a nd u n in st a ll pr oce sse s a s sm oot h a s possible for ou r u se r s. Th e r e is n o ov e r st a t in g t h e be a ut y of a cle a n , w e ll- de live r e d in st a lla t ion pr ogr a m , e spe cia lly w h e n you a r e on t h e r e ce ivin g e n d.

Several addit ional helper m et hods relat ed t o I I S virt ual direct ory adm inist rat ion, called by t he m ain I nst all m et hod, are cont ained in t he I I SAdm in.vb class, shown in it s ent iret y in List ing 16.11. List in g 1 6 .1 1 Th e I I SAdm in .v b cla ss con t a in s se ve r a l h e lpe r m e t h ods r e la t e d t o virt u a l dir e ct or y a dm in ist r a t ion , ca lle d by t h e I n st a ll a n d Un in st a llBa se m e t h ods.

Imports System.DirectoryServices Imports gasTIXInstall.Exceptions Public Class IISAdmin Private _server As String

AM FL Y

Imports System

TE

Public Sub New(ByVal server As String) _server = server End Sub

Public Sub CreateVirtualDir(ByVal directoryName As String, ByVal path As String) Dim newSite As DirectoryEntry Dim webroot As DirectoryEntry Try If Not DirectoryEntry.Exists("IIS://" & _server & "/W3SVC/1") Then Throw New DefaultWebSiteNotFoundException("Failed to find default web site.") End If If DirectoryEntry.Exists("IIS://" & _server & "/W3SVC/1/Root/" & directoryName) Then Throw New VirtualDirectoryExistsException("Virtual directory " & directoryName &_ " already exists.") End If webroot = New DirectoryEntry("IIS://" & _server & "/W3SVC/1/Root") newSite = webroot.Children().Add(directoryName, "IIsWebVirtualDir") newSite.CommitChanges()

Team-Fly®

newSite.Properties("AccessExecute")(0) = True newSite.Properties("AccessWrite")(0) = True newSite.Properties("AccessScript")(0) = True newSite.Properties("AccessRead")(0) = True newSite.Properties("AccessSource")(0) = True newSite.Properties("Path")(0) = path newSite.CommitChanges() Dim args(0) As Object args(0) = 2 newSite.Invoke("AppCreate2", args) newSite.Properties("AppFriendlyName")(0) = directoryName newSite.CommitChanges() Catch ex As Exception Throw ex End Try End Sub Public Sub DeleteVirtualDir(ByVal directoryName As String) Try If Not DirectoryEntry.Exists("IIS://" & _server & "/W3SVC/1") Then Throw New DefaultWebSiteNotFoundException("Failed to find default web site.") End If If Not (DirectoryEntry.Exists("IIS://" & _server & "/W3SVC/1/Root/" & directoryName)) _ Then Throw New VirtualDirectoryNotFoundException("Virtual directory " & directoryName & _ " does not exists.") End If

Dim webItem As DirectoryEntry = New DirectoryEntry("IIS://" & _server & "/W3SVC/1/Root/"_ & directoryName) Dim webParent As DirectoryEntry = New DirectoryEntry("IIS://" & _server & "/W3SVC/1/Root") webItem.Invoke("AppDelete", Nothing) webParent.Children.Remove(webItem) Catch ex As Exception Throw ex End Try End Sub End Class

The Uninst all and Rollback m et hods need t o do t he sam e basic work. Rollback will cont inue if it encount ers any error condit ion in case it t ries t o perform an uninst all funct ion t hat was not yet reached during t he abort ed inst all process. ( We inst ruct t he inst aller t o cont inue during Rollback by using Try..Cat ch blocks wit hout t hrowing errors, but inst ead only displaying m essage boxes) . Because Uninst all and Rollback do t he sam e work, we have abst ract ed t he im plem ent at ion out t o a separat e m et hod which bot h of t hese m et hods call, sim ply passing in a param et er t o not ify t he m et hod of ident it y ( eit her Rollback or Uninst all) of t he caller. This abst ract ion is shown in list ing 16.12. List in g 1 6 .1 2 Th e Rollba ck a n d Un in st a ll m e t h ods n e e d t o pe r for m t h e sa m e w or k , so t h e y ca ll t h e sa m e su b- m e t h od.

Public Overrides Sub Rollback(ByVal savedState As System.Collections.IDictionary)

MyBase.Rollback(savedState) UninstallBase(savedState) End Sub

Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary) MyBase.Uninstall(savedState) UninstallBase(savedState) End Sub The Uninst allBase m et hod it self is where cust om uninst allat ion act ions, such as rem oving t he dat abases, rem oving t he virt ual direct ory, and rem oving assem blies from t he Nat ive I m age Cache are perform ed. This m et hod is shown in it s ent iret y in List ing 16.13. List in g 1 6 .1 3 Th e Un in st a llBa se m e t hod pe r for m s t h e cu st om u n in st a lla t ion a ct ions r e qu ir e d for com ple t e ly r e m ovin g t h e ga sTI X a pplica t ion . Private Sub UninstallBase(ByVal savedState As System.Collections.IDictionary) Try Initialize(savedState) ' STEP 1: Remove the gasTIX virtual directory in IIS Dim vDir As gasTIXInstall.IISAdmin Try Log("Deleting gasTIX virtual directory.") vDir = New gasTIXInstall.IISAdmin("localhost") vDir.DeleteVirtualDir(GASTIX_PRODUCT_NAME) Catch e As Exception Log("Error encountered: " & _LastLogMsg & vbCrLf & e.Message) Finally vDir = Nothing End Try ' STEP 2: From here we need to stop IIS Log("Stopping IIsAdmin service.") InstallUtil.ExecuteProcess("iisreset", "/stop") ' STEP 3: Uninstall the databases Log("Removing databases.") Dim dbInstall As DatabaseInstaller Try Log("Removing gasTIX database.") dbInstall = New DatabaseInstaller(_gasTIXServer, _gasTIXDatabase, _gasTIXUser, _ gasTIXPassword) Log("Terminating gasTIX database connections.")

dbInstall.KillConnections() Log("Deleting gasTIX database.") dbInstall.UninstallDatabase() Catch e As Exception Log("Error encountered: " & _LastLogMsg & vbCrLf & e.Message) Finally dbInstall = Nothing End Try Try Log("Removing Address database.") dbInstall = New DatabaseInstaller(_AddressServer, _AddressDatabase, _AddressUser, _AddressPassword) Log("Terminating Address database connections.") dbInstall.KillConnections() Log("Deleting Address database.") dbInstall.UninstallDatabase() Catch e As Exception Log("Error encountered: " & _LastLogMsg & vbCrLf & e.Message) Finally dbInstall = Nothing End Try ' Step 4: Remove all the gasTIX objects from the native image cache HandlePreJIT(False) ' STEP 5: Unregister the gasIX.Business and gasTIX.DataAccess objects in COM+ Log("Unregistering gasTIX assemblies with COM+.") Dim regHelper As RegistrationHelper = New RegistrationHelper() Log("Unregistering gasTIX.Business assembly with COM+.") regHelper.UninstallAssembly(_TargetDir & GASTIX_WEB_BIN_DIR & "gasTIX.Business.dll", Nothing, Nothing) Log("Unregistering gasTIX.DataAccess assembly with COM+.") regHelper.UninstallAssembly(_TargetDir & GASTIX_WEB_BIN_DIR & "gasTIX.DataAccess.dll", Nothing, Nothing) regHelper = Nothing Log("Deleting COM+ TypLibs.") Try File.Delete(_TargetDir & GASTIX_WEB_BIN_DIR & "gasTIX.Business.tlb") Catch e As Exception Log("Error encountered: " & _LastLogMsg & vbCrLf & e.Message) End Try Try File.Delete(_TargetDir & GASTIX_WEB_BIN_DIR & "gasTIX.DataAccess.tlb") Catch e As Exception Log("Error encountered: " & _LastLogMsg & vbCrLf & e.Message) End Try ' STEP 6: Uninstall the Address Validation remoting host ' (either a Windows Service or an out-of-process EXE) If _AddressRemote = REMOTING_WINDOWS_SERVICE_OPTION Then Log("Stopping Address Validation remoting host Windows Server.")

Dim addressService As New ServiceProcess.ServiceController("Address Validation") If Not addressService Is Nothing Then addressService.Stop() addressService = Nothing Else Log("Error encountered: " & _LastLogMsg & vbCrLf & "Address Validation Windows service not found.") End If Log("Uninstalling Address Validation remoting host as a Windows Service.") InstallUtil.ExecuteProcess(Chr(34) & _DotNetDir & "installutil.exe" & Chr(34), "/u " & Chr(34) & _TargetDir & ADDRESS_VALIDATIONSVC_BIN & "AddressValidationSvc.exe" & Chr(34)) End If Catch e As Exception Log("Error encountered: " & _LastLogMsg & vbCrLf & e.Message) Throw New ApplicationException(_LastLogMsg) Finally ' STEP 7: Lets restart IIS InstallUtil.ExecuteProcess("iisreset", "/start") Uninitialize(savedState) End Try End Sub

As you can see, we have creat ed a highly ext ensible Windows I nst aller program by overriding t he built - in I nst all, Rollback, and Uninst all m et hods of Syst em .Configurat ion.I nst all.I nst aller ( we have not used t he Com m it m et hod) . Cust om act ions call our m et hods at t he appropriat e t im e dur ing inst allat ion, ensuring t hat t he cust om t asks specific t o our applicat ion are perform ed, set t ing up t he applicat ion as t he user has specified in our user int erface screens. We have built in error handling in Try..Cat ch blocks t o ensure a sm oot h process t hat degrades gracefully when an error condit ion is encount ered. Visual St udio .NET has m ade great st rides in providing a powerful environm ent in which t o creat e Windows I nst aller packages for our .NET e- Business applicat ions.

Cr e a t in g W e b Clu st e r s w it h Applica t ion Ce n t e r 2 0 0 0 As an illust rat ion of creat ing scalable and available Web sit es, we're going t o deploy t he applicat ion using Visual St udio .NET in t andem wit h a new product for ent erprise Web clust er m anagem ent on t he Windows server plat form , Microsoft Applicat ion Cent er 2000 ( AC2K) .

Abou t Applica t ion Ce n t e r 2 0 0 0 Applicat ion Cent er is anot her one of t he .NET Ent erprise Servers, and as such is posit ioned as t he t echnology of choice for creat ing load- balanced, highly available clust ers for I nt ernet and int ranet ent erprise applicat ions ( see Figure 16.10) .

Figu r e 1 6 .1 0 Applicat ion Cent er 2000 in act ion.

AC2K m anages cont ent on t he m achines t hat m ake up a Web clust er. A m achine is designat ed as t he cont roller for t he clust er, and is considered t o have t he m ast er copy of all relevant Web cont ent for t he sit e. Changes t o t he clust er cont roller cont ent can be aut om at ically propagat ed t o ot her m em bers of t he clust er. AC2K provides several powerful perform ance m onit or ing capabilit ies, in t he form of healt h m onit ors. These m onit ors can gat her perform ance dat a such as website perform ance st at ist ics, or t hey can gat her syst em healt h dat a such as m em ory ut ilizat ion and t he st at us of forwarded request s. Som e of t he different kinds of healt h m onit ors available are depict ed in Figure 16.11. AC2K also m anages increm ent al deploym ent . I f a change is m ade t o an applicat ion t hat requires I I S t o shut down on all t he m achines, AC2K will aut om at ically handle t he draining of connect ions, t he applicat ion updat e, and t he rest art ing of I I S on each m achine in t he clust er wit hout int ervent ion from an adm inist rat or. Alt hough AC2K has been dubbed a .NET Ent erprise Server, it st ill has plent y of support m echanism s for COM+ . Specialized clust ers can be creat ed for COM+ applicat ions using Com ponent Load Balancing, and t here is even support for clust ers which rout e COM+ request s t o COM+ applicat ion clust ers. Replacem ent of COM+ com ponent s requires shut t ing down and rest art ing COM+ applicat ions, so cont ent does not propagat e aut om at ically t hrough t hese clust ers; t he changes m ust be init iat ed m anually. Since COM+ is not part of t he .NET Fram ework, we will not discuss t hese clust ers furt her, lim it ing ourselves t o a discussion of Web clust ers under AC2K.

Figu r e 1 6 .1 1 Many healt h m onit ors are available in Applicat ion Cent er 2000 t o m onit or and m easure perform ance of a Web clust er.

Pr e pa r in g a W e b Clu st e r for Use w it h .N ET a n d AC2 K Ca ut ion: As of t h e t im e of t h is w r it in g, Applica t ion Ce nt e r 2 0 0 0 in st a lls poor ly on W in dow s 2 0 0 0 m a ch in e s a lr e a dy r u n n in g Se r v ice Pa ck 2 or la t e r . I n fa ct , it m a y n ot e v e n w or k a t a ll if in st a lle d on su ch m a ch in e s. Th e r e is n o r e a l solu t ion ot h e r t ha n t o t h in k a h e a d a n d inst a ll Applica t ion Ce nt e r 2 0 0 0 be for e a pply in g SP2 . Ke e p t h is in m in d if you pla n t o in st a ll Applica t ion Ce n t e r 2 0 0 0 a n d Visu a l St u dio .N ET on t h e sa m e m a ch in e [ m d] Visu a l St u dio .N ET r e qu ir e s Se r v ice Pa ck 2 . I t is sa fe t o in st a ll Se r vice Pa ck 2 a ft e r in st a llin g Applica t ion Ce nt e r 2 0 0 0 , a s lon g a s t he m a ch in e is n ot pa r t of a clu st e r . N ot e : I f w e pla n t o u se W in dow s N e t w or k Loa d Ba la n cin g a s ou r loa d dist r ibu t ion m e ch a n ism , e a ch com pu t e r in t h e clu st e r m u st h a ve t w o n e t w or k ca r ds. On e n e t w or k ca r d fr om e a ch com pu t e r w ill h ook t o a pr iva t e " he a r t be a t " ne t w or k t h a t cir cu la t e s Applica t ion Ce nt e r - spe cific m a n a ge m e n t t r a ffic; t he ot he r n e t w or k ca r d is t h e e x t e r n a l in t e r fa ce . Un de r W in dow s N e t w or k Loa d Ba la n cin g, t h e m a ch in e t ha t w ill be t h e clu st e r con t r olle r ne e ds t o h a ve a st a t ic I P a ddr e ss for it s e x t e r n a l n e t w or k ca r d. N e t w or k Loa d Ba la n cin g w ill ide n t ify t h e clu st e r a ccor din g t o t h is I P a ddr e ss. Tip: Clu st e r m e m be r s n e e d on ly be r u n n in g W in dow s 2 0 0 0 Se r ve r , a s AC2 K w ill a u t om a t ica lly a dd W in dow s N e t w or k Loa d Ba la n cin g on a n y m a ch in e s on w h ich it is in st a lle d. N e t w or k Loa d Ba la n cin g com e s a u t om a t ica lly a s pa r t of W indow s 2 0 0 0 Adv a n ce d Se r ve r .

To add m em bers t o a clust er, we m ust first creat e t he clust er. This can be done by opening Applicat ion Cent er and choosing t o connect t o a server. We will see a prom pt t hat asks us whet her t o creat e a new clust er or j oin an exist ing clust er. Use t he first opt ion t o creat e a clust er and m ake t he com put er t o which we are connect ing

t he cont roller for t hat clust er; use t he second opt ion, as shown in Figure 16.12, t o j oin m achines t o t he clust er we j ust creat ed. We will be asked t o verify som e inform at ion about t he net work card( s) and net work configurat ion in t he m achine t o be j oined.

Figu r e 1 6 .1 2 Adding a m em ber t o a clust er. Tip:Be pa t ie n t w h e n pr e pa r in g a W e b clu st e r . I t t a k e s t im e t o a dd com pu t e r s t o clu st e r s. A lot of w or k goe s on " u n de r t he h ood" t o su ppor t a ddin g a n e w m e m be r t o a clu st e r , in clu din g ch a n gin g N e t w or k Loa d Ba la n cin g se t t in gs a n d ot h e r n e t w or k pr ope r t ie s, a s w e ll a s be gin n in g syn ch r on iza t ion of cont e n t t o n e w clu st e r m e m be r s.

Going forward, we will work wit h t he AC2K Web clust er depict ed in Figure 16.13, which consist s of t wo m achines. TI XWEBCC is t he clust er cont roller, while TI XWEB2 is t he ot her clust er m em ber.

Figu r e 1 6 .1 3 The gasTI X clust er in Applicat ion Cent er.

D e ployin g .N ET Applica t ion s on Applica t ion Ce n t e r 2 0 0 0 Clu st e r s Applicat ion Cent er 2000 aut om at ically propagat es changes m ade t o Web applicat ions on t he clust er cont roller t o t he ot her m em bers of t he clust er. I n t he t radit ional Windows DNA set t ing, any discussion of Applicat ion Cent er 2000 would by necessit y involve m ost of t he feat ures and at t ribut es of Applicat ion Cent er 2000 applicat ions, which are sim ply collect ions of I I S virt ual direct ories and associat ed local resources which are t o be replicat ed across different m achines. This is because t he t rappings of COM+ can involve a lot of regist ry set t ings and quit e a few ext ernal resources such as DSNs. To m ake m at t ers worse, COM+ packages do not aut om at ically replicat e, as we briefly discussed earlier, and we would have t o perform a m anual synchronizat ion in order t o propagat e COM+ applicat ion changes. The .NET Fram ework changes all of t his. Because t here is no locking of shared DLLs in use, no processes need t o shut down in order t o inst all changes t o .NET DLLs. I n addit ion, since we are t reat ing all assem blies as privat e, we st ore our business and dat a obj ect s direct ly in t he Web applicat ion folders as sim ple cont ent . Applicat ion Cent er 2000 will aut om at ically propagat e changes in our applicat ion. Ca ut ion: t t h is t im e , it is r isk y t o r e plica t e D LLs st or e d in t h e globa l a sse m bly ca ch e ( GAC) . Sh a r e d a sse m blie s a r e st or e d in a ve r y spe cific dir e ct or y st r u ct ur e in side t h e W I N N T\ Asse m bly\ folde r . W h ile copyin g t h e GAC dir e ct or y t r e e be t w e e n m a ch in e s a s a " File Syst e m Pa t h " r e sour ce in t h e cor r e spon din g Applica t ion Ce nt e r 2 0 0 0 a pplica t ion is possible , t h e M icr osoft - r e com m e n de d w a y is t o u se a W in dow s I n st a lle r t o m a n ipu la t e t h e GAC. I n st a lle r pr oj e ct s spe cifica lly de signe d for t h e GAC ( su ch a s t h ose cr e a t e d by Visu a l St u dio .N ET) h a ve spe cia l fe a t u r e s, su ch a s a sse m bly r e fe r e n ce cou n t in g, w h ich m a k e w or k in g w it h t h e GAC sa fe .

Applicat ion ent er 2000 applicat ions are st ill very useful for propagat ing any regist ry keys ut ilized by t he applicat ion, or any DSNs needed by each m achine t hat exist on t he clust er cont roller. I n our case, we have com piled t he proper dat abase connect ion st rings int o our applicat ion files, so we will not need t o use t he local resource propagat ion feat ures of Applicat ion Cent er 2000. I f we w ere using t hese feat ures, it em s such as regist ry keys and DSNs would be bundled w it h an Applicat ion Cent er applicat ion as local resources, and would also be propagat ed t o ot her clust er m em bers. I n order t o add resources, sim ply view t he " Applicat ions" it em in t he t ree view for t he Web clust er, select t he resource t ype t o add, and click t he " Add…" but t on t o select an it em t o add. This process is shown in Figure 16.14.

Figu r e 1 6 .1 4 Adding resources t o an Applicat ion Cent er 2000 applicat ion.

We are now ready t o deploy cont ent t o t he clust er cont roller. I n our case, it is as sim ple as running t he inst aller we creat ed for gasTI X on t he clust er cont roller. Once t he applicat ion is inst alled on t he cont roller, if aut om at ic synchronizat ion is enabled, t he applicat ion will be replicat ed t o t he ot her clust er m em bers at t he next synchronizat ion int erval.

Fr om H e r e I n t his chapt er, we have got t en a t ast e of t he powerful opt ions available t o us for .NET dist ribut ed applicat ion deploym ent . While " XCopy Deploym ent " is a viable opt ion ( which som e people sw ear by) , t he abilit y t o creat e version- checked, easily m aint ained inst allat ion packages is wort h t he ext ra effort . Wit h pract ice, deploym ent proj ect s can becom e as st raight forward as sim ple deploym ent via copying files, but wit h m any m ore benefit s. As t his is a book on .NET e- Business archit ect ure, we have focused prim arily on Web deploym ent s and considerat ions relat ing t o Web sit e scalabilit y and availabilit y. As we have seen, .NET has a rich set of feat ures for t radit ional Windows client deploym ent as well. These feat ures behave in a sim ilar m anner t o t hose for Web deploym ent s, and should be fam iliar t o users of inst aller program s like I nst allShield and t he Package and Deploym ent Wizard in Visual Basic 6.0. As .NET is a new t echnology, we will no doubt cont inue t o see im provem ent s t o t he plat form t hat will provide furt her robust ness and ease in deploying next - generat ion dist ribut ed ent erprise applicat ions. We hope t hat t his chapt er, and indeed t his ent ire book, has provided you wit h a set of pract ical, usable skills t hat will help you m ast er t he new, ever- changing world of .NET e- Business. From all of us t o all of you, happy program m ing! Ret urn oft en t o w ww.gast ix.com and www.gasullivan.com for product updat es and furt her learning t opics!

Appe n dix ga sTI X Re qu ir e m e n t s The high- level requirem ent s for t he gasTI X applicat ion are described in t his sect ion. I t focuses on user, financial, back office, and adm inist rat ive requirem ent s of t he syst em . Funct ionalit y is also divided int o t he requirem ent s t hat are im plem ent ed in t his version of gasTI X, and t hose t hat have been deferred t o a fut ure version.

I m ple m e n t e d Fu n ct ion a lit y This sect ion describes t he funct ional requirem ent s t hat are im plem ent ed in t he current version of t he gasTI X .NET sam ple applicat ion. The requirem ent s are grouped by m aj or funct ional area: User, Financial, Back Office, and Adm inist rat ion. I . Use r Fun ct iona l Re quir e m e nt s Th e syst e m w ill pr ovide a se t of v e n u e s for u se r s t o pick fr om , a n d w ill t r a ck se a t in g for t h ose ve n ue s.

2.

Th e sy st e m w ill displa y de m ogr a phic infor m a t ion for e a ch ve nue , such a s a ddr e ss, phone n u m be r s, dir e ct ions, pa r k in g in for m a t ion , a n d so on . Th is w ill a llow u se r s t o m a k e in for m e d ch oice s w h e n pu r cha sin g t ick e t s.

3.

Th e syst e m w ill con t a in a gr a ph ica l se a t in g ch a r t for ve n u e s ( w h e n a gr a ph ics file is m a de a va ila ble by t he v e n ue ) w h ich w ill h e lp u se r s m a k e bu yin g de cision s.

4.

Th e syst e m w ill se ll t ick e t s for a n um be r of e ve n t s a n d w ill a llow u se r s t o qu e r y for a va ila ble se a t in g st ill r e m a in in g a t t h ose e ve n t s.

5.

Th e sy st e m w ill a llow u se r s t o se a r ch for spe cific e ve n t s for a n a r t ist or t e a m , e ve n t s w it h in a ce r t a in ca t e gor y ( spor t s, m u sic, w it h su bca t e gor ie s) , e v e n t s a t a spe cific ve n u e , a n d e v e nt s in a spe cific st a t e .

6.

Th e syst e m w ill a llow u se r s t o spe cify t he n u m be r of t ick e t s de sir e d for a n e v e nt , w it h a m a x im u m of e igh t t ick e t s pe r pu r ch a se .

7.

Th e sy st e m w ill a llow u se r s t o loca t e se a t s in a spe cific ve nue se ct ion, or qu e r y for t h e be st se a t s st ill a va ila ble for a n e ve nt .

8.

Th e syst e m w ill a llow u se r s t o pur ch a se t ick e t s u sin g a cr e dit ca r d t h r ou gh t h e I nt e r n e t .

9.

Th e syst e m w ill e - m a il a pu r cha se con fir m a t ion t o u se r s.

TE

AM FL Y

1.

1 0 . By t r a ck in g a n u m be r of e v e nt s for a spe cific a r t ist or t e a m , t h e syst e m w ill su ppor t a t our sch e du le for a r t ist s/ t e a m s. 1 1 . Th e syst e m w ill offe r t h e u se r a va r ie t y of t ick e t sh ippin g m e t h ods ( n e x t da y, t hr e e bu sin e ss da ys, st a n da r d post a l m a il, e t c.) . 1 2 . Th e syst e m w ill su ppor t a n a ddr e ss va lida t ion fe a t u r e t ha t w ill qu e r y a da t a ba se of va lid a ddr e sse s du r ing a ddr e ss e nt r y, a n d pr om pt u se r s if a n a ddr e ss ca n n ot be ve r ifie d. 1 3 . Th e syst e m w ill su ppor t t h e displa y of ba n ne r a dv e r t ise m e n t s.

Team-Fly®

I I . Fina ncia l Funct iona l Re quir e m e n t s 1.

Th e sy st e m w ill pr ovide t r a n sa ct iona l su ppor t for pu r ch a sin g, e n su r ing t h a t e it he r a ll st e ps in t h e pr oce ss a r e com ple t e d, or n on e a r e com ple t e d.

2.

Th e syst e m w ill e n su r e t h e se cur it y of se n sit ive fin a n cia l da t a su ch a s cr e dit ca r d in for m a t ion du r in g t he pu r ch a se pr oce ss.

I I I . Ba ck Office Fun ct iona l Re quir e m e nt s 1.

Th e sy st e m w ill be de sign e d t o su ppor t a la r ge n u m be r of u se r s.

2.

Th e sy st e m w ill pr ov ide a n XM L W e b se r v ice t ha t w ill a llow ot h e r w e b sit e s t o qu e r y for pr icin g, a v a ila bilit y , a n d e ve nt in for m a t ion .

3.

Th e sy st e m w ill pr ov ide a n XM L W e b se r v ice t ha t w ill a llow ot h e r w e b sit e s t o m a k e t ick e t pu r ch a se s.

4.

Th e sy st e m w ill in t e gr a t e w it h a Tr a n sa ct ion Pr oce ssin g Au t h or it y ( TPA) bu sin e ss pa r t n e r . This pa r t n e r w ill h a n dle a ll t ick e t pr int ing a n d sh ippin g fu lfillm e n t . Com m u n ica t ion s be t w e e n t h e ga sTI X syst e m a n d t h e TPA sy st e m w ill occu r in a n a syn chr on ou s, loose ly cou ple d m a n n e r .

Fu t u r e Fu n ct ion a lit y This sect ion describes t he funct ional requirem ent s t hat are deferred t o a fut ure version of t he gasTI X .NET sam ple applicat ion. While t hese requirem ent s have not been im plem ent ed at t his t im e, careful considerat ion was given t o t heir fut ure im plem ent at ion during design. I . Use r Fun ct iona l Re quir e m e nt s ( Fu t u r e ) 1.

Th e sy st e m w ill su ppor t on lin e h e lp for com m on ly a cce sse d fe a t u r e s.

2.

Th e sy st e m w ill pr ov ide u se r s w it h a fe a t ur e t o t r a ck t he st a t u s of or de r s.

3.

Th e u se r a ccou n t a r e a w ill t r a ck " pu r ch a se poin t s" w h ich ca n be a pplie d t o fut u r e or de r s.

4.

Th e syst e m w ill pr ovide u se r s w it h t h e ca pa bilit y t o sign u p for e m a il n ot ifica t ions of spe cific pr om ot ion s a n d e ve nt s.

5.

Th e sy st e m w ill pr ov ide a " for got t e n pa ssw or d" fu n ct iona lit y t o r e gist e r e d u se r s, a llow in g t he m t o r e ce ive t h e ir pa ssw or d via e - m a il.

6.

Th e sy st e m w ill su ppor t on lin e u se r r e gist r a t ion of de m ogr a ph ic in form a t ion a n d opt iona lly, cr e dit ca r d in for m a t ion. Re gist r a t ion w ill a lso a llow t h e u se r t o e n t e r a r t ist / t e a m / e v e nt pr e fe r e n ce s, a n d a cu st om ize d h om e pa ge w ill t h e n displa y spe cific e ve n t s ba se d on t h e se pr e fe r e n ce s. Re gist r a t ion w ill be a ccom plish e d t h r ough in t e gr a t ion w it h M icr osoft Pa sspor t .N ET, e na bling t h e sit e t o be gin offe r in g H a ilSt or m se r vice s.

7.

Th e syst e m w ill pr ovide u se r s w it h a pa ssw or d- pr ot e ct e d login scr e e n t o e nt e r t h e pe r son a l u se r a ccou nt a r e a w h e r e t h e y ca n m odify pr e fe r e n ce s. Login w ill be a ccom plish e d t hr ou gh in t e gr a t ion w it h M icr osoft Pa sspor t .N ET.

8.

I f a u se r ha s logge d in t o a pe r sona l a ccou nt a n d is m a k in g a pu r cha se , billin g in for m a t ion is r e a d fr om t h e u se r ''s pe r son a l a ccou nt a n d is displa ye d. Th e u se r m a y opt ion a lly cha n ge t h e billin g in for m a t ion for t h e pu r ch a se . Billin g in for m a t ion w ill be r e t r ie ve d via M icr osoft Pa sspor t .N ET Ex pr e ss Pu r cha se .

9.

Th e syst e m w ill a llow u se r s t o pr in t pu r ch a se d t ick e t s on lin e ( ba r code sca n n e r s w ill be r e qu ir e d a t ve n ue s t o r e a d t he se t ick e t s) .

1 0 . Th e sy st e m w ill offe r a n e x t e n de d pr odu ct lin e ( post e r s, T- sh ir t s, a n d ot h e r m e r ch a n dise ) r e la t e d t o e v e n t s fe a t u r e d on t h e sit e . 1 1 . Th e sy st e m w ill su ppor t in t e r na t iona liza t ion fe a t u r e s ( la ngu a ge a n d m on e t a r y con v e r sion s, da t e for m a t t in g, e t c.) , offe r in g se r vice t o u se r s in ove r se a s m a r k e t s.

I I . Fina ncia l Funct iona l Re quir e m e n t s ( Fut ur e ) The syst em w ill int egr at e wit h an ext ernal account ing syst em 's general ledger. I I I . Ba ck Office Fun ct iona l Re quir e m e nt s ( Fut u r e ) 1.

Th e sy st e m w ill su ppor t m obile de v ice con n e ct ivit y. W ir e le ss de vice s su ch a s digit a l t e le ph one s, PD As, a n d ot h e r W AP- com plia n t de vice s w ill a cce ss t he sit e ove r t he w e b. Th e se u se r s w ill be pr e se n t e d w it h pa ge s t a ilor e d for t h e sm a ll scr e e n .

2.

Th e syst e m w ill su ppor t XM L- ba se d e x cha n ge w it h ve n ue s for se a t in g in for m a t ion for diffe r e nt ve n u e la y ou t s.

3.

Th e sy st e m w ill qu e r y sh ippin g w e b sit e s ( Fe dEx , UPS, a nd so on ) t o de t e r m in e sh ippin g pr icin g for t ick e t s.

I V . Adm inist r a t ion Funct iona l Re quir e m e n t s ( Fut u r e ) 1.

Th e sy st e m w ill pr ov ide a login scr e e n for a dm in ist r a t or s of t he t ick e t in g a pplica t ion . Th is login w ill be a ccom plish e d t hr ou gh in t e gr a t ion w it h M icr osoft Pa sspor t .N ET.

2.

Th e a dm in ist r a t ion a r e a w ill a llow a ddin g, e dit in g, or de le t in g a ve n ue . Th e ve n ue m a in t e na n ce a r e a w ill a llow t h e a dm in t o e n t e r se a t in g de t a il for diffe r e n t ve n u e configu r a t ion s. This a r e a w ill a lso a llow t h e a dm in t o e n t e r t h e file n a m e of t h e gr a ph ica l se a t in g ch a r t for e a ch con figu r a t ion .

3.

Th e a dm in ist r a t or w ill n ot be a llow e d t o de le t e a ve n ue w it h e ve nt s w it h t ick e t s sold. On ly ve n u e s w it h no t ick e t s sold ( or a ve n ue w h e r e a ll t ick e t s for a ll e ve nt s ha ve be e n r e fun de d) ca n be de le t e d.

4.

Th e a dm in ist r a t ion a r e a w ill a llow a n a dm in t o a dd, e dit , or de le t e a n a r t ist / t e a m . Th e a r t ist / t e a m w ill con t a in a na m e , w e b sit e , pict ur e , a n d ca t e gor y. Th e a r t ist / t e a m ca n be de sign a t e d a s a fe a t u r e d pe r for m e r . W it h t h is de sign a t ion e n a ble d, a n a r t ist / t e a m w ill be fe a t u r e d on t h e ga sTI X h om e pa ge u n de r t he a ppr opr ia t e ca t e gor y .

5.

Th e a dm in ist r a t or w ill n ot be a llow e d t o de le t e a n a r t ist / t e a m w it h t ick e t s sold. On ly a r t ist s/ t e a m s w it h no t ick e t s sold ( or a r t ist s/ t e a m s w h e r e a ll t ick e t s for a ll e v e nt s h a ve be e n r e fu n de d) ca n be de le t e d.

6.

Th e a dm in ist r a t ion a r e a w ill a llow a n a dm in t o a dd, e dit , or de le t e a n e ve nt , j oin ing a n a r t ist / t e a m w it h a ve n ue for a spe cific da t e / t im e . D a t a su ch a s spe cia l in st r uct ions ( for e x a m ple , no ca m e r a s or r e cor de r s) , t ick e t lim it , da t e / t im e t ick e t s go on sa le , a n d so on ca n be e nt e r e d for a n e ve n t .

7.

Th e a dm in ist r a t or w ill n ot be a llow e d t o de le t e a n e ve nt w it h t ick e t s sold. On ly e ve n t s w it h no t ick e t s sold ( or a n e ve n t w h e r e a ll t ick e t s h a ve be e n r e fu n de d) ca n be de le t e d.

8.

Th e a dm in ist r a t or w ill be a ble t o pr oce ss a r e fun d for a ll t ick e t s for a n e ve nt in ca se of r a in out , ca n ce lla t ion , low sa le s, e t c.

9.

Th e sy st e m w ill a llow a n a dm in ist r a t or t o m a r k spe cific se a t s for a n e v e nt a s sold pr ior t o t he e ve nt goin g on sa le ( pr om ot ion a l give a w a ys, e t c.) .

1 0 . Th e sy st e m w ill pr ov ide st a n da r d t r a n sa ct iona l r e por t s list in g sa le s a nd ot h e r r e le v a nt da t a . 1 1 . Th e sy st e m w ill a llow e x e cut iv e s t o pr odu ce a d- h oc OLAP r e por t s t o de t e r m in e pu rch a sin g t r e n ds a n d pa t t e r n s.

ga sTI X Use Ca se s The gasTI X design t eam used t he Unified Modeling Language ( UML) during t he design phase. One of t he m ost im port ant art ifact s generat ed during UML m odeling is t he use case diagram . This diagram shows t he ent ire syst em scope, and is support ed by use case docum ent s t hat describe t he syst em scope in m ore det ail. The following use cases were const ruct ed at a high level, and cont ain only sim ple ext ension condit ions. I n a real- wor ld client engagem ent , m uch m ore det ail would be provided. The act or for each use case ( act or in t he UML sense of t he word, not a gasTI X perform er) is t he cust om er browsing t he applicat ion, searching for event s and purchasing t icket s.

Use Ca se 1 - - Se le ct Eve n t by Ca t e gor y Ex t e n ds: None ( A use case t hat ext ends anot her use case builds upon t he funct ionalit y of t he use case it ext ends) . Assum pt ion: The act or can cancel t his use case at any t im e. Pu r pose : Select an event based on t he cat egory of an art ist or t eam . Ou t pu t s: Det ailed inform at ion for a specific event . The act or picks a cat egory and a list of art ist or t eam nam es having event s fit t ing t he cat egory is displayed. The act or t hen picks a specific art ist or t eam nam e, and a list of event s for t hat art ist or t eam wit h sum m ar y inform at ion is displayed. The act or next select s a specific event and t he syst em displays det ailed inform at ion for t hat event .

Use Ca se 2 - - Se le ct Eve n t by Ve n u e Ex t e n ds: None Assum pt ion: The act or can cancel t his use case at any t im e. Pu r pose : Select an event based on t he venue in which an event is being held. Ou t pu t s: Det ailed inform at ion for a specific event . The act or decides whet her t o search for t he venue by nam e or by st at e and proceeds according t o one of t he follow ing t wo pat hs. a . Ve nue Se a r ch The act or input s t he nam e of a venue and issues t he search for a venue com m and. The syst em det erm ines t hat t he num ber of charact ers input is great er t han or equal t o one charact er in lengt h. I f not , t he user is prom pt ed t o ent er a venue nam e and m ust rest art t he process. Upon successful venue nam e ent ry, t he syst em searches for t he venue input by t he user. A list of venues is displayed t o t he act or if m at ches are found. A " no venues found" m essage is displayed t o t he act or if no m at ches are found. The act or picks a venue from t he list of m at ches and a list of event s at t hat venue wit h sum m ary inform at ion is displayed. The act or t hen select s a specific event and t he syst em displays det ailed inform at ion for t hat event .

b. Se le ct Ve n ue by St a t e A list of st at es and a graphical U.S. st at e m ap is displayed t o t he act or, who t hen picks a st at e. A list of cit ies wit h a covered venue is displayed. I f no covered venue cit ies are found for t he select ed st at e, a " no venues found" m essage is displayed. The act or next select s a cit y, and a list of venues is displayed. The act or picks a venue and a list of event s at t hat venue wit h sum m ary inform at ion is displayed. The act or t hen select s a specific event and t he syst em displays det ailed inform at ion for t hat event .

Use Ca se 3 - - Se le ct Eve n t by St a t e Ex t e n ds: None Assum pt ion: The act or can cancel t his use case at any t im e. Pu r pose : To select an event based on t he st at e in which t he event is t aking place. Ou t pu t s: Det ailed inform at ion for a specific event . A list of st at es and a graphical U.S. st at e m ap is displayed t o t he act or. The act or select s a st at e, and a list of art ist or t eam nam es wit h upcom ing event s in t he st at e is displayed, ordered by cat egory. The act or select s an art ist or t eam nam e and t he syst em displays a list of event s wit h lim it ed event sum m ary inform at ion for t he select ed nam e. The act or t hen select s a specific event and t he syst em displays det ailed inform at ion for t hat event .

Use Ca se 4 - - Se le ct Eve n t by Ar t ist / Te a m N a m e Ex t e n ds: None Assum pt ion: The act or can cancel t his use case at any t im e. Pu r pose : Select an event based on t he nam e of t he art ist / t eam . Ou t pu t s: Det ailed inform at ion for a specific event . The act or input s t he nam e of an art ist or t eam nam e and issues t he search for art ist / t eam nam e com m and. The syst em det erm ines t hat t he num ber of charact ers input is great er t han or equal t o one charact er in lengt h. I f not , t he user is prom pt ed t o ent er an art ist or t eam nam e and m ust rest art t he process. Upon successful art ist / t eam nam e ent ry, t he syst em searches for t he nam e input by t he user. A list of nam es is displayed t o t he act or if m at ches are found. A " no art ist s/ t eam s found" m essage is displayed t o t he act or if no m at ches are found. The act or picks an art ist / t eam nam e from t he list , and a list of event s for t hat nam e wit h sum m ary inform at ion is displayed. The act or t hen select s a specific event and t he syst em displays det ailed inform at ion for t hat event .

Use Ca se 5 - - Pr icin g/ Ava ila bilit y Che ck Ex t e n ds: Select Event by Cat egory, Select Event by Venue, Select Event by St at e, Select Event by Art ist / Team Nam e

Assum pt ions: The act or can cancel t his use case at any t im e. A specific event has been select ed in order t o run t his use case. Pu r pose : Det erm ine which seat s are st ill available for a specific event and t he pricing for t hose seat s. Ou t pu t s: A list of available seat s and prices for a specific event . The act or ent ers t he num ber of seat s desired for t he event ( up t o eight ) , and opt ionally t he price range willing t o pay. The syst em issues t he check pricing/ availabilit y search funct ion, and a list of available seat ing opt ions m eet ing t he crit eria is displayed. Det ailed pricing inform at ion, along wit h service charges and any applicable t axes, is displayed t o t he user.

Use Ca se 6 - - Vie w Se a t in g Ch a r t Ex t e n ds: Select Event by Cat egory, Select Event by Venue, Select Event by St at e, Select Event by Art ist / Team Nam e Assum pt ion: The act or can cancel t his use case at any t im e. A venue m ust be select ed in order t o proceed wit h t his use case. Pu r pose : To provide t he act or wit h a graphical view of t he seat ing chart for a specific venue. Ou t pu t s: A graphical display for t he select ed venue. The act or picks t he view seat ing chart opt ion for a specific venue. I f a seat ing chart is available for t he venue, it is displayed t o t he user. I f no chart is available, a " no seat ing chart available" m essage is displayed. The act or is able t o view t he different seat ing configurat ions for t he venue, if available.

Use Ca se 7 - - Pu r ch a se Tick e t s Ex t e n ds: Pricing/ Availabilit y Check Assum pt ions: The act or cannot cancel t his use case once t he final purchase has been subm it t ed. A specific event has been select ed, and a pricing/ availabilit y check has been run for t his event . Pu r pose : Enable t he act or t o purchase a specific num ber of t icket s for a specific price for a specific event . Ou t pu t s: A confirm at ion of purchase for t he select ed event , pricing, and seat ing. The act or ent ers t he shipping opt ion, billing address, and credit card inform at ion. I f a shipping opt ion is not picked, if required billing address fields are not com plet ed, or if credit card inform at ion is not com plet ed, t he syst em displays an error m essage t o t he user and provides an opport unit y t o fill in t he required fields. Once t he syst em det erm ines all inform at ion is ent ered correct ly, t he syst em calls t he purchase t icket funct ion. The credit card inform at ion is processed. The syst em rem oves t he seat s purchased from t he available list for t he event . The syst em displays a " purchase com plet ed" m essage t o t he user. I f any errors are encount ered during t he purchasing process, t he purchase is rolled back. Any error m essages are displayed t o t he user, who is given t he opport unit y t o resubm it t he purchase if appropriat e.

ga sTI X Sit e M a p The following sit e m ap provides a gr aphical view of t he general web page navigat ion pat h of t he gasTI X applicat ion.

Figu r e AP.1

ga sTI X Obj e ct M ode ls The follow ing obj ect m odels present a graphical view of t he gasTI X m iddle- t ier com ponent s. These diagram s were generat ed in Visual St udio .NET using t he Visio UML Reverse Engineer feat ure, found on t he Proj ect m enu. VS.NET aut om at ically generat es a Visio UML file when t his feat ure is select ed on a proj ect . This can be done on t he solut ion level as well as at t he individual proj ect and class level.

Addr e ssVa lida t ion

Figu r e AP.2

Addr e ssVa lida t ion Svc

Figu r e AP.3

Bu sin e ss

Figu r e AP.4

TE

AM FL Y

Bu sin e ss Fa ca de

Figu r e AP.5

Team-Fly®

Bu sin e ss Fa ça de , con t in u e d

Figu r e AP.6

Data

Figu r e AP.7

D a t a , con t in u e d

Figu r e AP.8

D a t a , con t in u e d

Figu r e AP.9

D a t a , con t in u e d

Figu r e AP.1 0

D a t a Acce ss

Figu r e AP.1 1

ga sTI XSe t u p

Figu r e AP.1 2

ga sTI XSe t u p, con t in u e d

Figu r e AP.1 3

ga sTI X D a t a M ode l The following figures depict t he gasTI X dat a m odel, generat ed wit h t he Erwin ER St udio design t ool.

Figu r e AP.1 4

AM FL Y TE Figu r e AP.1 5

Team-Fly®

Ta ble AP.1 Th e ga sTI X Ta ble s Su ppor t t h e Re qu ir e d Applica t ion Fu n ct ion a lit y

Ta ble N a m e

D e scr ipt ion

Ca t e gor y

A gr ou pin g of pa r t icipa n t s by t y pe . Ex a m ple s in clu de M usic a n d Spor t s.

Con figur a t ion

A spe cific a r r a n ge m e n t of se ct ion s, r ow s, a n d se a t s w it h in a ve n u e .

Con figur a t ion _ Row

Row s w it h in a se ct ion t ha t a r e a va ila ble w it h in a give n con figu r a t ion .

Con figur a t ion _ Se a t

Se a t s w it h in a r ow t h a t a r e a va ila ble w it h in a give n con figu r a t ion .

Con figur a t ion _ Se ct ion

Se ct ion s w it h in a ve n ue t ha t a r e use d w it h in a give n con figu r a t ion .

Cr e dit _ Ca r d_ Type

Ty pe of cr e dit ca r d u se d in t r a n sa ct ion .

Cou n t r y

Cou n t r y in w h ich a r e gion is loca t e d. Use d for cu st om e r sh ippin g a n d billin g a ddr e sse s a s w e ll a s v e n ue loca t ion s.

Cu st om e r

I n dividu a l pu r ch a sin g t ick e t s.

Cu st om e r _ Pr e fe r e n ce

Fa vor it e e ve n t ca t e gor ie s for a give n u se r . Ex a m ple s in clu de M u sic, Spor t s, e t c.

Eve n t

A spe cific occa sion a t a ve n u e for w h ich for w h ich t ick e t s a r e a va ila ble . Ex a m ple s in clu de a foot ba ll ga m e or a con ce r t . An e v e nt ca n ha ve m or e t ha n on e pa r t icipa nt a n d is a ssign e d t o a spe cific con figu r a t ion for t h e v e n ue a t w h ich t h e e v e nt occu r s. Ev e nt s occu r a t a spe cific da t e a nd t im e . Tick e t s ca n n ot go on sa le be for e a spe cific da t e .

Eve n t _ Pa r t icipa n t

A pa r t icipa nt t ha t is sch e du le t o a ppe a r a t a give n e ve nt .

Eve n t _ Se ct ion

Assign m e n t of a con figu r a t ion se ct ion t o a n e ve n t . Use d t o se t t ick e t pr ice s w h ich a r e t h e sa m e for a n e n t ir e se ct ion .

Eve n t _ Sa le _ Se a t

Ava ila ble se a t s for a give n e ve n t .

Map

Re pr e se n t s a n im a ge fr om w h ich t h e u se r ca n gr a ph ica l ch oose a r e gion .

Pa r t icipa nt

An in div idu a l or gr ou p for w h ich t ick e t s ca n be sold for a ssocia t e d e ve nt s.

Re gion

Re pr e se n t s a st a t e or pr ov in ce .

Re gion _ M a p

M a p coor din a t e s of a pa r t icu la r r e gion .

Sa le

Th e pu r ch a se of on e or m or e se a t s by a cu st om e r . I n clu de s t he in for m a t ion a bout t he cu st om e r .

Sh ippin g_ M e t h od

Th e m e t h od by w h ich t ick e t s w ill be sh ippe d t o cu st om e r . ( UPS, Fe dEx , e t c.)

Spon sor

A gr ou p t h a t su bsidize s a pa r t icu la r t ou r .

Su bca t e gor y

A gr ou pin g of pa r t icipa n t s w it h in a give n ca t e gor y. Ex a m ple s w it h t h e ca t e gor y of M u sic m igh t in clu de Cou n t r y, Rock , e t c.

Ta x _ Ra t e

Th e pe r ce nt a ge of t a x a pplie d t o t he sa le by r e gion a n d e ffe ct ive da t e .

Tou r

A se r ie s of com m on e ve n t s.

Ve n u e

A loca t ion a t w h ich on e or m or e e ve n t s ca n be h ost e d.

Ve n u e _ Row _ Se a t

A loca t ion for a n in dividu a l t o sit w it h in a r ow .

Ve n u e _ Se ct ion

A gr ou p of r e la t e d r ow s a va ila ble w it h in a ve n ue . Th is t a ble ( a n d r e la t e d t a ble s Ve n u e _ Se ct ion _ Row a n d Ve n u e _ Row _ Se a t ) colle ct iv e ly list a ll a va ila ble se a t in g opt ion s w it h in a ve n u e .

Ve n u e _ Se ct ion _ Row

A gr ou p of a dj a ce n t se a t s w it h in a se ct ion .