locked
Generic repository and WCF RRS feed

Answers

  • Hello,

    WCF doesn't support open generic types. It means that you can't pass Repository<T> to ServiceHost. You must always pass concrete type like Repository<MyEntity>. The reason is that WCF must know all types used in the service to be able to provide metadata. In your case you must have a service for each entity type.

    Check WCF Data Services instead it will save you a lot of time and provide you features which will take you enormous time to create them yourselves. Features include linq queries defined on the client, eager loading and projection defined on the client, single service for whole data model, etc. Whole service on exiting EF model is created within few minutes.

    Best regards,
    Ladislav

    • Proposed as answer by Jackie-Sun Thursday, April 14, 2011 9:35 AM
    • Marked as answer by Jackie-Sun Wednesday, April 20, 2011 5:28 AM
    Tuesday, April 12, 2011 7:31 AM

All replies

  • I'm not quite sure I understand what you're trying to do.  Are you trying to wrap services inside a generic repo? 

    Thursday, April 7, 2011 1:55 AM
  • Yes, I want to make all those generic calls like First(), Add() to be WCF service methods.

    With generic repository you can do the following now:

    GenericRepository<Customer> custRepository=new GenericRepository<Customer>();

    Customer cust= custRepository.First()

    I want to do the same with repository residing in the server (WCF service).

    So for example from client I should be able to do:

    GenericRepository<Customer> custRepository=Service.GetRepository<Customer>();

    When I try to host the sevice, I can't do the following:

    myServiceHostRepository =

    new ServiceHost(typeof(Service.GenericRepository<T

    ));

    Hope I'm clear now.

    Thanks a lot!

    Thursday, April 7, 2011 2:27 PM
  • Ah yes I see.  Can I ask what it is that you'd like to do with this generic repository?  I think you'd have to do something like this:

    public class GenericRepository<T>
    {
      public Type UnderlyingType { get; private set; }
    
      public GenericRepository<T>()
      {
        UnderlyingType = typeof(T);
      }
    }
    

    Then when you're spooling up your service host you'd have to do something like this:

    var repo = new GenericRepository<MyService>();
    new ServiceHost(repo.UnderlyingType);
    
    I'm not really sure what you'd gain by doing this though....

    Monday, April 11, 2011 11:42 PM
  • Hello,

    WCF doesn't support open generic types. It means that you can't pass Repository<T> to ServiceHost. You must always pass concrete type like Repository<MyEntity>. The reason is that WCF must know all types used in the service to be able to provide metadata. In your case you must have a service for each entity type.

    Check WCF Data Services instead it will save you a lot of time and provide you features which will take you enormous time to create them yourselves. Features include linq queries defined on the client, eager loading and projection defined on the client, single service for whole data model, etc. Whole service on exiting EF model is created within few minutes.

    Best regards,
    Ladislav

    • Proposed as answer by Jackie-Sun Thursday, April 14, 2011 9:35 AM
    • Marked as answer by Jackie-Sun Wednesday, April 20, 2011 5:28 AM
    Tuesday, April 12, 2011 7:31 AM