DekGenius.com
Team LiB
Previous Section Next Section

Spec My Components

Previously in this chapter, you learned about the different application architectures and how ADO .NET is integrated in each one. We covered ASP .NET in more detail because its architecture is substantially different for classical applications. ASP .NET is based more on distributed application architecture. Distributed application has at least three main layers, or tiers: the presentation tier, the business tier, and the data tier. Components in any of the tiers, especially the business and data tiers, are designed to be independent and can be used as building blocks to different applications. In the following sections we will look at data components, which are found in the data tier.

What is a Component in .NET?

Generally in programming, “component” is used to refer to self-contained, compiled pieces of code that are reusable and can interact with other codes and objects. Previously, components were implemented through the COM model or derivatives, such as COM+ and DCOM. In the .NET Framework, a component is simply a class that implements the System.ComponentModel.IComponent interface or is inherited directly or indirectly from a class that implements IComponent. A .NET Framework component also provides additional features, such as control over external resources and design-time support.

Components are hosted or sited within a container.

  • Container: This is a class that implements the System.ComponentModel.IContainer interface or is inherited from a class that does. The IContainer interface must support methods for adding, removing, and retrieving components. A container contains one or more components that are referred to as the container’s child components.

  • Site: This is a class that implements the System.ComponentModel.ISite interface or is inherited from a class that does. Sites are provided by a container as a way to manage and communicate with their child components. Typically, a container and a site are implemented as one unit.

When to Build Data Components

When you develop an application that shares data, you should consider merging that data into a single component. If you store your data via data components, you provide a standard way for other components to access the data. For example, if you have a stock entry component, it can be used by both an inventory management component and an order processing component. You first have to determine the component’s functionality, and only then will you be prepared to refine the component design and implementation.

Component Design Guidelines

When you design your component, your first decision is which tier the component will fall into. This is important, but it is not always easy to separate the logic. It takes some experience to correctly do the separation, and it will affect how you design the component. In this book, we are more interested in the data tier, so I will concentrate on design guidelines for data components, especially from the point of view of ASP .NET applications.

All your component design decisions are interrelated. Your component function will affect how it is used in your applications and will, in turn, affect what kind of models you will use.

Component Scope

When you create an instance of a component in your ASP .NET application, you must decide what its scope will be. You have three scopes available to you:

  • Page: Most business rule components are placed at the page scope. The object created on the page scope is available on the page. It is created with each round-trip and destroyed each time.

  • Session: If the component functionality spans multiple pages, it should be at the session scope. Session scope objects are created for each session a user has with the server. The object is only destroyed when the user disconnects from the server. This is the usual scope for data components. Be aware that session variables consume resources on the server, and this can have some issues with scalability.

  • Application: This scope is usually reserved for application-wide components, such as a page counter component. Variables and objects at application level are available to all users and sessions.

The scope of your component will determine which methods you choose to implement in your component and how you store state variables. If you have a component that is scoped for page, there is no need to store data in variables so that the successive page accesses them. Remember, the component is destroyed in each round-trip. Instead, you would have to store the data in the data source directly and recreate them in each round-trip or each time the component is constructed.

Component Implementation

Implementing components in .NET is as simple as implementing any other kind of class with a few rules that you must follow so that the class is considered to be a component, a container, or a site, as we discussed previously. There are also a few guidelines that you are advised to follow so that your component is efficient, maintainable, and scalable. We will discuss issues specific to the implementation of components next.

Properties vs. Public Fields

In a class, you can give the developer access to member fields by either making the fields public or providing methods that modify those fields. To maintain the rule of encapsulation, you should avoid the use of public fields in all your classes. In components, methods that modify fields have a special syntax and are called properties.

Visual designers, such as Visual Studio .NET, display properties but do not display public fields. Therefore, it is better for a component to define properties instead of public fields. Public fields work against the principle of encapsulation in the object-oriented paradigm, whereas properties embrace encapsulation. Properties act like intelligent fields and normally have a private data member combined with modifier and accessor functions. It is then accessed syntactically as a field of a class.

A property definition generally consists of the following two pieces: a private or protected data field and a public or protected property.

Public Class APropertyExample

  ' The data field.
  Private amount As Integer = 0

  Public Property propAmount As Integer
    ' Retrieves amount.
    Get
      Return amount
    End Get

    ' Assigns to amount.
    Set
      amount = value
    End Set
  End Property

'Other members...
End Class

  ' Example of how to use the property
Public Class UseAPropertyExample
  Public Shared Sub Main()

   ' The data field.
    Dim example As New APropertyExample()

    ' Sets the property.
    example. propAmount = 5

    ' Gets the property.
    Dim anumber As Integer = example.propAmount
  End Sub

End Class

As you can see in the code snippet above, the property is further divided into two parts: the data accessor (or Get function) and the data modifier (or Set function).

Team LiB
Previous Section Next Section