Generics

May 12, 2011 at 5:10 PM

Hi,

I've gone through the samples and read the documentation but I'm still a little confused about whether something like this is possible:

    [Export(typeof(IRepository<>))]
    public class SomeClass : IRepository<Customer>
    {
        public String SomeProperty { get; set; }

        public Customer Get(Int32 id)
        {
            return new Customer();
        }

        public void Save(Customer c)
        {
        }
    }

    [Export(typeof(IRepository<>))]
    public class SomeOtherClass : IRepository<Order>
    {
        public String SomeOtherProperty { get; set; }

        public Order Get(Int32 id)
        {
            return new Order();
        }

        public void Save(Order c)
        {
        }
    }
....................

var

 

typeCatalog = new TypeCatalog(typeof(IRepository<>));

 

Thanks,

Coordinator
May 13, 2011 at 8:12 AM
Edited May 13, 2011 at 8:14 AM

Hi,

Open generics are supported by MefContrib project, please see blogpost at http://pwlodek.blogspot.com/2010/12/introduction-to-interceptingcatalog.html

Regarding your samples, there's no need in exporting those classes with open generic contract (again, it is possible with MefContrib) as they are NOT generic types, they implement generic interface with concrete generic parameter. You should consider making them generic, and implement IRepository<T> rather than IRepository<Order>

Hope this helps!

 

Best,

Piotr Włodek