This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
Class vs. Component vs. Control
A class that targets the NGWS runtime can be versioned via assemblies. Additionally, if it exposes only Common Language Specification (CLS) compliant members, it can be used seamlessly from other CLS-compliant languages. A NGWS runtime or frameworks class thus satisfies the major requirements for software reuse. Why then is another entity called component defined, and what is its role?
- A component is a class with emphasis on cleanup and containment. A component can be hosted in a container, and has the ability to query and get services from its container. A component ensures that resources are released explicitly through its Dispose method, without waiting for automatic memory management. During teardown of a container, it is guaranteed that all resources allocated to the container and its components will be released. Containment is logical and does not have to be visual. A middle tier container that sites database components is an example of logical containment.
Note: The Win Forms and Web Forms designers in Visual Studio 7.0 require that a class must be a component to be displayed in the designer. This requirement is based on considerations such as described above.
The definitions of a component and a control are given below.
- Component
- To be a component, a class must implement the System.ComponentModel.IComponent interface, and provide a constructor that requires no parameters. A class can also be a component if it derives from a class that implements System.ComponentModel.IComponent such as System.ComponentModel.Component. A component can be contained and sited by a container. When sited in a container, a component interacts with the container through its container provided site.
- Control
- A control is a component with user interface (UI) capabilities. NGWS frameworks has not defined interfaces that a class must implement in order to be a control. The only controls in NGWS frameworks are classes that derive from System.WinForms.Control or System.Web.UI.Control, and their subclasses. Both of these base classes are components that provide basic UI capabilities.
[C#]
//MyWinControl is a control since RichControl derives from
//System.WinForms.Control
using System.WinForms;
public class MyWinControl : RichControl {…}
//MyWebControl is a control since WebControl derives from
//System.Web.UI.Control
using System.Web.UI;
public class MyWebControl : WebControl {…}
If you are developing components and controls for Win Forms or Web Forms, you will not have to implement containers or sites. The Win Forms and Web Forms designers are containers for Win Forms and Web Forms controls. Containers are used for providing services. At design time, controls are sited in the designer and obtain services from the designer. For completeness, the definitions of a container and a site are given below.
- Container
- To be a container, a class must implement the System.ComponentModel.IContainer interface and provide a constructor that requires no parameters. A class can also be a container if it derives from a class that implements System.ComponentModel.IContainer. A container logically contains one or more components that are called the container's child components.
- Site
- Sites are provided by a container to manage and communicate with its child components. Typically, a container and a site are implemented as a unit. To be a site, a class must implement the System.ComponentModel.ISite interface.
A Win Forms control sited in Microsoft Internet Explorer 5.5, is another example of a component cited in a container. The browser provides services, such as ambient properties, to the control through its site.
See Also
Essential Programming Constructs