Skip to content

Instantly share code, notes, and snippets.

@mdissel
Created November 20, 2012 16:34
Show Gist options
  • Save mdissel/4119071 to your computer and use it in GitHub Desktop.
Save mdissel/4119071 to your computer and use it in GitHub Desktop.
Register PluginFamily with structuremap
[Test]
public void PluginTest() {
using (IContainer container = new Container()) {
container.Configure(x => {
x.Scan(y => {
y.TheCallingAssembly();
y.Convention<PluginFamilyScanner>();
});
});
container.AssertConfigurationIsValid();
IFoo foo2 = container.GetInstance<IFoo>();
Assert.That(foo2.GetType().Equals(typeof(Foo2)));
IEnumerable<IFoo> foos = container.GetAllInstances<IFoo>();
Assert.That(foos.Count(), Is.EqualTo(2));
}
}
[PluginFamily()]
public interface IFoo {}
public class Foo : IFoo {}
public class Foo2 : IFoo {}
public class PluginFamilyScanner : IRegistrationConvention {
public void Process(Type type, Registry registry) {
if (!type.IsConcrete()) return;
foreach (Type iType in type.GetInterfaces()) {
if (iType == null || !PluginFamilyAttribute.MarkedAsPluginFamily(iType))
return;
if (PluginCache.GetPlugin(iType) != null) {
if (type.CanBeCastTo(iType) && Constructor.HasConstructors(type)) {
registry.For(iType).Use(type);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment