Introduction
C#, in concert with the underlying .NET platform, provides some powerful features and constructs. Some of what is offered is new, and some is the sincerest form of flattery to the platforms and languages that have come before it. However, the unique combination of features provides some interesting ways to achieve project goals. This article discusses some of the features that can be used to design extensible solutions with the use of plug-ins. It also examines a skeleton example that has the potential to replace the stand-alone applications that are found in many organizations. An organizations application suite usually consists of many targeted solutions to manage data. One application might exist for managing employee information, and another might target customer relationships. Often solutions of this nature are designed as stand-alone applications with little interaction and some duplication of effort. As an alternative, these solutions can be designed as plug-ins that are hosted by a single application. A plug-in design helps to share common functionality between solutions and also provides a common look and feel.
Figure 1 shows a screenshot of the example application. The user interface is similar to many other familiar applications. The window is divided vertically into two panes. The left pane is a tree view used for displaying the list of plug-ins. Under each plug-in branch are branches for the data on which the plug-in operates. The right pane is used for editing plug-in data after it is selected in the tree view. The individual plug-ins provide the user interface for editing the data they contain. It also shows a workspace that might be used by a talent agent. The agent needs to manage his comedic talent and the clubs that they perform in.
Figure 1: The example application
The Big Picture
At the highest level of abstraction, the host application must be able to load plug-ins and then communicate with plug-ins to take advantage of their designed services. Both of these tasks can be implemented in different ways depending on a developers choice of language and platform. If the choice is C# and .NET, then reflection can be used for loading plug-ins, and interfaces or abstract classes can be used as a means for generic communication.
To help conceptualize the communication that occurs between a host application and a plug-in, the Strategy design pattern can be applied. For the uninitiated, Erich Gamma originally offered design patterns [1]. Design patterns help to communicate commonly used architectures and object strategies. The premise is that while projects differ in their inputs and outputs they are for the most part similar in structure. Design patterns help developers utilize proven object strategies to solve new problems. They can also become the de facto language that developers use to discuss solutions without intimate knowledge of the problem to be solved, or the specific language constructs that will be used. The gist of the Strategy design pattern is to decouple the functionality of a solution from its container. This decoupling is achieved by separating the implementation of the host application and the things it must do into strategies. Communication between the host application and its strategies is then done through a well-defined interface. This separation provides two immediate benefits. First, anytime a software project can be broken down into smaller discrete units, it is a bonus to the engineering process. Smaller code pieces are easier to build and maintain. The second benefit is the ability to switch strategies without affecting the operation of the host application. The host application is not concerned with specific strategies, just the common mechanism to communicate with the strategies.
Creating the Interface
In C#, an interface defines what a class is expected to support. The expectation
that an interface defines is a collection of signatures for methods, properties,
and events. To use an interface, a concrete class must be defined that implements
the interface by defining what each signature does. Listing 1 shows the interface, IPlug
, defined in the example application.
Listing 1: The IPlug interface
public interface IPlug { IPlugData[] GetData(); PlugDataEditControl GetEditControl(IPlugData Data); bool Save(string Path); bool Print(PrintDocument Document); }
The interface defines four methods: GetData
, GetEditControl
, Save
,
and Print
. These four method definitions do not define any implementation,
but they guarantee that any class that supports the IPlug
interface will
support the method calls.
Custom Attributes
Before examining any more code, the discussion must turn to custom attributes.
Custom attributes are one of the exciting new features that .NET developers
can utilize. Attributes are a common construct to all programming languages.
For example, the access specifier of a method (public
, private
,
or protected
) is an attribute of that method. Custom attributes are exciting
because .NET developers are no longer restricted to the set of attributes designed
into their programming language of choice. A custom attribute is a class that
is derived from System.Attribute
and allows the code you write to be
self-describing. Custom attributes can be applied to most language constructs
in C# including classes, methods, events, fields, and properties. The example
code defines two custom attributes, PlugDisplayNameAttribute
and PlugDescriptionAttribute
,
that all plug-ins must support at the class level. Listing
2 is the class definition for PlugDisplayNameAttribute
. This attribute
is used by the host application as the text to display for the plug-ins
tree node. During execution, the host application is able to query the value
of attributes by using reflection.
Listing 2: The PlugDisplayNameAttribute class definition
[AttributeUsage(AttributeTargets.Class)] public class PlugDisplayNameAttribute : System.Attribute { private string _displayName; public PlugDisplayNameAttribute(string DisplayName) : base() { _displayName=DisplayName; return; } public override string ToString() { return _displayName; }