Page

16.1.1- What is a Component?

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 16. Building Script Components for ASP.

Not tagged.
<< 16.1.0- A Brief Introduction to ComponentsChapter1616.1.2- Why do we Use Components? >>

What is a Component?

So these are two approaches that are very much encouraged in application programming of all kinds (not only web application programming). Breaking a large application down into smallish, logical, self-contained sections makes the whole thing much easier to understand, to write and to maintain. Using procedures and functions means that those pieces of code only need to be written once, and can be used often – again facilitating the design, writing and maintaining processes.

 

In fact, these approaches are well-recognized, but we can go further than that. In fact, in order to optimize the way that we implement our application in logically-dissected self-contained blocks, we can use components.

 

You can think of a component as being a small, self-contained nugget of code that provides a certain amount of related functionality and information – in the form of methods, properties and events. When we write an application, we might use a number of components – each component is designed to do a specific job, and the application utilizes the specific functionality of each component in a particular way, to achieve its goal.

 

Even better, there is an established technology available that allows us to build components in a way that allows them to talk to one another – that technology is COM (the Component Object Model). COM is a framework for creating and using components in such a way that their functionality is well-defined and unambiguous. Also, many programming languages are COM-compliant, which is good news for us. It means that COM-compliant languages and COM components can all work together, without language barriers, to solve large and complex problems.

We won't spend much time in this book getting into the complexities of COM – it's beyond the scope of the book. There are many good books on COM; for an introduction, you might like to try Beginning Components for ASP (Wrox, ISBN 1-861002-88-2). (This book is no longer available from Wrox but you might find it from a store that sells used books.)

Examples of Components

Before we think about a programming example, let's draw an analogy with a real-life example. Consider the art of photography. Here are some of the different basic camera parts that you'd need to buy if you were choosing to take up photography as a hobby:

 

  • You'd need a camera body, of course – that's the bit that you put the film into, and which has the button that takes the photograph
  • You'd need a lens, which you attach to the camera body and which focuses the light from outside onto the film inside the camera's body
  • You might want to put a filter on the end of the lens, to create a particular effect
  • You might want a flash, for taking indoor photographs in poor light

 

OK, let's suppose that you've bought a basic photographer's kit that contains a camera body, a regular lens with zoom facility, a filter or two and a flash mechanism. Then a month later, you decide that you really want to start taking pictures of landscapes instead. Your regular lens with zoom facility isn't great for landscapes; what you really need is a wide-angle lens instead. But you don't go and buy a whole new camera – all you need to do is buy a new lens, detach the zoom lens and attach the wide-angle lens.

 543636_pg728.jpg

 

So each component here is a bit like a black box – it is responsible for a certain part of the overall job, and we use whichever components are appropriate for our purpose, by piecing them together in the way we need.

 

OK, that's photography, but what about programming? Well, working with components in our programming code is just like working with different photographic components when preparing to take photos. Each component is designed to provide a specific piece of functionality, or a set of related functionality, and we can piece any number of required components together in our application in order to do the job.

 

If fact, you may not have realized it but we have been using components all the way through this book! For example:

 

  • We have been writing web applications using ASP, and for that we needed the ASP intrinsic objects (Request, Response etc). In fact, whenever we need one of these objects in our code, the object is created from a COM component – this component provides the compiled code that defines the object's properties, methods and events. The code for this component is contained in a dynamic link library (DLL) file called asp.dll; you'll be able to find this file somewhere on the hard disk of your machine. The asp.dll file is used by your web server when a user requests an ASP page; but if the user requests an HTML page then the asp.dll component isn't required.
  • The ADO data access objects (Connection, Recordset etc) that we've used are also defined by a COM component, whose compiled code is contained in the file msado15.dll. When we're involved in data access with ADO, the system digs out msado15.dll and uses it to provide data access functionality.
  • Even better, consider one of the very familiar applications that we use every day – perhaps something like Internet Explorer. Many applications like this are written using components. For example, when you start up Internet Explorer, the system runs the base executable file Iexplore.exe. But Iexplore.exe is very small – it's only around 60Kb. All the functionality associated with Internet Explorer is actually contained in the numerous components that are only called into action (by Iexplore.exe) when they're needed.

 

<< 16.1.0- A Brief Introduction to ComponentsChapter1616.1.2- Why do we Use Components? >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |