SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Featuring nearly fifty rules for best practice, EFFECTIVE VISUAL BASIC gives the working programmer concrete guidelines for designing better systems and writing better code. Whether the focus is COM, databases, or distributed systems using MTS and COM+, this book offers proven, timeless solutions derived from the authors years of experience consulting and training in the VB arena.
EFFECTIVE VISUAL BASIC addresses general practices, from defensive programming to error handling and object-oriented design. In addition, it covers the critical issues in working with COM: interfaces, compatibility, and class design. Rules for building multi-tier applications are followed by a section on best practices for programming the Web. This book also addresses databases and effective data access, among the most important uses of VB.
Highlights include:
The product of a wealth of practical experience, EFFECTIVE VISUAL BASIC enables readers to advance their skills and become more effective software developers.
Designing, Building, and Working with COM-Based Components
Click below for Sample Chapter related to this title:
hummelch2.pdf
1. Shifting from Liberal Arts to Software Engineering.
Maximize the Potential of VB's Compile-Time Type Checking.
Make Assumptions Explicit Via Debug.Assert.
Consider #If When Compile-Time Conditions Differ.
Raise Errors to Signal Exceptional Conditions.
Trap Locally, Act Globally: Effective Error Handling.
Know the Difference Between Type and Class.
Embrace Good Object-Oriented Design, Not Just VB.
Prefer UDTs Over Classes for Value Types.
Automate Mundane Tasks.
Think in Terms of Interfaces.
Use Custom Interfaces.
Define Custom Interfaces Separately, Preferably Using IDL.
Avoid the Limitations of Class-Based Events with Custom Call-Backs.
Be Deliberate about Maintaining Compatibility.
Choose the Right COM Activation Technique.
Beware of Class_Terminate.
Model in Terms of Scenarios Instead of Entities.
Avoid ActiveX EXEs Except for Simple, Small-Scale Needs.
Understand the Design of MTS and COM+.
Don't Use Singletons in MTS and COM+.
Know When to Use New versus CreateObject versus GetObjectContext.CreateInstance.
Understand the Real Motivation for SetComplete.
Consider an Auto-Abort Style with Transactions.
Don't Reinvent the DBMS.
Don't Feel Obligated to Configure All Your Components.
Avoid Compiling Things into DLLs that You'll Later Regret.
Best Practices for Porting MTS Code into COM+.
Best Practices for Writing Code that Runs on MTS and COM+.
Understand the IIS Architecture.
Manage Application State to Maximize Efficiency.
Manage Session State to Maximize Scalability.
Understand the Differences Between DCOM and HTTP.
Write COM Components for Scripting Environments (like ASP).
Understand How Your COM Objects Interact with ASP.
Use XML Instead of Proprietary Data Formats.
Be Deliberate about Presentation vs. Business Logic.
Use XSLT to Move from Data to Presentation.
Efficiency Basics: Roundtrips, SQL Statements, and Providers.
Don't Over-Encapsulate Data Access.
Never Hold Database Connections as Data Members.
Deadlock is Common; Develop Defensively.
Use Firehose Cursors whenever Possible.</LI>
Make the Right Data Searching Decision (Avoid SelectSingleNode Abuse).
Visual Basic (VB) has become the most popular programming language in the United States. It is a large language and a complex product. Yet the ease with which it builds graphical user interfaces, accesses databases, and supports the Component Object Model (COM) is impressive.
This book is written by VB programmers for VB programmers. We assume you have been working with VB for quite some time and, if youre lucky, in a variety of ways: building front ends, reading databases, generating Web pages, programming COM objects. The goal of this book is to summarize a number of effective VB techniques that we have learnedand put into practiceover the years. This book has something for everyone, from general practices and COM-based components to COM+ and distributed applications. The wider your exposure to VB and the Windows Distributed Internet Application Architecture (DNA), the more applicable youll find the techniques.
We follow the style of other books in the DevelopMentor series, in that each technique stands alone as an independent item ready for application. We try our best to describe each item as concisely as possible, and to let you read the book in any order you like. In a few cases, however, one rule leads quite naturally to another and thus should be read in that order; well point these dependencies out to you.
If weve done our job, then reading this bookand taking its ideas to heartwill make you a more effective VB programmer. It will also make you a better practitioner because your skills will improve in other areas as well, such as object-oriented design, MTS, databases, and the Web.
Acknowledgments
We all work for a developer services company called DevelopMentor. Its how we met, and how we keep abreast of the technology. Its also a fantastic place to spend ones working hours, and we owe this book (and much more) to our friends and coworkers at DM. If you ever want to see what DevelopMentor is all about, attend one of our Guerilla eventsand be sure not to miss out on Thursday night. By the way, its not just the employees that make DM; its also the students and their energy. Thank you.
We also thank Gary Clarke for his sustained effort in getting this project off the ground and flying. We owe a great deal, as well, to Kristin Erickson (and her coworkers!) at Addison-Wesley for her tireless work behind the scenes, bringing this book to completion.
The anonymous reviewers did an excellent job, and we thank them for their hard work under a tight schedule. The book is much better as a result.
Finally, wed like to thank our families for their boundless support and understanding during our late nights of reading, researching, writing, and reinstalling Windows.
A
Abstraction, 42, 43–44, 46
Access permissions, checking, 170
ACID rules, 142, 154, 155
Activation, COM, 91, 99–110
CreateObject function, 99, 103–105
GetObjectContext.CreateInstance and Server.CreateObject, 99, 100, 106–107
GetObject function, 99, 105–106
New operator, 102–103
performance considerations, 107–110
types of, 107–108, 109, 118–119
Active Template Library (ATL), 143
ActiveX EXEs, 118–122
Activities in MTS/COM+, 124, 126, 127–128, 131
ADO disconnected recordset, 297
ADO objects, 136–137
ADO Recordset, 231–232, 235, 273, 288
disconnected, 297
Advanced Data Tablegram format (ADTG), 235
Apartment–threaded object, 221
Application variable and, 223–224
Session variable and, 222–223
API calls, 30
Application object. See ASP Application object
Application state management, 184–194
ASP Application object, 191–193
BAS module data and, 185–188
SPM and, 188–191
weighing options for, 193–194
Arguments
in conditional compilation, 15–17, 19
in scripts, 58
ASP, 150, 175, 185
eliminating from distributed application, 206–208
interaction with COM, 218–227
intrinsic objects, 219–220
benefits of, 225–226
disadvantages of, 226–227
presentation layer–business logic layer separation and, 236–243
MTS components and, 238–239
WebClasses and, 240–243
thread–pooling scheme, 223
threads in, 186
ASP.dll, 175
ASP Application object, 153–154, 220
apartment–threaded object and, 223–224
caching using, 191–193
ASP page, object creation from, 137
ASP Session object, 194–195, 219
AspTrackThreadingModel property, 224
Assignment of class versus type, 38, 39–40
Assumptions, explicit, 8–14
ATL (Active Template Library), 143
Atomicity, issue of, 154
Auto–abort style with transactions, 146–150
Automating mundane tasks, 53–60
with nmake, 53–56
with Windows Scripting Host (WSH),
56–60
B
Base client, 130
BAS modules, 122, 150
caching data in, 185–188
variables, 187–188
Batch files, 53
Batch SQL statement, 261–262
Binary Compatibility setting, 95–96, 97
Binding, client–object, 107
Binding mechanisms, client–object, 100–101
Brittle–code problem, 248–249
Browser, 237
Builds, debug versus release, 15–16
Business logic layer–presentation layer separation, 236–243
MTS components and, 238–239
WebClasses and, 240–243
ByRef, 36, 37, 40
ByVal, 36, 37, 40
C
C++, 41
Caching, 151, 156
of durable state web application, 184–194
ASP Application object, 191–193
BAS module data and, 185–188
SPM and, 188–191
weighing options for, 193–194
in–memory, 150–151
Callbacks, custom, 83–90
Class(es)
accessing, 66
custom interfaces and evolution of, 67–69
groups of, 48
real–world entities modeled by, 42–43
type versus, 35–41
value types and, 49–52
Class–based events, 83–86
Class–based references, 67
Class_Initialize, 43
Class_Terminate, 43, 111–114
Cleanup, 111–114
Client(s)
accidental DLL installation on, 170–171
base, 130
compiled, 93–96
object binding by, 100–101
scripting, 92–93
storing session information on, 196–201
via cookies, 197–198
in database, 201–202
in header, 197–198
in hidden form fields, 200–201
with QueryString variables, 199–200
Close methods, 111–112
CLSIDs, 91
Coclass (COM class) definitions, 81
CoCreateInstance, 164
CoCreateInstanceEx, 164
COM+, 119, 123–172
activities in, 124, 126, 127–128
Auto–Abort style with transactions, 146–150
coding practices for, 169–172
components configuration issues, 157–158
context in, 157
DBMS issues, 150–156
design of, 124–127
DLL issues, 159–164
object creation in, 129–140
porting MTS to, 164–169
declarative security checks and, 167
GetObjectContext.CreateInstance and, 164–165
MSI and, 168–169
ObjectConstruct string and,
166–167
RefreshComponents command and, 167–168
SafeRef and, 165
secondary object errors and, 165–166
SetComplete, 140–146
singletons in, 127–129, 132
synchronization in, 125–126
threads in, 186
transactional methods in, 141–142
transaction infrastructure in, 282–283
Windows 2000 and, 138–139, 149–150
COM. See also DCOM
activation, 91, 99–110
CreateObject function, 99, 103–105
GetObjectContext.CreateInstance and Server.CreateObject, 99, 100, 106–107
GetObject function, 99, 105–106
New operator, 102–103
&nb