Use defined list in export

Jun 23, 2009 at 2:55 PM

let say I have a catalog class defined like that:

class Catalog
{
   [Import(typeof(ISource)]
   public IEnumerable<ISource> Sources { get; private set; }

and now I have 2 place where I can find source. A general purpose FileSource class and a ServiceSource class which can generate many Source based on an Id:

[Export(typeof(ISource))]
public class FileSource : ISource
{.....}

public ServiceSource
{
.....
   public IEnumerable<ISource> GetDynamicSources();

How do I merge all sources together in my catalog?
if I try to tag the method with an export like that:

[Export(typef(ISource))] 
public IEnumerable<ISource> GetDynamicSources(); 

it throw an error when Compose(0 is called.

How do I go about solving such a scenario?

Jun 23, 2009 at 5:58 PM

Currently there is no ‘ExportMany’ feature.

The most direct workaround in your scenario is to add the individual components to the container as exports:

var batch = new CompositionBatch();

foreach (var source in serviceSource.GetSources())

  batch.AddExportedObject(source);

container.Compose(batch);

Alternatively, and in a more MEF-like fashion, you can use a combination of [ImportMany], lazy instantiation and metadata to achieve the same result in many scenarios. Are the ISource implementations separate classes, and is there anything you can attach to them at compile time that will allow the correct sources to be selected?

Nick

From: superlloyd [mailto:notifications@codeplex.com]
Sent: Tuesday, June 23, 2009 6:56 AM
To: Nicholas Blumhardt
Subject: Use defined list in export [MEF:60385]

From: superlloyd

let say I have a catalog class defined like that:

class Catalog
{
[Import(typeof(ISource)]
public IEnumerable<ISource> Sources { get; private set; }
}

and now I have 2 place where I can find source. A general purpose FileSource class and a ServiceSource class which can generate many Source based on an Id:

[Export(typeof(ISource))]
public class FileSource : ISource
{.....}

public ServiceSource
{
.....
public IEnumerable<ISource> GetDynamicSources();
}

How do I merge all sources together in my catalog?
if I try to tag the method with an export like that:

[Export(typef(ISource))]
public IEnumerable<ISource> GetDynamicSources();

it throw an error when Compose(0 is called.

How do I go about solving such a scenario?

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

Jun 24, 2009 at 12:38 AM

Hi,

Thanks for your answer!

I did not know ImportMany, I have to look into it! (I'm new to MEF)

Actually I did find a nice work around.

I am not exporting ISource(s) but ISourceManager(s) which have one property, an enumeration of ISource.

Anyway, in this particular case, I don't think I can use ImportMany.

My scenario is source reprent a "data reporitoy", you got one MemoryRepository class and instance to store data in memory.
You got one FileSystemRepository class and instance to access all file as stream.

And you got one class StructureFileRepository with many instance to represent all the opened strutured file content. One instance per file. Of the same class.

How would you go about that?

 

Jun 25, 2009 at 7:16 PM

For MEF it seems like the path you’re on – ‘source managers’ as exports – is probably the right one. It’s hard without deep insight into your project to be more specific than that.

Cheers,


Nick

From: superlloyd [mailto:notifications@codeplex.com]
Sent: Tuesday, June 23, 2009 4:38 PM
To: Nicholas Blumhardt
Subject: Re: Use defined list in export [MEF:60385]

From: superlloyd

Hi,

Thanks for your answer!

I did not know ImportMany, I have to look into it! (I'm new to MEF)

Actually I did find a nice work around.

I am not exporting ISource(s) but ISourceManager(s) which have one property, an enumeration of ISource.

Anyway, in this particular case, I don't think I can use ImportMany.

My scenario is source reprent a "data reporitoy", you got one MemoryRepository class and instance to store data in memory.
You got one FileSystemRepository class and instance to access all file as stream.

And you got one class StructureFileRepository with many instance to represent all the opened strutured file content. One instance per file. Of the same class.

How would you go about that?

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

Jun 26, 2009 at 12:38 AM

Since then I got a better understanding of MEF! :)

MEF is Cool! :)

And, indeed, "SourceManager" is the way to go for me. And it's no big deal! :)