You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
api-approvedAPI was approved in API review, it can be implementedarea-System.CompositionenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors
MEF1 has method ComposeExportedValue<T>(T exportedValue). MEF2 should have a similar functionality in System.Composition.Hosting.ConfigurationContainer (as discussed in #18624 and #15362)
Suggested API
public ContainerConfiguration WithInstance<TExport> (TExport instance);
public ContainerConfiguration WithInstance<TExport> (string contractName, TExport instance);
public ContainerConfiguration WithInstance (Type t, object instance);
public ContainerConfiguration WithInstance (Type t, string contractName, object instance);
ContainerConfiguration containerConfig = new ContainerConfiguration()
.WithAssembly(GetType().Assembly)
.WithInstance<IExample>(new Example());
var container = containerConfig.CreateContainer();
var example = container.GetExport<IExample>();
I'v reviewed some of the MEF2 stuff but I didn't code it. I did however code the MEF1 stuff many years ago and from what I know of MEF2 the APIs seems like they would be reasonable but I would suggest someone doing a proof of concept implementation of them before formally accepting the API proposal. The reason for that is to ensure that these fit into the model that MEF2 uses.
This is a pretty straightforward addition; I wrote and included it as an extension WithExport() in the original MEF2 Code drop, along with another useful one, WithFactoryDelegate():
var config = new ContainerConfiguration()
.WithExport<TextWriter>(Console.Out)
.WithFactoryDelegate<string>(() => DateTime.Now.ToString(), isShared: true)
.WithPart<Program>();
The terminology WithExport() could be the some bikeshedding but as far as I remember it's the most accurate WRT the underlying model.
And the InstanceExportDescriptorProvider itself is pretty straightforward:
// we're just aiming to show the mechanics here.
class InstanceExportDescriptorProvider : SinglePartExportDescriptorProvider
{
object _exportedInstance;
public InstanceExportDescriptorProvider(object exportedInstance, Type contractType, string contractName, IDictionary<string, object> metadata)
: base (contractType, contractName, metadata)
{
if (exportedInstance == null) throw new ArgumentNullException("exportedInstance");
_exportedInstance = exportedInstance;
}
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
{
if (IsSupportedContract(contract))
yield return new ExportDescriptorPromise(contract, _exportedInstance.ToString(), true, NoDependencies, _ =>
ExportDescriptor.Create((c, o) => _exportedInstance, Metadata));
}
}
Since it's all based on the public API/extension points, you can drop this in today @gthvidsten and it should "just work".
It'd be wonderful to see this added to the official package, or included in the docs somewhere, if anyone picks it up (it still pains me that there's so much great stuff buried in this little project).
As the person who originally opened #15362 back in 2015, I'm glad to see this finally make it into the official packages. It drastically simplifies migration from MEF1 to MEF2 for people who don't need the flexibility of MEF1 and want to avoid the potentially higher performance cost. Refactoring a project to use plugins is a lot easier when you don't have to fully rearchitect to fit design assumptions and can just export an existing instance.
I'll be glad to review my legacy projects and adjust for this. Might even have time to swap out the old handwritten context classes for AssemblyLoadContext.
Reacted by Nicholas Blumhardt and Tarek Mahmoud Sayed
Reacted by Nicholas Blumhardt
ghost locked as resolved and limited conversation to collaborators on May 14, 2022
api-approvedAPI was approved in API review, it can be implementedarea-System.CompositionenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors
Activity
danmoseley commentedon Apr 29, 2019
Unfortunately we have no real expertise in MEF remaining on the team. @weshaggard did you work on this code?
weshaggard commentedon Apr 29, 2019
I'v reviewed some of the MEF2 stuff but I didn't code it. I did however code the MEF1 stuff many years ago and from what I know of MEF2 the APIs seems like they would be reasonable but I would suggest someone doing a proof of concept implementation of them before formally accepting the API proposal. The reason for that is to ensure that these fit into the model that MEF2 uses.
danmoseley commentedon Apr 29, 2019
Thanks @weshaggard . Does anyone around have knowledge of MEF2 specifically?
weshaggard commentedon May 6, 2019
I doubt there is anyone that has any recent knowledge but @nblumhardt wrote it originally and might have some memory of the code.
nblumhardt commentedon May 7, 2019
Hey @danmosemsft @weshaggard @gthvidsten !
This is a pretty straightforward addition; I wrote and included it as an extension
WithExport()in the original MEF2 Code drop, along with another useful one,WithFactoryDelegate():The terminology
WithExport()could be the some bikeshedding but as far as I remember it's the most accurate WRT the underlying model.The demo project is here: https://github.com/MicrosoftArchive/mef/blob/master/oob/demo/Microsoft.Composition.Demos.ExtendedPartTypes/Program.cs#L24
The extension method
WithExport()just adds anInstanceExportDescriptorProviderto the container:And the
InstanceExportDescriptorProvideritself is pretty straightforward:Since it's all based on the public API/extension points, you can drop this in today @gthvidsten and it should "just work".
It'd be wonderful to see this added to the official package, or included in the docs somewhere, if anyone picks it up (it still pains me that there's so much great stuff buried in this little project).
bryanchen-d commentedon May 8, 2019
+1 vote for this feature.
ericstj commentedon Jul 9, 2020
Thanks for the input @nblumhardt, I've gone ahead and marked this one ready-for-review. Cheers!
terrajobst commentedon Jul 9, 2020
I'd go with @nblumhardt's proposal, slightly adjusted:
bartonjs commentedon Jul 9, 2020
Video
After some discussion on the name of the methods, the use of defaults parameters, and the name of the generic, it's approved as
We also think it would be valueable to add an analyzer that warns when generic inference was used. (e.g.
WithExport(new SomeExportedType()))5 remaining items
danmoseley commentedon Jul 31, 2020
Setting milestone to indicate this is not a required part of the 5.0 product.
ZigMeowNyan commentedon Apr 14, 2022
Should also close issue #27324.
As the person who originally opened #15362 back in 2015, I'm glad to see this finally make it into the official packages. It drastically simplifies migration from MEF1 to MEF2 for people who don't need the flexibility of MEF1 and want to avoid the potentially higher performance cost. Refactoring a project to use plugins is a lot easier when you don't have to fully rearchitect to fit design assumptions and can just export an existing instance.
I'll be glad to review my legacy projects and adjust for this. Might even have time to swap out the old handwritten context classes for
AssemblyLoadContext.