Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: Strongly typed metadata

$
0
0

I think I've worked it out after reading this SO question.

I created a Metadata Attribute:

[MetadataAttribute]
publicclass ExtensionMetadataAttribute : ExportAttribute, IExtensionMetadata
{
	publicint ID { get; set; }

	public ExtensionMetadataAttribute(int id) : base(typeof (IExtension))
	{
		ID = id;
	}
}

Then modified ExtensionA.cs:

[ExtensionMetadata(1)]
publicclass ExtensionA : IExtension
{
	publicvoid DoWork()
	{
		System.Diagnostics.Debug.WriteLine("ExtensionA doing work..");
	}
}

And now ExtensionManager.cs looks like this:

publicclass ExtensionManager : IExtensionManager
{
	privatereadonly IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> _extensions;

	public ExtensionManager(IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> extensions)
	{
		_extensions = extensions;
	}

	publicvoid DoWork(int extensionID)
	{
		foreach (var extension in _extensions)
		{
			if (extension.Metadata.ID == extensionID)
			{
				using (var foo = extension.CreateExport())
				{
					foo.Value.DoWork();
				}
			}
		}
	}
}

This seems to do the trick, but I would still be interested in your feed back re best practices, performance issues etc.

Thanks!


Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>