Take the 2-minute tour ×

I'm checking out WCF Data Services and OData for the first time and I'm wondering if it's bad practice to use inheritance in the entity model? It doesn't seem to be very well supported.

For instance I'm getting issues with OData where I'd like to filter on a derived type's property. But it seems only possible to filter on base type properties?

This fails: http://[localhost]:8080/Entities?$filter=startswith(Text,'bla')

In any case, is it better to create non-derived entity types and have interface's to "harmonize" properties in different entity types?

For instance:

EntityBase -> Id, ParentId

Notebook : EntityBase -> Title

Note : EntityBase -> Text


IEntity -> Id, ParentId

Notebook : IEntity -> Id, ParentId, Title

Note : IEntity -> Id, ParentId, Text

And why is still not the $format-tag supported in 5.1.0-rc1?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

$format will be supported soon. This has been lower priority in part because of ideological reasons and because there are simple workarounds. Until then you can use the JSONP attribute Pablo has blogged about. At any rate, you should see $format supported in URLs by our stack sometime in the next couple of months.

As for derived types, most operations should work fine to the best of my knowledge. To filter on a derived type's property, you'll need to cast to that type first. For instance, in my Scratch service I have the following classes:

namespace Scratch.Web
    public abstract class Product
        public int ID { get; set; }
        public string Name { get; set; }
        public Product RelatedProduct { get; set; }

    public class DiscountedProduct : Product
        public double Discount { get; set; }
        public ICollection<Category> Categories { get; set; }

    // ...

This enables me to issue a query such as the following (which will get me all of the discounted products with a discount greater than 10):

http://localhost:59803/ScratchService.svc/Products/Scratch.Web.DiscountedProduct?$filter=Discount gt 10.0

In the URL, notice the cast to the type that I want to filter on (with the fully qualified entity type name).

share|improve this answer
I also just noticed that LightSwitch doesn't seem to support inheritance in the domain model, so I wouldn't recommend anyone using inheritance in their models yet: connect.microsoft.com/VisualStudio/feedback/details/749933/… –  Andreas Zita Aug 11 '12 at 10:47
If your client is going to be LightSwitch, that's probably true. If your client is going to be WCF Data Services, or data.js, or ODataLib, or simple JavaScript, I think you'd be perfectly fine using inheritance. –  Mark Stafford - MSFT Aug 11 '12 at 16:06
In order to use inheritance in Data Services I had to upgrade to the latest version of the libraries. I'm using version 5.4 (NuGet) of the client libraries and I can confirm that inheritance does work with that version. –  The Senator May 2 '13 at 14:12
add comment

Your Answer


