什么是强制执行,必须在每个子类中* *不同的字段来实现性能的最佳方式是什么? [英] What is the best way to enforce properties that must be implemented at each subclass in *different* fields?

查看:189
本文介绍了什么是强制执行,必须在每个子类中* *不同的字段来实现性能的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拿出最佳的方式来实现 SQL数据服务灵活的实体模型,每个班级可以存储为一个实体,甚至derrived类。

例:每个子类都有的不同的

 字符串ID
字符串类
字典<字符串,对象>性能
 

到目前为止,我朝着兼具实体类(含以上),并有一些集合类如

基类的方向

 词典<字符串,实体GT;数据
 

和有每个子类添加到字典和刚刚获得/设置属性,如

 数据[EntityKind]属性[属性名]
 

不过,因为每个类只有一个实体,好像我应该使用(词典的这一翻译)某种堆栈,其中每个层次都知道它的确切位置。然后,它发生在我的类继承是栈,所以我想也许我只是缺少一些巨大的面向对象的概念,将真正简化了这一切。类似于

 抽象eachsubclassmusthaveitsown实体的实体
 

解决方案

每个类是一个实体,并且希望某些实体元数据与其关联?

这听起来像属性可能你最好的拍摄。

您需要定义一个类 EntityAttribute ,其成员以存储描述一个实体所需的元数据。这将然后让你标记实体类[实体]。如果所有的字段都是强制性的,赋予属性类的单个构造,需要传递的价值观。

然后使用反射来发现实体类。请注意,您只需要做的发现过程一次,然后缓存,所以反射性能不应该是一个问题。

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

解决方案

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.

这篇关于什么是强制执行,必须在每个子类中* *不同的字段来实现性能的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆