I'm trying to enhance my current established assemblies that use C# MEF. Since these assemblies are already used in production, modifying the individual classes directly is NOT a viable approach at the moment. Primarily I'm adding new behaviors to currently existing ones. For example I have:
public IExtension
{
Object Execute();
}
public BaseExtension : IExtension
{
// other methods and members
public virtual Object Execute()
{
// do operations here.
}
}
[Export(typeof(IExtension)]
public AppRecordExtension : BaseExtension
{
// .. other methods and members
public override Object Execute()
{
base.Execute(); // shown just for example..
this.someOperation();
}
}
// other extensions made.
Now the above works when the MEF container calls the extension in a driver's method:
[ImportMany(typeof(IExtension)]
private IEnumerable<Lazy<IExtension>> operations;
public void ExecuteExtensions()
{
var catalog = new AggregateCatalog( new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog("extensions", ".dll"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
Dictionary<IExtension, object> result = new Dictionary<IExtension, object>();
foreach(Lazy(IExtension> extension in operations)
{
result.Add((extension.Value, extension.Value.Execute());
}
}
However if I want to implement specific decorators for the IExtension or BaseExtension, I'm at a loss where I should put them in the container, or how I should put the attributes on the decorators so that all the original IExtension concrete classes are loaded and executed with the additional behaviors. An example of a IExtension decorator:
// do I put an attribute here?
// if an attribute is put here, how does the MEF container call it?
public BatchableExtension : BaseExtension
{
private IExtension extension = null;
public BatchableExtension( IExtension extension)
{
this.extension = extension;
}
public override Object Execute()
{
this.extension.Execute();
doSomeBatchSpecificOperation();
}
}
// do I put an attribute here?
// if an attribute is put here, how does the MEF container call it?
public MonitoringExtension : BaseExtension
{
private IExtension extension = null;
public MonitoringExtension( IExtension extension)
{
this.extension = extension;
}
public override Object Execute()
{
this.extension.Execute();
doSomeMonitoringSpecificOperation();
doSomeMoreBehaviors();
}
Can someone help out here? I want to make sure that when the container picks up the extensions, the new behaviors are picked up as well, depending on the passed parameters (e.g., if isBatchable = true, add BatchableExtension, etc). If it were non-MEF, the above would look something like:
public void Main(String[] args)
{
IExtension ext = new AppRecordExtension();
// this is the part where I want to simulate when I use MEF.
IExtension ext2 = new MonitoringExtension(new BatchableExtension(ext));
ext2.Execute();
}