GetExports

Jul 15, 2009 at 3:03 PM

I'm converting some code from Preview 4 to VS2010 beta 1. I don't find a replacement for many of the GetExports overloads, such as

// ExportCollection ret = _compositionContainer.GetExports(type, metadataViewType, constraint);

I find the ImportDefinition which can contain the constraint, but if I use this overload for GetExports, I do not see how to include the type and metadata type.

Thanks for any comments on this.

Kathleen

Jul 15, 2009 at 7:33 PM

The overloads of GetExports that took a constraint were removed for a couple of reasons:

  1. Users found them confusing because they didn't not automatically filter the contract name/identity based on specified 'type'. The constraint had to do this manually, leading to very ugly and convoluted queries.
  2. They added very little benefit over simply applying the same query over the returned Exports. This was especially true for the overload that took a strongly-typed metadata view - the constraint itself would have to query the raw metadata dictionary (hard-coding strings, etc), whereas, it was much easier to make a query on the returned exports by looking at the strongly-typed metadata view.

In your case, this would mean that the replacement would be something similar to:

        foreach (var export in container.GetExports(typeof(IFoo), typeof(IFooMetadata), "ContractName"))
        {
            IFooMetadata metadata = (IFooMetadata)export.Metadata;
            if (metadata.Name == "Foo")
            {

            }            
        }

Or if you are using the generic overloads:

        foreach (var export in container.GetExports<IFoo, IFooMetadata>("ContractName"))
        {
            IFooMetadata metadata = export.Metadata;
            if (metadata.Name == "Foo")
            {

            }
        }

Regards

David

 

 

Jul 16, 2009 at 2:46 PM

David,

Thanks for the explanation. That makes sense.

Kathleen