Notes
Outline
COM/COM+ and Visual Basic
Goals
I want to easily piece complex applications together from pre-existing parts.
I want the write the parts of the program assigned to me in a language I’m comfortable with and still have my code be compatible with the rest of the application.
I want to provide my pieces to the program in packaged parts which hide the implementation details from the rest of the developers.
I want the system to handle all the storing and loading of my program pieces and I want to be able to use them anytime and anywhere on the network.
I want to be able to purchase pre-built pieces for my application if I find suitable alternatives to writing them myself.  If I go this route, I want to easily be able to integrate some strangers code with mine no matter what language he wrote his piece in.
If I come up with an ingenious piece of code, I want to make it available to use in future projects no matter the development environment.
Scratch that last line, I want to be able to use it in other projects NOW, but I only want it stored once on my system so I don’t have to update it twice whenever I change it.
Traditional Client/Server Development
Each client application bound directly to the database
When the database structure changes (field names, relationships, etc…) EVERY client application needs to be re-installed to update changes
Every client machine maintains it’s own connections to the database.  This requires a large amount of resources on the client machines themselves
Drawbacks: Not very scalable, not very maintainable
Traditional Web Development
Much less maintenance on the client computers.  All that is required is a browser
Easier to maintain the entire application from a network perspective.  The entire application runs on a small number of servers.
Changes made to these servers affect EVERY user.
Drawbacks:  These few servers must support a large number of users.  If the web server is down, the entire application is down.
Windows DNA n-Tier Application
Many types of clients can easily be supported by the same infrastructure
All clients share the business logic stored in objects on the object server and data stored in the database.
No client makes requests of the database directly.  All database interaction takes place through the object server.
The object server (usually Microsoft Transaction Server or COM+ services) provides a number of performance benefits in this situation:
Database Connection Pooling
Multi-Threaded (Apartment) execution
Object Pooling
Distributed Transaction Support
COM
COM (Component Object Model)
‘Binary’ object standard making it language independent
Interface based
All objects are registered in the Windows registry
Easy to develop in a large number of languages
Visual C++
Visual Basic
Borland Delphi
Visual J++
VBScript/Jscript functions can be exposed  and used as COM Objects (although they are not technically binary COM objects)
Location independent
Objects can execute on any machine on the network transparently to the calling application
All objects stored as DLL’s or EXE’s in the Windows file system.  Loaded and executed on demand based on information based in the registry.  All this is handled transparently by the operating system making it extremely easy to use any COM objects registered on your system.
COM+
COM+
Microsoft’s Marketing guru’s added a ‘+’
Tightly integrated with the Windows 2000 operating system providing for increased performance
COM+ is identical to COM at a binary object level.  The same COM principles still apply.
The ‘+’ denotes a number of additional features and services added by the Windows 2000 operating system.
Queued Components (transparent integration with MSMQ)
Component Load Balancing (AppCenter Server)
Robust Eventing System
Component Services (Next version of MTS built into operating system)
Simple Objects in VB
Created as ActiveX EXE’s or ActiveX DLL’s
Each ‘Class’ within the VB project will be exposed as a COM object.
Each ‘Class’ has an ‘Instancing’ property which determines how it can be created by external programs.
‘Private’ – This class does not appear to other programs and objects of this type cannot be created outside of the host VB Class itself
‘PublicNotCreatable’ – This class can be viewed and manipulated by an external program but the external program cannot create a new instance of it.
‘Multiuse/GlobalMultiuse’ – This class can be created and used by external programs
Heavy use of Public/Private/Friend keywords.
‘Public’ properties and methods are exposed to all programs using this object
‘Friend’ properties and methods are exposed only to other classes or modules within the same VB project
‘Private’ properties and methods can only be used from within the same VB class as the property or method
Make heavy use of Encapsulation
It is possible to to simple, ‘Interface’ based inheritance using the ‘Implements’ keyword
Can use ‘Event’s to inform client application of changes in state or other events.
Object Hierarchies
COM objects can be ‘wrappers’ around collections of other objects
An individual property can return another object
Examples:
mobjPerson.Locations(2).City = “Ballwin”
mobjPerson.Relatives.Item(1).Locations.Item(3).State=“Nebraska”
For each mobjLocation in mobjPerson.Locations
Debug.Print mobjLocation.State
Next
Object Persistence in VB
What is Persistence?
The ability for an object to save, or ‘Persist’, it’s state to some form of storage medium (i.e., File, Database, etc)
The ability for an object to restore a previously saved state from the same medium.
Persisting objects to a database
Database tables with columns that map to object properties
Proper conversion of VB property types to database column data types
VB String Properties convert to SQL Server nvarchar() types, etc…
Unique identifier for each object
Object Hierarchies can be easily representing in a Relational Database
Since all data access is hidden from the user of a COM object, it is possible to load an object and all it’s child objects in one step.
For example:
mobjPerson.Load 23  ‘This command would load the data into the mobjPerson object as well as all it’s child collections without having the COM client worry about loading the children separately
Object Persistence (Option 1)
Connect to the database directly from the client side application
Public Sub Set_Name(ByVal ID as Long, ByVal Name as String)
Dim mobjRS as ADODB.Recordset
Set mobjRS = New ADODB.Recordset ‘Create a new ADO recordset
mobjRS.Open “Select * From PersonTable Where ID=” & ID, cnConnection, _
adOpenKeyset, adLockOptimistic
mobjRS.Fields(“Name”) = Name ‘Set our property
mobjRS.Update
mobjRS.Close
Set mobjRS = Nothing
End Sub
Object Persistence (Option 2)
Use a simple COM object which wraps up our data:
Public Sub Set_Name(ByVal ID as Long, ByVal Name as String)
Dim mobjPerson as Person
Set mobjPerson = New Person ‘Create a new instance of Person class
With mobjPerson
.Load ID
.Name = Name
.Save
End With
Set mobjPerson = Nothing
End Sub
VB and COM
When you request a COM object in VB the system looks through the current VB project references to find which COM libraries are referenced and then looks through the registry to find the location of the file containing that COM library.  Once the file is loaded the COM system dynamically allocates memory and loads the correct object from the library into the memory of the computer which is being used to run the object.  It then returns to VB a pointer to that instance of that object in memory.
All the above happens transparently to the user EVERY time a COM object is requested by a client application.
All the programmer has to do is request the object through COM.
VB exposes three ways to create a COM object:
New – The standard ‘New’ keyword (I.e. Set mobjObject = New Person)
CreateObject(“Library.Object”,”Computer”) – This method requires you to specify both the entire name of the object requested as well as the computer on which it should run.  Note that the object must already be installed on the computer it is to run on.
CreateInstance – This is used to maintain transactional integrity in MTS and I won’t go into it much here. But it is a way to create COM objects in VB nonetheless.
VB and COM
Almost everything in VB 5 and 6 is based on COM.
VB is also a very simple tool to rapidly create your own COM objects without much knowledge of the underlying COM infrastructure.
When you compile an ActiveX Dll project in VB containing any number of objects, VB handles all the registering of the object and it’s interfaces behind the scenes.  All you have to do is click on ‘File->Make MyDLL.dll…’ and specify a location to save your newly created DLL.  VB automatically creates a language neutral COM object and handles all the registration process behind the scenes.
As far as the COM system is concerned “ocx” files and COM “dll” files are all binary COM objects.  The “ocx” extension indicates that this COM object exposes a graphical interface and exposes a certain set of properties required by the ActiveX specification (I.e. Top, Left, Forecolor, Backcolor, etc, etc…)
Since ActiveX components make up most of the user interface of a large number of programs (I.e. Buttons, Toolbars, etc), you can see how heavily the operating system depends on the underlying COM infrastructure.
Distributed COM (DCOM)
In the previous code examples, where did the ‘Person’ object run?
How could we have ran it on another computer?
Public Sub Set_Name(ByVal ID as Long, ByVal Name as String)
Dim mobjPerson as Person
Set mobjPerson = CreateObject(“PersonDLL.Person”,”KevinsComputer”) ‘Create a new instance of Person class
With mobjPerson
.Load ID
.Name = Name
.Save
End With
Set mobjPerson = Nothing
End Sub
Notice the rest of the code functions no differently no matter where the object runs.
Additional Information
Msdn.Microsoft.com – The complete source of everything Microsoft and the complete online MSDN library
www.devx.com – The people who write VBPJ – an excellent source for the latest news and information on the programming world
VBPJ – Visual Basic Programmers Journal – Get it if you can
www.vbchat.com - Great discussion boards and an IRQ chat for VB developers
ANY WROX books which interests you
Learn to Program Objects With Visual Basic 6  by John Smiley (WROX Press)
Visual Basic 6.0 Business Objects by Rockford Lhotka (WROX Press)
Enterprise Application Architecture with VB, ASP and MTS  by Joseph Moniz (WROX Press)
Understanding and Programming COM+: A Practical Guide to Windows 2000 DNA  by Robert Oberg (Prentice Hall)
Me
kvgros@sseinc.com (Work)
kvgross@swbell.net (Home)
Feel free to ask any questions you might have