Intro
App that I am working on uses licenses that are separate project in the main solution. There is separate application that generates license files. So far, the approach was to build the said main solution and copy licenses.dll to license generator app.
Since the main app is delivered to numerous clients the versions they use may vary thus it is required to also maintain several instances of license generator app. My idea was to use MEF to switch licenses.dll in runtime and actually to keep just one instance of the generator. I was hoping for as little effort as possible (especially that I don't want to put too much work in all the old versions), i.e. just decorate class with Export annotation in all the versions and do some more extensive work in the license generator and it should be fine.
Irrelevant part
I am however slightly bamboozled now with figuring out which of the loaded instances of my LicenseNodes class is which.
The initial approach was obviously:
[Export("LicenseNodes")]
public class LicenseNodes {
[stuff here...]
}
[ImportMany("LicenseNodes")]
private IEnumerable<Lazy<dynamic>> _licenseNodes;
private void LoadLicenses()
{
var catalog = new AggregateCatalog();
var dirs = Directory.GetDirectories(licensesDir); //here are folders each of which
//contains different version
foreach (var dir in dirs)
{
catalog.Catalogs.Add(new DirectoryCatalog(dir));
}
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
But this produces just a collection of objects with no distinction to differentiate between them.
Actual issue
Then I figured out metadata should be the way. So:
[Export("LicenseNodes", ExportMetadata("Version", "1.0.0"))
public class LicenseNodes
Of course i produced few of these with different versions.
And respectively:
[ImportMany("LicenseNodes")]
private IEnumerable<Lazy<dynamic, Dictionary<string, object>>> _licenseNodes;
And:
foreach (var node in _licenseNodes)
{
if (node.Metadata["Version"] == "7.7.0")
{
currentNodes = node.Value;
}
}
The thing is, that all the Metadata for all of the instances has the same value loaded from the first occurrence.
I tried also with the interface instead of dictionary to no avail.
What am I missing?