![]() |
Site Archive (Complete) | |||
|
ABOUT US |
CONTACT |
ADVERTISE |
SUBSCRIBE |
SOURCE CODE |
CURRENT PRINT ISSUE |
NEWSLETTERS
|
RESOURCES
|
BLOGS
|
PODCASTS
|
CAREERS
|
||||
January 01, 2003
Implementing a Plug-In Architecture in C#Shawn Walcheske
This article describes how to implement a plug-in architecture in C#. You'll learn how to build a plug-in interface and how to employ the .NET feature known as reflection for run-time inspection of type information. The author then shows how to deploy a C# plug-in and provides a detailed example.
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. Figure 1 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.
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. 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.
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. Some points of interest include:
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 applications 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.
Loading Plug-Ins
Listing 4 contains the method LoadPlugs. LoadPlugs is located in HostForm.cs and is a private instance method of the HostForm class. The LoadPlugs method uses .NET reflection to load the available plug-in files, validates them as plug-ins that can be used by the host application, and then adds them to the host applications tree view. The method goes through several steps:
Deployment
The primary framework for the example application is deployed as two assemblies. The first assembly is Host.exe, and it houses the window forms host application. The second assembly is HostCommon.dll, and it houses all of the types that are used for communication between the host application and plug-ins. For example, the IPlug interface is deployed in HostCommon.dll so that it is equally accessible to both the host application and plug-ins. With the two assemblies in place, additional assemblies can be deployed that store the individual plug-ins. These assemblies are deployed to the plugs directory directly below the application path. The EmployeePlug class is deployed in the Employee.plug assembly, and the CustomerPlug class is deployed in the Customer.plug assembly. The example has taken the liberty of creating its own .plug file extension for plug-ins. The plug-in assemblies that are deployed are ordinary .NET library assemblies. Usually library assemblies are deployed with a .dll extension. The special extension has no influence on the runtime, but helps the plug-ins to stand out to users.
Alternative Designs
The design chosen for the example application is not exclusively correct. For example, using attributes is not required to develop a plug-in solution with C#. The services provided by the two defined attributes could also be provided by two property signatures added to the IPlug interface definition. Attributes were chosen because the name of a plug-in and its description are declarative in nature and fit quite nicely into the attribute model. Of course, using attributes requires more reflection code in the host application. This is a case-by-case design decision that developers will have to make on their own.
Conclusion
The example application is intended to be as thin as possible to help accentuate the communication between a host application and plug-ins. For a production environment, many improvements can be made to make a more useful solution. Some possible additions include:
Source CodeThe complete source code for the sample application is available for download in walchesk.zip.
Note[1] Erich Gamma et al. Design Patterns (Addison-Wesley, 1995).
About the Author
Shawn Patrick Walcheske is a software developer in Phoenix, Arizona. He is a Microsoft Certified Solution Developer and a Sun Certified Programmer for the Java 2 Platform. He can be contacted at questions@walcheske.com.
|
|
||||||||||||||||||||||||||
|
|