Thursday, March 15, 2012

Objects vs Components


This article is in continuation with my previous article dated 27 Feb 2012

Components

Building on the idea of object-oriented programming comes component-oriented design, which facilitates even better reuse of the objects we design and build.

Objects vs Components

Objects are created from classes and classes are made up of source code - which is language specific and thus can only be used, easily, in one environment. In contrast components are pre-compiled units of binary code - thus they are language independent.

A component may consist of one object or a collection of objects.

A component based approach to software design enhances the OO approach we first looked at - in the case that a component consists of a group of objects, we can actually define which objects are accessible outside the component (and hence keep some objects restricted to being accessed by other objects inside the component) by defining the interface of the component.

COM

Of course the above section rather glosses over at least one very important topic; how do we get all these components talking to each other? That's where the Component Object Model (COM) comes in - it's Microsoft's standard for allowing objects and components to interact irrespective of the language in which the components were first built. So, using COM technology we can call up a component, and providing we know what interface it has, we can get it to go away and do things for us.

As part of the COM mechanism, components that we create in VB actually have a number of interfaces that are provided as part of the standard architecture in addition to any interfaces we define. We'll discuss this further at the appropriate time, but the important thing to note is that VB fits seamlessly into COM and as VB developers we are insulated from a great deal of complexity.

ActiveX

An ActiveX component is an application that stands alone and lets other applications use the classes and objects it contains.

Active Scripting

ASP pages are able to access ActiveX components through a technology called Active Scripting. We've skated over the previous general theory pretty quickly (as we alluded to, there are whole books written on the subject) but it's probably worth lingering over this particular aspect.

Active Scripting is a reusable scripting engine that, if you want, you can use in your own application. It can be found in use in Active Server Pages (ASP) and Windows Script Host (WSH). Active Scripting is capable of supporting many languages by allowing developers to write supporting language plug-ins.

Active Scripting ships with VBScript (a cut down version of Visual Basic) and JScript. Other vendors have developed PERL plug-ins, as well as support for other languages. Broadly, this means that you can write ASP code in whatever language plug-in you have available, as Active Scripting and the plug-in work together to make the call into the component and, through that call, tell Visual Basic to execute the code contained within the method or property.

ASP works (as illustrated below) by stripping out all of the VBScript code, creating an instance of Active Scripting, and asking it to execute the code on its behalf. ASP presents a set of its own ActiveX components to the context of the script (the environment that the script runs in). So, the Response object we call in ASP is actually an ActiveX component that ships with ASP, and when ASP fires up Active Scripting to run your code, it passes a reference to this component to Active Scripting and asks for it to be made available to your code as Response.


At this point we need to bring in the idea of a type library. A type library is a file that describes an object (here meaning component or control) in a standard format such that anything wanting to make use of that object can find out which classes are accessible (or supported). This type library is actually embedded into ActiveX DLL files created via Visual Basic; however, it's still available to programs interacting with it using COM.

So, when our ASP page attempts to call a method on the Response object, Active Scripting looks at the type information in the type library and determines if the method or property name is valid and, if it is valid, what parameters it takes (the type library is not just a list of the names of the methods and properties - it also includes the parameters that any call has and the possible types of value that can be returned). This inspection of the published information is part of the technology that makes up ActiveX, not Active Scripting.

1 comment: