Importing and Executing a List of IExecutableCommands

11/11/2009 9:25 PM
You can subscribe to this wiki article using an RSS feed reader.
In order to import a list of commands, you need a reference to the Extension Service, and you will probably want a reference to the Logging Service as well, in case something goes wrong with the commands and you want to log the error.  Of course you also need to get a reference to the commands themselves.  Use an Import Attribute in a class that exports itself and is somehow already imported by SoapBox Core.  Here is an example:

[Import(SoapBox.Core.Services.Logging.LoggingService, typeof(ILoggingService))]
private ILoggingService logger { get; set; }

[Import(SoapBox.Core.Services.Host.ExtensionService, typeof(IExtensionService))]
private IExtensionService extensionService { get; set; }

[ImportMany(ExtensionPoints.MyProject.MyClass.CommandsToExecute,
            typeof(IExecutableCommand), AllowRecomposition=true)]
private IEnumerable<IExecutableCommand> commandsToExecute { get; set; }

private void ExecuteCommands()
{
    foreach(IExecutableCommand cmd in
extensionService.Sort(commandsToExecute))
    {
        try
        {
            cmd.Run();
        }
        catch (Exception e)
        {
            logger.Error("Exception while running command " + cmd.ID, e);
        }
    }
}


Tags:
Home: SoapBox Core What's new: Recently changed articles