Channels ▼
RSS

Design

Implementing a Plug-In Architecture in C#


Introduction

More Insights

White Papers

More >>

Reports

More >>

Webcasts

More >>

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 organization’s 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 developer’s 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-in’s 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;
  }


Related Reading






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video