Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utilising methods available in plugin1 in plugin2 via MEF

Tags:

c#

.net

mef

I have a console based MEF application the host (CompositionContainer) of which loads available plugin assemblies based on the command line parameter, for example:

app.exe plugin1

will load the host (app.exe) and plugin1. The VS solution is structured such that each plugin has it's own project (hence it's own assembly).

There exists a collection of plugins, some of which have opportunity for re-use of code. So for example plugin1 has a method CopyFiles(string fileName) and that same functionality is to be utilised by plugin2.

Now, in a traditional console application one would add a reference to plugin1.dll and by using that namespace go about it's business of re-use.

I am curious if there is a "MEF" way, if you will, of implementing this re-use. So, something along the lines of creating an object of plugin1 in plugin2 by adding an Import attribute to plugin1's exportable interface and letting MEF take care of the rest. Is that possible? And if so, how?

Or is there a better approach that one should take when designing such applications? Bonus voodoo for any pointers to useful learning resources and explanations.

Thanks!

like image 977
aateeque Avatar asked Oct 06 '11 17:10

aateeque


1 Answers

If the method you want to share is in plugin1, you would Export plugin1. You can also Export a method if you just wanted to share that method.

plugin2 would Import plugin1. MEF would take care of the rest. If you are getting your assemblies from the command line and creating an AssemblyCatalog for plugin1, then you will also need to tell it about plugin2 and get that into an AggregateCatalog, or put them all in a directory and use a DirectoryCatalog (you could pass the directory name on the command line).

The MEF Programming Guide covers similar scenarios.

There are also Hands-On-Labs for MEF in the Visual Studio 2010 and .NET Framework 4 Training Kit which are helpful.

like image 118
Jim Counts Avatar answered Nov 07 '22 17:11

Jim Counts