Skip to content

Instantly share code, notes, and snippets.

@XSockets
Last active December 26, 2015 05:39
Show Gist options
  • Save XSockets/7101845 to your computer and use it in GitHub Desktop.
Save XSockets/7101845 to your computer and use it in GitHub Desktop.
Example of XSockets.Pluginframework - The idea here is that we can get hold of plugins without any configuration. By having export/import attributes on our interfaces we only have to implement a interface to get the plugin functionality. So we only need a reference to the interfaces and the pluginframework.... The actual plugins may be assemblie…
//The interfaces export the correct inteface, but can also import interfaces on properties.
//The implementing class does not need to do any imports/exports. It will be taken care of....
[Export(typeof(IZoo))]
public interface IZoo
{
[ImportMany(typeof(IAnimal))]
IList<IAnimal> Animals { get; set; }
}
[Export(typeof(IAnimal))]
public interface IAnimal
{
string Say();
}
//Note, this assembly is not referenced by any other project...
//Early Interface Binding
//Note that the plugins does not know of anything but the interfaces assembly.
//They will still be exported since the interfaces has import/export attributes.
public class Lion : IAnimal
{
public string Say()
{
return "Grr";
}
}
public class Donkey : IAnimal
{
public string Say()
{
return "Yeeehaaa";
}
}
public class Zoo : IZoo
{
public IList<IAnimal> Animals { get; set; }
}
//Note: The program will only know of the ImportExportInterfaces.dll at compile-time...
//The plugins (Plugins.dll and others) will be located dynamically by configuration or even a filesystemwatcher etc in runtime
//Accuire one instance of the IZoo interface... If we hade several zoo´s (or were unsure of it) we could use GetExports<T>
var zoo = Composable.GetExport<IZoo>();
//The Zoo will now automatically contain a list of the exported animals Donkey and Lion.
foreach (var animal in zoo.Animals)
{
Console.WriteLine(animal.Say());
}
//Output:
Grr
Yeeehaaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment