Azure 表和实体继承
假设我正在为兽医和他们照顾的动物建立一个数据库。我有以下实体:
动物类:TableServiceEntity
狗类:动物
猫类:动物
狗和猫都有独特的属性。我有一个名为 Vets 的 Azure 表,其中 vet.Guid 作为分区键,“Dog_”+dog.Guid 或“Cat_”+cat.Guid 作为行键。如果我要使用 AsTableServiceQuery() 检索所有内容,我可以将动物实体转换为 Dog 或 Cat 并保持这些独特属性完好无损吗?
从更广泛的意义上来说,每个人对这种情况下的权衡有何看法?将 Dog 和 Cat 保留在一张表中以进行高效查询,但在业务层中需要额外的步骤,并使用单独的表进行额外的查询但不那么混乱的代码?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要记住的是,Windows Azure 表中的数据将以 AtomPub 格式返回给您。因此,您有一个序列化过程,可以将 XML 映射到 DTO 上的公共属性。您可以指定在出现额外或缺失属性时要执行的操作(视为错误或忽略)。此外,您可以选择通过挂钩 ReadingEntity 事件来自行重写序列化过程。
因此,在您的问题中,您需要记住,如果您要序列化为动物,则无法转换为狗,因为您需要在运行时指定序列化类型。例如,
如果您选择 TypeToSerialize 作为 Dog,您始终可以向下转换为 Animal,但这可能没有那么有用。但是,如果您选择 TypeToSerialize 作为 Animal,那么 Dog 所拥有的信息就会丢失,因为它不会在任何地方序列化,并且会被有效地忽略。
我提到 ReadingEvent 的原因是您可以自己处理序列化,然后一切就都结束了。您可以检查返回的属性并决定要序列化为哪种类型,或者您可以将额外的属性存储到属性包等中
。HTH
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.
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