This project is read-only.

OData from nested objects

Topics: Web Api
Dec 13, 2010 at 7:44 PM
Edited Dec 13, 2010 at 11:20 PM

Are there any plans to make it easier to expose existing data structures in OData format through WCF Data Services?  See the example below for details.

Examples are all over the place to use OData with the ADO.NET Entity Data Model.  However, for many companies it is too costly to change existing object structures to use the Entity Data Model.  It would be nice if there was an easy way to expose nested object structures as OData Via WCF Data Services.

Here is one example of how this can be done, but the major drawback is that it requires a flattened hierarchy, which may not be possible with most object structures.

Ex.  Requests to a service like this fail...

Public Class WcfDataService1
    Inherits DataService(Of DataSource)
    Public Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead)
    End Sub
End Class

Public Class DataSource
    Public Property Foos As IQueryable(Of Foo)
End Class

Public Class Foo
    Public Property FooId As Integer
    Public Property Bars As IQueryable(Of Bar)
End Class

Public Class Bar
    Public Property BarId As Integer
End Class
Notice how Foo has an IQuerable of Bar, which has the DataServiceKey.  
For this to work one must either move the 'Bars' property to the DataSource class, or
remove the DataServiceKey property from 'Bar'.  These two alternatives may not be an option for some companies.