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:
- 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 theIPlug
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. - 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.
- 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 typeEmployeeData
, which is a concrete class that supports the interfaceIPlugData
. TheIPlugData
interface is defined inIPlugData.cs
and provides a lowest common denominator for data exchange between the host application and any of its plug-ins. Objects that support theIPlugData
interface provide notification when the underlying data changes. The notification comes in the form of theDataChanged
event. - 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 ofIPlugData
objects. The host application can then call theToString
method of each object in the array to use as the text for a tree node. TheToString
method is overridden in theEmployeeData
class to display an employees full name. - The
IPlug
interface defines aSave
method and aPrint
method. The purpose of the two methods is to notify a plug-in when it should print and save its data. TheEmployeePlug
class is responsible for providing an implementation for printing and saving its data. In the case of theSave
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 thePrint
method, the host application passes in aSystem.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 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.