locked
How can I extend a Resource? RRS feed

  • Question

  • Can anyone help me please...

    ...I am using a WCF Data Service to expose data from a Linq-to-Sql data model. The resourcekinds are based on the entities in the data model. By implementing IUpdatable the service allows Read/Write. What I want to do as part of the post is allow the client to push extra properties with the resource. I then plan to create ChangeInterceptors to extract the extra properties before posting the resource.

    Can anyone help?

    Thanks

     

    Wednesday, March 30, 2011 8:27 PM

Answers

  • There's no "reflection" support for open properties. You have to use custom provider, in which you need to implement GetOpenProperties method (on the IDataServiceQueryProvider). It's totally up to you where you get these properties from. It can definitely be Dictionary, but it can be anything else as well. But you have to write that code, there's no built-in magic to do this for you.

    Thanks,


    Vitek Karas [MSFT]
    Friday, April 1, 2011 9:36 AM
    Moderator

All replies

  • Hi,

    Could you please explain what do you mean by pushing extra properties with the resource?  

    Thursday, March 31, 2011 6:16 AM
  • Yes sorry I was a bit vague.

    Take for example an ExpenseReport resource. The resource may have 3 properties exposed by the LINQ-SQL data model, e.g. Name, Activity, Amount. From an extensibility point of view we allow the consumer of the resource to extend the database and therefore add extra fields to the ExpenseReport resource, e.g. LineManager. However this addition to the database will not be included in the data model, the resource remains static (this is so we can support a multi-tenant code base but a single-tenant database).

    Therefore on POST of a new ExpenseReport the consumer specifies (through a Javascript client) the Name, Activity, Amount and their LineManager value.

    On the server side I would like the service to allow all the fields to be specified so that their additional fields are recognised and submitted to the database as part of the ExpenseReport update.

    The reason I am using LINQ-SQL is because I want that deferred execution of the database query. I don't want a collection of in-memory objects. I have tried the creating my own Custom Data Provider as blogged by Alex James but that didn't give me what I wanted.

    Any help would be appreciated...could I perhaps include these 'extension' fields in a custom request header? Another alternative would be to batch the request so that the extra property would be included in a secondary POST - the downside to this is that there is the potential for the 'relates-to' expression included in the batch to get quite complicated.

    Thanks

    Thursday, March 31, 2011 7:54 AM
  • Hi,

    This is supported by usage of open types (open properties), which basically mean that a given entity can have any number of additional properties on top of those declared by the model (as long as such entity type is marked as open).

    Currently open types are only supported with custom providers. You could implement a custom provider and use LINQ to SQL internally, so that you don't have to implement the LINQ translator yourself. But then exposing the open properties might be rather tricky (depends on your DB model design).

    Also note, that the .NET WCF Data Service client currently only supports open properties if the client knows about them at compile-time. Dynamic open properties are not supported on the client yet.

    Thanks,


    Vitek Karas [MSFT]
    Thursday, March 31, 2011 10:35 AM
    Moderator
  • Vitek,

    That sounds exactly what I want...if I can expose these extra properties as say a Name/Value collection then specifying them at compile time would not be a problem - through Javascript libraries I could ask the consumer for their extra fields and then populate the resource myself client side.

    Can I be a pain and ask, 'have you got any documentation / links / blogs / code samples' of how to use open properties both on the server and the client?

    Thank you - and thanks for your speedy reply very impressed.

    Thursday, March 31, 2011 12:15 PM
  • Hi,

    I must admit that we don't have samples for these. As noted above, the client doesn't really support it, the only support on the client is that you define the client-side classes yourself, if you include the open properties in the class declaration, it will just work (since from the protocol perspective there's no difference between open and declared property).

    On the server you must have a custom provider, and then you mark some of your types as open and a coupld of more methods will be called on your provider interfaces which need to be implemented. This part is usually easy to figure out. The harder part is support quieries over the open properties, because then you need to be able to translate the LINQ queries (Which use special method calls for open properties) into your underlying query language.

    Thanks,


    Vitek Karas [MSFT]
    Thursday, March 31, 2011 4:10 PM
    Moderator
  • Thanks Vitek, finally you say make my new properties open how do I do this and are Dictionaries supported as Open Types?

     

    Thursday, March 31, 2011 5:00 PM
  • There's no "reflection" support for open properties. You have to use custom provider, in which you need to implement GetOpenProperties method (on the IDataServiceQueryProvider). It's totally up to you where you get these properties from. It can definitely be Dictionary, but it can be anything else as well. But you have to write that code, there's no built-in magic to do this for you.

    Thanks,


    Vitek Karas [MSFT]
    Friday, April 1, 2011 9:36 AM
    Moderator