Project Description:

.NET Plugin Loader Framework using MEF(Microsoft Extensibility Framework). It has a generic loader which loads plugin that implements generic interface IPlugin.

Note: It requires .Net version 4 or above as The Managed Extensibility Framework (MEF) is available from .NET Framework 4.0.

Features:

  • Load plugins only in given specified plugin path, instead loading all at once
  • Get All Plugins of given IPlugin Type and its metadata type IPluginMetaData

How to Use:

For example, you want to  develop your own Continuous Integration Tool to support multiple version controls and multiple platforms as part of that you may need to perform below operations:

  1. Get the source code from different version control systems like SVN, TFS and so
  2. Build projects on different platforms like .NET, Java and so

CASE#1:

Get the source code from different version control systems:

1. Create interface for source control plugin as below.

 /// <summary>
  /// Interface for Source control plugin.
  /// </summary>
  public interface ISourceControl : IPlugin
  {
    /// <summary>
    /// Gets the source code.
    /// </summary>
    /// <param name="options">The options.</param>
    void GetSourceCode(SourceControlCheckoutOptions options);
  }

 

2. Create Interface for source control metadata as below.

  /// <summary>
  /// Interface for Source control plugin metadata.
  /// </summary>
  public interface ISourceControlData : IPluginMetaData
  {
    /// <summary>
    /// Gets the type of the source control.
    /// </summary>
    /// <value>
    /// The type of the source control.
    /// </value>
    string SourceControlType { get; }
  }

 

3. Implement source control plugin for SVN.

  /// <summary>
  /// The Plugin class for getting source code from SVN.
  /// </summary>
  [Export(typeof(ISourceControl))]
  [ExportMetadata("SourceControlType", "SVN")]
  public class SVNSourceControl : ISourceControl, IPlugin

Note: Create this class in a project, so that you would deploy independently without changing anything.

And like this you can create source control plugin for other versions controls and deploy independently.

For Name Value pair in the Export Metadata attribute, the Name should be the property name of ISourceControlData.

 

4. After having the plugin implementation, now time to load and consume the plugin, so let having a manager class like SourceControlManager class

  Declare a class variable as below.

/// <summary>
/// The field holds all available source controls plugin.
/// </summary>
private IEnumerable<LazyPlugin<ISourceControl, ISourceControlData>> _sourceControls;

 

Call Plugin Loader in constructor to load the ISourceControlPlugin plugins only,do this in the constructor of   manager class as said below.

 

   _sourceControls = (new PluginLoader(PluginsPath)).GetPlugins();

 

Note: PluginsPath should be the path where the dll(s) of implemented plugins available.

To Get All source control plugins that are available.

(from s in _sourceControls
              select s.Metadata.SourceControlType)

 

Get the source control plugin based on source control type e.g., "SVN" which the same string specified in  the export meta data of the SVN source control plugin class definition.

    /// <summary>
    /// Gets the current source control.
    /// </summary>
    /// <param name="sourceControlType">Type of the source control.</param>
    /// <returns>The source control.</returns>
    private ISourceControl GetCurrentSourceControl(string sourceControlType)
    {
      var sourceControl = from s in _sourceControls
                          where s.Metadata.SourceControlType.Equals(sourceControlType)
                          select s.Plugin;
      return sourceControl.FirstOrDefault();
    }

CASE#2:

Build projects on different platforms:

Follow the same steps said above for this case. 

 

Hope this will help you.

You can reach me @ mailto:narayanareddy.tera@gmail.com for any clarifications and suggestions.


Happy coding!!

Last edited Jul 20, 2013 at 12:05 PM by sakethtera, version 9