To load plugin collections, I lean toward the IoC container to load assemblies from the directory (I use StructureMap), although you can manually load them according to @Oskar's answer.
If you want to support loading plugins while your application is running, StructureMap can be "reconfigured", thus choosing new plugins.
For your applications, you can send events to the event bus. In the example below, the StructureMap structure allows you to find all registered event handlers, but you can use the usual old reflection or another IoC container:
public interface IEvent { } public interface IHandle<TEvent> where TEvent : IEvent { void Handle(TEvent e); } public static class EventBus { public static void RaiseEvent(TEvent e) where TEvent : IEvent { foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>()) handler.Handle(e); } }
Then you can create an event as follows:
public class Foo { public Foo() { EventBus.RaiseEvent(new FooCreatedEvent { Created = DateTime.UtcNow }); } } public class FooCreatedEvent : IEvent { public DateTime Created {get;set;} }
And process it (e.g. in your plugin):
public class FooCreatedEventHandler : IHandle<FooCreatedEvent> { public void Handle(FooCreatedEvent e) { Logger.Log("Foo created on " + e.Created.ToString()); } }
I would recommend this post to Shannon Deminic, which covers many of the problems with developing plug-in applications. This is what we used as the base for our own โplugin managerโ.
Personally, I would not download assemblies on demand. IMO, it is better to have a slightly longer launch time (even less problem in the web application) than users of the running application, who must wait for the necessary plug-ins to load.
Ben foster
source share