locked
Data Service over EF4 using factory pattern RRS feed

  • Question

  • Hello, I have an entity framework project that has three different entity data models that I have implemented a factory to interact with. I would like to place a WCF Data Service over this assembly and allow the service determine which is the appropriate data model to query. However, I am really not sure how to go about accompishing this. Essentially, I want to use a query interceptor to interrogate the query string for a given parameter (I have done this to implement JSON support before) and then have the service route the query to the apporpriate data model from there. Is this even possible? If so, are there any examples that I could use to guide me? I have not been able to find any searching endlessly.

    Best.

    Sunday, February 20, 2011 9:04 PM

Answers

  • If you have three different models, then you will have three different context types and thus three different DataService<T> implementations (three services, which means three .svc files and so on)

    In each of those you would override the CreateDataSource and using your factory you would create the appropriate model, getting the right context type.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by rkbrown Monday, February 28, 2011 11:11 PM
    Friday, February 25, 2011 11:01 AM
    Moderator

All replies

  • On 2/20/2011 4:04 PM, rkbrown wrote:
    > Hello, I have an entity framework project that has three different
    > entity data models that I have implemented a factory to interact with. I
    > would like to place a WCF Data Service over this assembly and allow the
    > service determine which is the appropriate data model to query. However,
    > I am really not sure how to go about accompishing this. Essentially, I
    > want to use a query interceptor to interrogate the query string for a
    > given parameter (I have done this to implement JSON support before) and
    > then have the service route the query to the apporpriate data model from
    > there. Is this even possible? If so, are there any examples that I could
    > use to guide me? I have not been able to find any searching endlessly.
    >
     
    I think you would use a Linq service provider that you will need to
    create/roll your own.
     
    <http://msdn.microsoft.com/en-us/library/bb546158.aspx>
     
    Sunday, February 20, 2011 11:13 PM
  • Hi,

    Are the three models different in their shape or are they of the same shape, just different data?

    If the only difference is in data, you can do this by overriding the CreateDataSource method on the DataService class, look at the query parameter and return an ObjectContext instance appropriate for the given query.

    If the models differ in their shape, this can't be done as easily as noted above. You could implement your custom IDataServiceHost and host the service on your own. For each incomming request you would determine which model it goes to and according to that you would create a DataService<TForTheModel>.

    Note that I don't think the second approach is particularly good because query options should not completely change the "scheme", people/clients don't expect that. If you have three completely different models, then I would expose them as three different services (endpoints). That way clients will easily understand that the shapes are different.

    (Without this for example the Add Service Reference from VS won't work, since it relies on $metadata response which would now change depending on the custom query option).

    Thanks,


    Vitek Karas [MSFT]
    Monday, February 21, 2011 11:04 AM
    Moderator
  • Excellent point about the shape of the data causing issues for the clients of the service. I will create three different services to solve this issue. would it still be recommended to overload the IDataServiceHost to create the DataService<T> via the factory?

    Thank you very much!

    Monday, February 21, 2011 7:00 PM
  • If you are going to have three separate end points, then I don't see a need to use IDataServiceHost. It would probably be just more trouble without any real gain.

    Thanks,


    Vitek Karas [MSFT]
    Tuesday, February 22, 2011 9:14 AM
    Moderator
  • I do understand that. I believe I may have misstated. I am just not aware of how to instantiate the service while creating a data source via a factory. I can override the CreateDataSource method. However, the return type is the context I have specified in the generic inheritance specification.

    I am probably missing something very obvious. I apologize if this is the case.

    Thank you!

    Thursday, February 24, 2011 9:39 PM
  • If you have three different models, then you will have three different context types and thus three different DataService<T> implementations (three services, which means three .svc files and so on)

    In each of those you would override the CreateDataSource and using your factory you would create the appropriate model, getting the right context type.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by rkbrown Monday, February 28, 2011 11:11 PM
    Friday, February 25, 2011 11:01 AM
    Moderator