-
-
Save schotime/1576721 to your computer and use it in GitHub Desktop.
public interface IActivity | |
{ | |
void Run(); | |
bool Matches(int i); | |
} | |
public class PluginRunner | |
{ | |
private readonly IContainer _container; | |
private readonly ILogger _logger; | |
public PluginRunner(IContainer container, ILogger logger) | |
{ | |
_container = container; | |
_logger = logger; | |
} | |
public void Run() | |
{ | |
//var i = 0; | |
foreach (var activity in GetActivities().Where(activity => activity.Matches(1))) | |
{ | |
activity.Run(); | |
_logger.SetDebug(); | |
} | |
} | |
private IEnumerable<IActivity> GetActivities() | |
{ | |
if (Directory.Exists("plugins")) | |
{ | |
_container.Configure(x => x.Scan(y => | |
{ | |
y.AssembliesFromPath("plugins"); | |
y.AddAllTypesOf<IActivity>(); | |
})); | |
} | |
return _container.GetAllInstances<IActivity>(); | |
} | |
} |
Watch the Directory.Exists and AssembliesFromPath business. I'd use FubuCore's filesystem stuff to help the ease the IO pain there
Does this have to be runtime? You can't do this all at startup?
Its not a nested container...its the same container from startup. What are the benefits of a nested container here?
Its a console application/windows service that needs to stay alive and find activities if they have been dropped in the folder.
Just making sure that it wasn't a nested container. You might then consider keeping track of what assemblies have already been loaded and only load the new ones.
Is that just a performance thing, or is there something else?
Mostly performance. Maybe something more like "find the activities from these assemblies". As it stands right now, every plugin you have is going to rerun whenever a new assembly is dropped
It still needs to scan all of them each time to find out which one is needed to be run.
Maybe you can change the configuration of your container to operate off of the new assemblies. So rather than a x.AssembliesFromPath(), you can pass in each new assembly so the scan operation is doing something more...let me fork this and show you.
Then something like this?
https://gist.github.com/1577402
Is this a nested container that you're operating in? If not, you need to make the GetActivities idempotent (don't keep configuring the container unless you need to)