PartExportsInheritedAttribute and Metadata behavior

Jul 7, 2009 at 10:46 PM
Edited Jul 7, 2009 at 10:48 PM

Hi again,

I was looking at a way to export certain types without having to mark them with the Export attribute. I found the different threads and blogs on creating custom programming models and even looked at the contrib project.

At one point I started playing around with ParthExportsInheritedAttribute since it would have allowed us to do almost what I wanted but without having to depart from the attributed model that was working quite well.

When doing my own tests I realized that metadata on subclasses wasn't being consumed. I then found the following in the MEF Preview 5 test code:

        [Export(typeof(BaseClassWithExportButNoMetadata))]
        [PartExportsInherited]
        public abstract class BaseClassWithExportButNoMetadata
        {
        }


        [ExportMetadata("Name", "FromDerivedType")]
        public class DerivedClassMetadataButNoExport : BaseClassWithExportButNoMetadata
        {
        }

        [TestMethod]
        public void Metadata_BaseClassWithExportButNoMetadata()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(
                typeof(BaseClassWithExportButNoMetadata),
                typeof(DerivedClassMetadataButNoExport));

            var export = container.GetExport<BaseClassWithExportButNoMetadata>();

            Assert.IsFalse(export.Metadata.ContainsKey("Name"), "Export should only contain metadata from the base!");
        }

I found that rather counter-intuitive. In our case, the type exported with the PartExportsInheritedAttribute is an abstract base class, and the different implementation are all marked with a series of attributes that are themselves marked ExportMetadata. I was naturally expecting to be able to have all that metadata available, but I guess MEF is not going to provide that.

What is the rational behind that?

And also, is there any other way to define exports through custom attributes without implementing a new model? (probably I'm missing something, but I couldn't see how implementing an ExportProvider would help me either)


Thanks,

K

 

Jul 8, 2009 at 3:36 AM
Edited Jul 8, 2009 at 3:48 AM

Hi K

Yes, in our current bits we ignore free-floating ExportMetadata that is not attached to an export. There were serveral reasons why we do not see loose export metadata that is not connected to an export, but the primary has to do with it being a performance optimization to only look at metadata if an export is present.

However, we are providing In our next preview which is shipping ANY day a solution to this problem. We are removing PartExportsInheritedAttribute and providing a new InheritedExport attribute. You can derive from InheritedExport and create your own InheritedExport which specifies a specific contract. You can also decorate that attribute with a MetadataAttribute and add additional properties to your Export which carry metadata. A deriver of a part decorated with that attribute, can then another instance of that attribute, and specify new meatdata. The new instance of the attribute will not however result in a duplicate export, instance it will replace the export in the base.

So for example:

[MetadataAttribute]
public class Plugin : InheritedExport 
{
  public Plugin() : 
    base(typeof(IPlugin))
  {
  }

  public string Category {get;set;}
  public string Name {get;set;]
}

[Plugin] //exports IPlugin
public class BasePart : IPlugin {
}

[Plugin(Category="SomeCategory", Name="DerivedPart"]
public class DerivedPart : BasePart {
}

DerivedPart will have a single Export of IPlugin which contains metadata for Category and Name.

HTH

Glenn

Jul 8, 2009 at 5:20 PM

Hey Glen,

This looks great! The InheritedExport works exactly how I was hoping it would work. I know there was another thread asking for why Export was sealed, and I think this is a nice way to work it out.

I will be marking the ANY date on my calendar ;)

Thanks again,

Kane

Jul 8, 2009 at 5:43 PM

Hi Glen,

Is this upcoming preview release being planed to make it into the 4.0 framework release?


Thanks,

K

Jul 8, 2009 at 5:55 PM

We don’t make guarantees about direct alignment between the CodePlex and Framework versions, since the CodePlex version (in addition to targeting the 3.5 runtime) will likely continue to evolve post-.NET 4.

The Preview 6 release however matches up with the current .NET 4 plans very closely.

Nick

From: ctzkane [mailto:notifications@codeplex.com]
Sent: Wednesday, July 08, 2009 10:43 AM
To: Nicholas Blumhardt
Subject: Re: PartExportsInheritedAttribute and Metadata behavior [MEF:61785]

From: ctzkane

Hi Glen,

Is this upcoming preview release being planed to make it into the 4.0 framework release?


Thanks,

K

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com