Can recomposition handle string imports?

Jul 29, 2009 at 2:43 PM

I'm having difficulty understanding if/how recomposition can be made to work with single string imports. I'd originally thought recomposition was only for dealing with new assemblies being added at runtime, but then http://codebetter.com/blogs/glenn.block/archive/2009/03/21/recomposition-and-constructors.aspx mentioned that "The change does not only have to be due to new assemblies appearing, it could due to contextual changes in the application such as a user updating their profile, or the application moving into a different state."

So, I naively tried creating the following classes:

[Export(typeof(ProviderOfRecomposableExport))]
public class ProviderOfRecomposableExport
{
	[ImportingConstructor]
	public ProviderOfRecomposableExport()
	{
		Value = "Hello";
	}

	[Export]
	public string Value { get; set; }
}

[Export(typeof(ConsumerOfRecomposableImport))]
public class ConsumerOfRecomposableImport
{
	[Import(AllowRecomposition = true)]
	public string RecomposableValue { get; set; }
}

and using them as follows:

[Test]
public void Recomposition()
{
	var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
	var container = new CompositionContainer(catalog);
	var batch = new CompositionBatch();
	container.Compose(batch);

	var provider = container.GetExportedValue<ProviderOfRecomposableExport>();
	var consumer = container.GetExportedValue<ConsumerOfRecomposableImport>();

	Assert.AreEqual(provider.Value, consumer.RecomposableValue);

	provider.Value = "Goodbye";

	Assert.AreEqual(provider.Value, consumer.RecomposableValue);
}

This fails at the final assert, as consumer.RecomposableValue is still "Hello". Do I need to do something to the catalog to make it "recomposition-aware"?

Thanks

Akash

Jul 29, 2009 at 7:04 PM

Hi,

Recomposition only kicks in when parts (supplying exports) are added or removed.

Recomposition doesn’t have any effect when the values of the exports themselves change.

You still need to use standard mechanisms like INotifyPropertyChanged if you want to transmit changes to export values.

Hope this clears things up!

Nick

From: akashchopra [mailto:notifications@codeplex.com]
Sent: Wednesday, July 29, 2009 7:44 AM
To: Nicholas Blumhardt
Subject: Can recomposition handle string imports? [MEF:63874]

From: akashchopra

I'm having difficulty understanding if/how recomposition can be made to work with single string imports. I'd originally thought recomposition was only for dealing with new assemblies being added at runtime, but then http://codebetter.com/blogs/glenn.block/archive/2009/03/21/recomposition-and-constructors.aspx mentioned that "The change does not only have to be due to new assemblies appearing, it could due to contextual changes in the application such as a user updating their profile, or the application moving into a different state."

So, I naively tried creating the following classes:

[Export(typeof(ProviderOfRecomposableExport))]
public class ProviderOfRecomposableExport
{
        [ImportingConstructor]
        public ProviderOfRecomposableExport()
        {
               Value = "Hello";
        }
 
        [Export]
        public string Value { get; set; }
}
 
[Export(typeof(ConsumerOfRecomposableImport))]
public class ConsumerOfRecomposableImport
{
        [Import(AllowRecomposition = true)]
        public string RecomposableValue { get; set; }
}

and using them as follows:

[Test]
public void Recomposition()
{
        var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
        var container = new CompositionContainer(catalog);
        var batch = new CompositionBatch();
        container.Compose(batch);
 
        var provider = container.GetExportedValue<ProviderOfRecomposableExport>();
        var consumer = container.GetExportedValue<ConsumerOfRecomposableImport>();
 
        Assert.AreEqual(provider.Value, consumer.RecomposableValue);
 
        provider.Value = "Goodbye";
 
        Assert.AreEqual(provider.Value, consumer.RecomposableValue);
}

This fails at the final assert, as consumer.RecomposableValue is still "Hello". Do I need to do something to the catalog to make it "recomposition-aware"?

Thanks

Akash

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

Jul 29, 2009 at 7:51 PM
Edited Jul 29, 2009 at 7:53 PM

Thanks Nick, that makes it perfectly clear. I'll probably just export/import a Func<string>, even though it will be slightly more awkward than a simple string property when the classes are used outside of MEF.