As for your MEF question, you can do something like the following to run methods from the interface:
var catalog = new DirectoryCatalog("bin"); var container = new CompositionContainer(catalog); container.ComposeParts(); var plugins = container.GetExportedValues<IPlugin>(); foreach (IPlugin plugin in plugins) { plugin.OnCreating(); }
Or create an event interface as suggested by Brian Mines:
public interface IPlugin { event OnCreatingEventHandler OnCreating; }
then the above code will be more like:
var catalog = new DirectoryCatalog("bin"); var container = new CompositionContainer(catalog); container.ComposeParts(); var plugins = container.GetExportedValues<IPlugin>(); foreach (IPlugin plugin in plugins) { plugin.OnCreating += MyOnCreatingHandler; }
I think I like the latter for the names of the methods you specified. For my work with the plugin, I created an interface similar to the following:
public interface IPlugin { void Setup(); void RegisterEntities(); void SeedFactoryData(); }
The RegisterEntities() method extends the database schema at run time, and the SeedFactoryData() method adds any default data (for example, adding a default user, pre-populating a city table, etc.).
James fernandes
source share