Skip to content

Instantly share code, notes, and snippets.

@schotime
Created January 8, 2012 01:16
Show Gist options
  • Save schotime/1576721 to your computer and use it in GitHub Desktop.
Save schotime/1576721 to your computer and use it in GitHub Desktop.
Plugin Runner
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>();
}
}
@jmarnold
Copy link

jmarnold commented Jan 8, 2012

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)

@jmarnold
Copy link

jmarnold commented Jan 8, 2012

Watch the Directory.Exists and AssembliesFromPath business. I'd use FubuCore's filesystem stuff to help the ease the IO pain there

@jmarnold
Copy link

jmarnold commented Jan 8, 2012

Does this have to be runtime? You can't do this all at startup?

@schotime
Copy link
Author

schotime commented Jan 8, 2012

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.

@jmarnold
Copy link

jmarnold commented Jan 8, 2012

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.

@schotime
Copy link
Author

schotime commented Jan 8, 2012

Is that just a performance thing, or is there something else?

@jmarnold
Copy link

jmarnold commented Jan 8, 2012

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

@schotime
Copy link
Author

schotime commented Jan 8, 2012

It still needs to scan all of them each time to find out which one is needed to be run.

@jmarnold
Copy link

jmarnold commented Jan 8, 2012

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.

@jmarnold
Copy link

jmarnold commented Jan 8, 2012

@schotime
Copy link
Author

schotime commented Jan 8, 2012

Then something like this?
https://gist.github.com/1577402

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment