Chapter 25: Building Components


Although C# can be used to write nearly any type of application, one of its most important is the component. Component-based programming is so integral to C# that it is sometimes referred to as a component-oriented language. Because C# and the .NET Framework were designed with components in mind, the component programming model is substantially simplified and streamlined over older approaches. For example, if the term component makes you think of COM components and all the trouble they can be, don’t worry. C#-based components are much easier to write.

What Is a Component?

Let’s begin by defining what is meant by the term component. Here is a general definition: A component is an independent, reusable unit of binary functionality. This definition describes four key characteristics of a component. Each is examined, in turn.

A component is independent. This means that each component is self-contained (that is, encapsulated). Thus, a component provides all the functionality it needs. Furthermore, its inner workings are not exposed to the outside; the precise implementation of the component can change without affecting the code that uses the component.

A component is reusable, which means that a component can be used by any other program that requires its functionality. A program that uses a component is called a client. Thus, a component can be used by any number of clients.

A component is a single unit of functionality. This is a key concept. As viewed from the client, a component performs a specific function, or a set of functions. The functionality provided by a component can be used by an application, but a component, by itself, is not a stand-alone program.

Finally, a component’s reusability is provided in a binary form. This is fundamental. Although any number of clients can use a component, they do so without access to the component’s source code. The functionality of a component is exposed to clients through its public members. Thus, a component controls what functionality is available to clients and what functionality it keeps private.

The Component Model

Although the definition just given does accurately describe a software component, another issue fundamentally affects a component: the model it implements. For a client to use a component, both the client and the component must use the same set of rules. The set of rules that define the form and nature of a component is called the component model. It is the component model that defines how a component and a client interact.

The component model is important because reusable, binary functionality can be created in any number of ways. There are different ways to pass parameters and to return values, for example. Also, there are various ways to handle the allocation and release of memory or system resources. For clients to be able to use components freely, both must follow the rules defined by the component model. In essence, the component model defines the contract between the client and the component by which both agree to abide.

As a point of interest, prior to C# and the .NET Framework, most components were COM components. COM, which stands for Component Object Model, was designed for the traditional Windows environment and C++. As such, it did not receive the benefits of the modern memory management that C# and the .NET Framework provide. As a result, the COM contract was rather difficult to implement and was error-prone. Fortunately, C# and the .NET Framework have eliminated nearly all of these troubles. So if you have had bad experiences with COM in the past, you will be pleasantly surprised by the ease with which components are created in C#.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net