开发者

Azure Table and Entity Inheritance

开发者 https://www.devze.com 2023-03-04 06:25 出处:网络
Let\'s say I\'m building a database for vets and animals they care for. I have following entities: class Animal : TableServiceEntity

Let's say I'm building a database for vets and animals they care for. I have following entities:

class Animal : TableServiceEntity

class Dog : Animal

class Cat : Animal

开发者_StackOverflow中文版

Dog and Cat each has unique properties. And I have a Azure Table called Vets with vet.Guid as partition key and "Dog_" + dog.Guid or "Cat_" + cat.Guid as row key. If I were to retrieve everything by using AsTableServiceQuery() can I then cast an animal entity to Dog or Cat with those unique properties intact?

In broader sense, what's everyone's take on the trade offs in these kinds of situations? Between keeping Dog and Cat in one table for efficient querying but extra step in business layer and have separate tables with extra query but less messy code?


The thing to keep in mind is that the data in Windows Azure tables is coming back to you in AtomPub format. So, you have a serialization process that maps the XML into public properties on your DTO. You can specify what to do in the case of extra or missing properties (treat as error or ignore). Furthermore, you can optionally override the serialization process yourself by hooking the ReadingEntity event.

So, in context of your question, you need to keep in mind that if you were to Serialize to an Animal, you could not cast to a Dog because you need to specify the serialization type at runtime. E.g.

ctx.CreateQuery<TypeToSerialize>().Where(s => s.Property == blah);

If you choose TypeToSerialize as Dog, you could always downcast to Animal, but that might not be as useful. But if you chose TypeToSerialize as Animal, then the information that a Dog has would be missing already as it would not be serialized anywhere and would effectively be ignored.

The reason I mention the ReadingEvent is that it is possible for you to handle serialization yourself and then all bets are off. You can inspect the properties coming back and decide which type you wish to serialize into or you could store the extra attributes into a property bag, etc.

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消