Andreas Zita
Andreas Zita

Reputation: 7560

Derived entity types in WCF Data Services and OData

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

or

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?

Upvotes: 1

Views: 1356

Answers (1)

Mark Stafford - MSFT
Mark Stafford - MSFT

Reputation: 4336

$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).

Upvotes: 2

Related Questions