强制执行必须在*不同*字段中的每个子类中实现的属性的最佳方法是什么?

发布于 2024-07-14 12:35:46 字数 709 浏览 2 评论 0原文

我试图想出“最佳”方法来实现 灵活的 SQL 数据服务实体模型,其中每个类都可以存储为实体,甚至派生类。

示例:每个子类都有不同

string Id
string Kind
Dictionary<string, object> Properties

到目前为止,我正朝着同时拥有实体类(如上所示)和基类的方向前进,该基类具有某种类型的集合,例如

Dictionary<string, Entity> data

并将每个子类添加到该字典并且只是获取/设置属性,例如

data["EntityKind"].Properties["PropertyName"]

但是,由于每个类只有一个实体,所以似乎我应该使用某种堆栈(而不是字典),其中层次结构的每个级别都确切地知道它在哪里。 然后我想到类继承就是堆栈,所以我想也许我只是错过了一些巨大的面向对象概念,而这些概念确实可以简化所有这一切。 就像是

abstract eachsubclassmusthaveitsown Entity entity

I am trying to come up with the "best" way to implement SQL Data Services flexible entity model where each class could be stored as an entity, even derrived classes.

Example: Every subclass has different

string Id
string Kind
Dictionary<string, object> Properties

So far, I'm heading in the direction of having both an Entity class (with above) and a base class that has some kind of collection like

Dictionary<string, Entity> data

And have each subclass add to that dictionary and just get/set properties like

data["EntityKind"].Properties["PropertyName"]

However, since each class only has ONE Entity, it seems like I should be using some sort of Stack (intead of Dictionary) where each level of the hierarchy knows exactly where it is. Then it occured to me that class inheritance IS the stack, so I thought maybe I was just missing some huge OO concept that would really simplify all of this. Something like

abstract eachsubclassmusthaveitsown Entity entity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-07-21 12:35:46

每个类都是一个实体,您想将一些实体元数据与其关联吗?

听起来属性可能是你最好的选择。

您将定义一个类EntityAttribute,其中的成员用于存储描述实体所需的元数据。 然后,您可以使用 [Entity] 标记实体类。 如果所有字段都是必填字段,请为属性类提供一个需要传递值的构造函数。

然后使用反射来发现实体类。 请注意,您只需执行一次发现过程,然后将其缓存,因此反射的性能不应该成为问题。

Each class is an entity, and you want to associate some entity metadata with it?

It sounds like Attributes might be your best shot.

You would define a class EntityAttribute, with members to store the metadata needed to describe an entity. That would then allow you to tag entity classes with [Entity]. If all the fields are mandatory, give the attribute class a single constructor that requires the values to be passed.

Then use reflection to discover the entity classes. Note that you only need to do that discovery process once and then cache it, so the performance of reflection should not be an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文