Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Implementing a Plug-In Architecture in C#


Plug-Ins

The example application contains two plug-in implementations. The plug-ins are defined in EmployeePlug.cs and CustomerPlug.cs. Listing 3 shows a partial listing of the class definition for EmployeePlug.

Listing 3: A partial listing of the EmployeePlug class definition

[PlugDisplayName("Employees")]
[PlugDescription("This plug is for managing employee data")]
public class EmployeePlug : System.Object, IPlug
{
  public IPlugData[] GetData()
  {
     IPlugData[] data = new EmployeeData[]
      {
        new EmployeeData("Jerry", "Seinfeld")
        ,new EmployeeData("Bill", "Cosby")
        ,new EmployeeData("Martin", "Lawrence")
      };

    return data;
  }

  public PlugDataEditControl GetEditControl(IPlugData Data)
  {
    return new EmployeeControl((EmployeeData)Data);
  }

  public bool Save(string Path)
  {
    //implementation not shown
  }

  public bool Print(PrintDocument Document)
  {
    //implementation not shown
  }
}

Some points of interest include:

  1. The class implements the IPlug interface. This is important because by design the host application has no direct knowledge of any specific plug-in class definition. It communicates with all plug-ins using the definition of the IPlug interface. This design utilizes the object-oriented principle of polymorphism. Polymorphism allows for the communication with a type to occur with a reference to one of its base types or an interface that the type supports.
  2. The class is marked with the two attributes required by the host application to be considered a valid plug-in. In C#, to assign an attribute to a class, you declare an instance of the attribute enclosed in brackets before the class definition.
  3. In an effort to be brief and focused, the class is using hard-coded data. In a production plug-in, the data would be persisted in a database or file. Each plug-in is ultimately responsible for the management of the data it contains. The data for EmployeePlug is stored as objects of type EmployeeData, which is a concrete class that supports the interface IPlugData. The IPlugData interface is defined in IPlugData.cs and provides a lowest common denominator for data exchange between the host application and any of its plug-ins. Objects that support the IPlugData interface provide notification when the underlying data changes. The notification comes in the form of the DataChanged event.
  4. When the host application needs to display a list of the data a plug-in contains, it calls the GetData method. The method returns an array of IPlugData objects. The host application can then call the ToString method of each object in the array to use as the text for a tree node. The ToString method is overridden in the EmployeeData class to display an employee’s full name.
  5. The IPlug interface defines a Save method and a Print method. The purpose of the two methods is to notify a plug-in when it should print and save its data. The EmployeePlug class is responsible for providing an implementation for printing and saving its data. In the case of the Save method, the path to save the data in is provided within the method call. This presumes that the host application will query the user for the path information. The querying of path information is a service that the host application supplies to each of its plug-ins, saving the plug-ins from having to query the user itself. For the Print method, the host application passes in a System.Drawing.Printing.PrintDocument instance that contains the selected printing options. In both cases, the interaction with the user is consistent as provided by the host application.

Reflection

With a plug-in defined, the next step is to examine the code in the host application that loads plug-ins. To do this, the host application uses reflection. Reflection is a feature in .NET that allows for the run-time inspection of type information. With the aid of reflection, types are loaded and inspected. The inspection discerns if the type can be used as a plug-in. If a type passes the tests for a plug-in, it is added to the host application’s display and becomes accessible to users.

The example application uses three framework classes for its reflection work: System.Reflection.Assembly, System.Type, and System.Activator.

The System.Reflection.Assembly class represents a .NET assembly. In .NET, the assembly is the unit of deployment. For a typical Windows application, the assembly is deployed as a single Win32 portable executable file, with additional information to enable the .NET runtime. Assemblies can also be deployed as Win32 DLLs (dynamic link libraries), with additional .NET runtime information. The System.Reflection.Assembly class can be used at run time to get information about the executing assembly or any other assembly accessible to the executing assembly. The available information includes the types that the assembly contains.

The System.Type class represents a type declaration. A type declaration could be a class, interface, array, structure, or enumeration. After being instantiated with a type, the System.Type class can be used to enumerate supported methods, properties, events, and interfaces of the type.

The System.Activator class can be used to create an instance of a type.


Related Reading


More Insights






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.