Simple IoC container for a small plug-in system

I am developing a simple plugin structure a for a .NET 3.5 application (WinForms).

Our current application should start supporting dynamic loading and “plug in” various “plugins” / “extensions” that are unknown to the application at compile time.

These extensions will be “connected” to various areas of the application, for example, as event handlers for certain classes.

For example (simplified):

public class SomeSystem
{
     public event Action<string> Completed;

     public event Action<string> Failed;

     public event Action<string> Stopped;
}

One use case that I would like to have is for developers to be able to define handlers for such events in the plugin assembly, not knowing that the application knows about them.

, IoC .

IoC ? ?

, IoC ? (, , , ).

, :

  • , Register ?
  • IoC ? ( ?)
  • IoC?
+5
2

, MEF. , . (ComposableParts, Exports ..) , .

, Register ?

MEF . , : " , X".

IoC ? ( ?)

, MEF, , . DLL, , GAC - . ( )

IoC

MEF .

+5

.

"" "" / "", .

, , , .

, , , :

class ApplicationMenu
{
    // The "File" menu
    IMenuItem File { get; }
}

interface IMenuRegistrar
{
    void Register(ApplicationMenu menu);
}

, :

[Component]
public class CoolPluginMenuRegistrar : IMenuRegistrar
{
    public void Register(ApplicationMenu menu)
    {
        menu.File.Add("mnuMyPluginMenuName", "Load jokes");
    }
}

[Component] , .

, , , :

public class Program
{
    public static void Main(string[] args)
    {
        var registrar = new ContainerRegistrar();
        registrar.RegisterComponents(Lifetime.Transient, Environment.CurrentDirectory, "MyApp.Plugin.*.dll");
        var container = registrar.Build();

        // all extension points have been loaded. To load all menu extensions simply do something like:

        var menu = GetMainMenu();
        foreach (var registrar in container.ResolveAll<IMenuRegistrar>())
        {
            registrar.Register(menu);
        }
    }
}

"" , , . , IoC .

. .

IoC ? ?

. . ( .NET ). , :

[Component]
public class ReplyEmailNotification : ISubscriberOf<ReplyPosted>
{
    ISmtpClient _client;
    IUserQueries _userQueries;

    public ReplyEmailNotification(ISmtpClient client, IUserQueries userQueries)
    {
        _client = client;
        _userQueries = userQueries;
    }

    public void Invoke(ReplyPosted e)
    {
        var user = _userQueries.Get(e.PosterId);
        _client.Send(new MailMessage(user.Email, "bla bla"));
    }
} 

:

DomainEvent.Publish(new ReplyPosted(user.Id, "This is a subject"));

, :

  • ([Component] )
  • ISubscriberOf<T>

, Register ?

. , .

IoC ? ( ?)

. .

IoC?

: http://www.codeproject.com/Articles/440665/Having-fun-with-Griffin-Container

0

All Articles