locked
Query options $filter and $orderby not working with untyped properties RRS feed

  • Question

  • Hi,

    I have implemented my own custom data service provider (metadata, query, and update components till now). I am able to get query roots for all resource sets and also use $filter and $orderby for CLR properties correctly.
    One of my resource types also contains a property dictionary and I was able to expose its members as untyped properties. I am facing issues while trying to use these untyped properties to filter or order the query root for the resource type. I'm guessing I would need to implement something similar to IDataServiceQueryProvider.GetPropertyValue() for some Linq provider?

    Thanks,
    Nitesh Baranwal

    Thursday, July 29, 2010 9:42 PM

Answers

All replies

  • Hi,

    The so called "untyped" properties (non-CLR but declared only in the metadata) are expressed differently in the LINQ Expression tree. Simply put, they can't be accessed as the usual CLR properties with the Expression.Property node (MemberAccess type), since there's no property on the actual CLR type. Instead WCF Data Services uses a method call. For more detailed take a look at this post describing how properties are accessed in expression trees: http://blogs.msdn.com/b/vitek/archive/2010/04/16/data-services-expressions-part-4-accessing-properties.aspx. A sample of a provider which does that is described in this blog post: http://blogs.msdn.com/b/alexj/archive/2010/03/02/creating-a-data-service-provider-part-9-un-typed.aspx. You can also get a full project of a provider which uses dictionaries to store property values. It's part of the OData Provider Tookit found here http://www.odata.org/developers/odata-sdk. In the zip archive look for UntypedRO (read only implementation) or UntypedRW (includes IUpdatable for such properties).

    Thanks,


    Vitek Karas [MSFT]
    Friday, July 30, 2010 9:02 AM
    Moderator
  • Vitek

    Would it be possible to use Reflection.Emit in order to create a dynamic typed model?

    Thanks in advance

    Friday, August 6, 2010 7:07 AM
  • Hi,

    Technically it is possible. But I would advise against it. The problem with Reflection.Emit is that the generated types will remain in memory forever (well until the appdomain/process dies). So if your metadata changes quite often you're basically polluting the memory and thus you can run out of it (not counting possible issues due to memory fragmentation).

    Just curious - why do you think it would be easier to generate the types using Reflection.Emit as oppose to use the dynamic type support in WCF Data Services? (Generating new classes which actually work with Reflection.Emit is not really simple).

    Thanks,


    Vitek Karas [MSFT]
    Friday, August 6, 2010 2:57 PM
    Moderator
  • Hi Vitek,

    Thanks for those links. I based my implementation of the Linq provider off the sample project and the resulting translated expression looks correct (references the right method on the resource to get value of resource property used in the expression). Here are the expressions before and after translation-

    Before: {value(NHibernate.Linq.Query`1[Japt.Core.Domain.Data.BusinessObject]).OrderBy(element => Convert(GetValue(element, value(System.Data.Services.Providers.ResourceProperty))))}

    After: {value(NHibernate.Linq.Query`1[Japt.Core.Domain.Data.BusinessObject]).OrderBy(element => Convert(element.GetFieldValue(value(System.Data.Services.Providers.ResourceProperty).Name)))}

    The GetFieldValue(string fieldName) function is defined on BusinessObject.

    However the code runs into a NullReferenceException at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria subcriteria, String propertyName) for the propertyName. Don't know if you are the best person for this but hopefully you have seen or run into it before. Any help/links would be appreciated.

    Thanks,
    Nitesh Baranwal

    Thursday, August 12, 2010 4:52 PM
  • Hi,

    I haven't seen that before and without some detailed debugging I don't know what might be wrong. I honestly don't know NHibernate that much.

    I think you should try asking this one some NHibernate focues forum.

    Thanks,


    Vitek Karas [MSFT]
    Friday, August 13, 2010 7:08 AM
    Moderator
  • Thanks a lot for all your help, Vitek.
    Friday, August 13, 2010 7:18 PM
  •  

    That's beacuse i am looking for a way for implementing a dynamic domain model with entity framework

    Thanks

     

    Wednesday, August 18, 2010 3:42 PM