Multiple Export Contracts?

Mar 20, 2009 at 7:54 AM
Edited Mar 20, 2009 at 7:58 AM
I have another silly idea ;-)

What would you have to do in MEF to have one 'Export instance' export multiple contracts. I was looking at the UnityServiceLocatorAdapter in CAL and it implements 2 interfaces, the IServiceLocator and the (System.)IServiceProvider interface. I recognized the benifit 'cause I have 'legacy' code that also uses IServiceProvider. So that was the 'inspiration' but how would you do something like that in MEF?

Would this make sense?

[Export(typeof(IMyInterface1))]
[Export(typeof(IMyInterface2))]
public class MyProvider : IMyInterface1, IMyInterface2
{
    //......
}

I don't think MEF (preview4) supports this, but it seems very logical from a 'user' perspective, doesn't it?
Mar 20, 2009 at 8:19 AM
Well you're right! MEF does not support that. Best you can get are two separate instances that are Shared by using CompositionOptions. But you can't get only one instance with two contracts.

I'm sure you could do that with a custom Export provider though. Not sure how.

M.

On Fri, Mar 20, 2009 at 7:54 AM, obiwanjacobi <notifications@codeplex.com> wrote:

From: obiwanjacobi

I have another silly idea ;-)

What would you have to do in MEF to have one 'Export instance' export multiple contracts. I was looking at the UnityServiceLocatorAdapter in CAL and it implements 2 interfaces, the IServiceLocator and the (System.)IServiceProvider. I recognized the benifit 'cause I have 'legacy' code that also uses IServiceProvider. So that was the 'inspiration' but how would you do something like that in MEF?

Would this make sense?

[Export(typeof(IMyInterface1))]
[Export(typeof(IMyInterface2))]
public class MyProvider : IMyInterface1, IMyInterface2
{
    //......
}

I don't think MEF (preview4) supports this, but it seems very logical from a 'user' perspective, doesn't it?

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




--
Magnus Mårtensson
Senior Consultant - Scrum Master - MCSD, MCTS
Dotway AB

Tel: +46 (768) 51 00 36

http://blog.noop.se/
Mar 20, 2009 at 4:52 PM
Actually MEF does support this. CreationPolicy applies to the PART not the export. If you mark the part as shared, then exports are all pulled from the same part instance. At least that is thew way it is supposed to work.

The reasoning that CAL's UnityServiceLocator adapter implements both interfaces, is because the underlying CommonServiceLocator implements IServiceProvider and IServiceLocator. This was a decision we made when we designed CSL in order to support legacy code / code that uses the System.ComponentModel.IServiceProvider interface.

Mar 20, 2009 at 5:12 PM
Hey I learned something today!

Here is the unit test that prooves the case:

public interface IBaz
{ }

public interface IBax
{ }

[Export(typeof(IBaz)),
 Export(typeof(IBax)),
 CompositionOptions(CreationPolicy = CreationPolicy.Shared)]
public class TheBaz : IBaz, IBax
{ }

[Import]
public IBaz Baz { get; set; }

[Import]
public IBax Bax { get; set; }

[TestMethod]
public void DiffrentExportsOnSharedPartAreSame()
{
    Assert.AreSame(Baz, Bax);
}

Thanks Glenn!

M.

On Fri, Mar 20, 2009 at 4:52 PM, gblock <notifications@codeplex.com> wrote:

From: gblock

Actually MEF does support this. CreationPolicy applies to the PART not the export. If you mark the part as shared, then exports are all pulled from the same part instance. At least that is thew way it is supposed to work.

The reasoning that CAL's UnityServiceLocator adapter implements both interfaces, is because the underlying CommonServiceLocator implements IServiceProvider and IServiceLocator. This was a decision we made when we designed CSL in order to support legacy code / code that uses the System.ComponentModel.IServiceProvider interface.

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




--
Magnus Mårtensson
Senior Consultant - Scrum Master - MCSD, MCTS
Dotway AB

Tel: +46 (768) 51 00 36

http://blog.noop.se/