Open Generics and Contract Name

Jun 14, 2010 at 6:16 PM

Hello all,

I just downloaded the latest source from SVN, build the solution using Visual Studio 2010 RTM and all compiled fine.

However, I can't get open generics to work, even with the open generics test project.

 

From what I can see in the test, the contract name of the Export from the container's catalog is "IRepository()" instead of something like "IRepository(Order)" and this results in composition errors.

However, if I explicitly specify the type on the Import like this: [Import(typeof(IRepository<>))], it works. But this won't work I have two Imports of type IRepository<>.

 

Can someone please shed some light on this? Maybe I am missing something?

 

Regards.

Jul 12, 2010 at 7:02 PM

Hi, I'm having a similar issue: I attribute my export class like (DummyPartAttribute is a metadata attribute):

[Export(typeof(IDummy))]

[DummyPart(SomeValue="Hello")]

public class Dummy<T> : IDummy ...

If I then use a generic catalog to retrieve all the parts implementing IDummy:

var test = (from l in container.GetExports<IDummy, IDummyPartMetadata>()
               select l.Metadata.PartType).ToArray();

I can see that the Dummy<T> part does not appear at all in the exports. How can I solve this?

Coordinator
Jul 12, 2010 at 8:28 PM
Hi guys @fkode87, I will take a look. @Mathetes the code you are writing above won't work with the open generics implementation, as you can't query the container directly. It only works if you are importing open generic types on a part. Thanks Glenn
Oct 2, 2010 at 1:45 PM

Hi all,

What about solving a problem touched by fkode87...

I used the code from http://codebetter.com/blogs/glenn.block/archive/2009/08/20/open-generic-support-in-mef.aspx

and I had the same problem as  fkode87

Thanks in advance

 

Coordinator
Oct 2, 2010 at 5:55 PM

Hi,

Thank you for your feedback! We are aware of the issues and that their fixes are long overdue and for that I am sorry.

The reason is that we've been waiting for a contribution that has been delayed several times. The generics support gas been moved to this contriburion and the old one will be deprecated.

The good news is that the contribution was commited to the repository a couple of days ago.

Another team member is applying some changes to it and among those are the fixes for the reported bugs with generics. Eta is sometime during next week

Thanks!

On Oct 2, 2010 2:45 PM, "robertok" <notifications@codeplex.com> wrote:

From: robertok

Hi all,

What about solving a problem touched by fkode87...

I used the code from http://codebetter.com/blogs/glenn.block/archive/2009/08/20/open-generic-support-in-mef.aspx

and I had the same problem as  fkode87

Thanks in advance

 

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFContrib@discussions.codeplex.com)

To start a new discussion for this project, email MEFContrib@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

Oct 14, 2010 at 3:18 PM

Hi,

Thanks for info... well, bad news for us,... do you have any information about coming contribution?

 

Coordinator
Oct 14, 2010 at 7:09 PM

Robert,

It is our hope that there should only be minor changes required for you. The reason for this is that the new catalog, the interceptioncatalog, takes a set of stategies when you construct it

and one of the available ones is support for open generics. The way we plan on making the changes to the current catalog is to convert it into a decorator catalog instead of a fully

fledged catalog on its own. So there will still be a genricscatalog but internally it will use the interceptioncatalog with the open-generics catalog. There *might* be some api changes of

the catalog but there shouldn't be a need to start using an entirely new catalog.

I'm working on pushing some changes to our conventioncatalog and once those are done we will start looking at this, including fixing bugs. If you find any new bugs in our stuff please

visit http://mefcontrib.com and visit out support section, where you can report the issues.

 

Thanks!

Dec 14, 2010 at 7:02 PM

I'd like to use the Interceptor catalog for open generic resolution, which is mentioned as a possibilty, but I can't work out how to do it. Any documentaiton on how to do this?

Coordinator
Dec 14, 2010 at 7:49 PM
Edited Dec 14, 2010 at 7:58 PM

Hi everybody!

To all people using open-generics support in MefContrib: today we added full support for open generics! To get it, download the latest sources from github. What's new ? Besides exporting types with InheritedExport attribute, you can use regular export attribute as on the following samples:

1)

 

namespace MefContrib.Samples.Generics
{
    [Export]
    public class Repository<T> : IRepository<T>
    {
        public T Get(int id)
        {
            return ...
        }

        public void Save(T instance)
        {
            Console.WriteLine("Saving {0} instance.", instance.GetType().Name);
        }
    }

    public class Customer { }

    [Export]
    public class SomeOtherClass
    {
        [Import]
        public Repository<Customer> Repository { get; set; }
    }
}

 

2)

 

namespace MefContrib.Samples.Generics
{
    [Export(typeof(IGenericContractRegistry))]
    public class MyGenericContractRegistry : GenericContractRegistryBase
    {
        protected override void Initialize()
        {
            Register(typeof(IRepository<>), typeof(Repository<>));
        }
    }

    public interface IRepository<T>
    {
        T Get(int id);

        void Save(T instance);
    }

    [Export(typeof(IRepository<>))]
    public class Repository<T> : IRepository<T> where T : new()
    {
        public T Get(int id)
        {
            return default(T);
        }

        public void Save(T instance)
        {
            Console.WriteLine("Saving {0} instance.", instance.GetType().Name);
        }
    }

    public class Customer { }

    [Export]
    public class TestClassWhichImportsClosedGenerics
    {
        [Import]
        public IRepository<Customer> Repository { get; set; }
    }
}

 

Note that when specifying contract type other than the default one, you have to provide mapping between the target contract type and its implementation. Please see our samples located at https://github.com/MefContrib/MefContrib-Samples to get the idea of how to setup the container. There's the solution with interception samples, it also contains open generics demo as the implementation relays on the InteceptingCatalog. Okay, here are the two versions of a setup routine which enables open-generics support in MEF:

private static CompositionContainer CreateContainer1()
{
    // Create source catalog, note we are passing the registry part.
    // During the runtime, GenericExportHandler will query this catalog for
    // all types implementing IGenericContractRegistry
    var typeCatalog = new TypeCatalog(types..., typeof(MyGenericContractRegistry));

    // Create the interception configuration and add support for open generics
    var cfg = new InterceptionConfiguration()
        .AddHandler(new GenericExportHandler());

    // Create the InterceptingCatalog and pass the configuration
    var interceptingCatalog = new InterceptingCatalog(typeCatalog, cfg);

    // Create the container
    return new CompositionContainer(interceptingCatalog);
}

private static CompositionContainer CreateContainer2()
{
    // Create source catalog
    var typeCatalog = new TypeCatalog(types...);

    // Create catalog which supports open-generics, pass in the registry
    var genericCatalog = new GenericCatalog(new MyGenericContractRegistry());

    // Aggregate the both catalogs
    var aggregateCatalog = new AggregateCatalog(typeCatalog, genericCatalog);

    // Create the container
    return new CompositionContainer(aggregateCatalog);
}
Use anyone you like, they are equivalents =)

I am also in the middle of writing a blog post about open-generics support in MEF, so monitor http://pwlodek.blogspot.com/ from time to time =)

 

Regards,

Piotr

Coordinator
Dec 21, 2010 at 1:35 PM

You can read more about recent open-generics support in MEF at http://pwlodek.blogspot.com/2010/12/introduction-to-interceptingcatalog.html

 

Piotr