简体   繁体   English

如何处理以编程方式添加字段?

[英]How to handle programmatic addition of fields?

Yesterday, I asked this question regarding best practices for a simple information retrieval system I am beginning to work on. 昨天,我问了一个有关我将开始工作的简单信息检索系统的最佳实践的问题

Today, my customer asked me if it is possible to allow them to add fields to the primary entity at a later date using the administration interface. 今天,我的客户问我是否可以允许他们稍后使用管理界面将字段添加到主要实体。 That is, the application allows you to search across one database table (let us call it Entities), which may have various associations to other objects, but the focus is on the Entities. 也就是说,该应用程序允许您在一个数据库表(我们称为实体)中进行搜索,该数据库表可能与其他对象具有各种关联,但重点是实体。

Now, of course, the sky's the limit, and this is possible. 当然,现在是天空的极限,这是可能的。

My initial thought with this problem is to create two new tables: 我最初对这个问题的想法是创建两个新表:

AdditionalFields (FieldID PK , FieldName, IsShownInSearchResults, and other metadata) This is the table that would have its rows created by the admin interface. AdditionalFields (FieldID PK ,FieldName,IsShownInSearchResults和其他元数据)这是将由管理界面创建其行的表。 For instance, if the customer later decides they want to add the ability to track, say, the eye color an Entity, they would create an AdditionalField with a FieldName of "Eye Color". 例如,如果客户后来决定要添加跟踪实体眼睛颜色的功能,则他们将创建一个FieldName为“眼睛颜色”的AdditionalField。

AdditionalFieldData (FieldDataID PK , EntityID FK , FieldID FK , FieldValue) This is the table relating the Entities to the their respective values for any additional fields added by the customer. AdditionalFieldData (FieldDataID PK ,EntityID FK ,FieldID FK ,FieldValue)这是一个表,将实体与客户为其添加的任何其他字段的相应值相关联。 For instance, with our eye color example, if we have two Entities (EntityID 3 and 4) at the time the field was added and the new AdditionalField has a FieldID of 1, it is valid to have no eye color data associated with the entities. 例如,在我们的眼睛颜色示例中,如果在添加字段时我们有两个实体(EntityID 3和4),而新的AdditionalField的FieldID为1,则没有与该实体相关联的眼睛颜色数据是有效的。 The customer could then choose, to add the eye color "Blue" to Entity 3, and we'd insert a row to AdditionalFieldData like this: ( autogenerated PK , 3, 1, "Blue"). 然后,客户可以选择将眼睛颜色“蓝色”添加到实体3,然后我们将一行插入到AdditionalFieldData中,如下所示:( 自动生成的PK ,3、1,“蓝色”)。

Since I want to easily search across these, I will simply require that all additional fields have string values. 由于我想轻松地搜索这些内容,因此我仅要求所有其他字段都具有字符串值。

Following the approach I posted in the previous question, I'd be using Linq-To-SQL with a query similar to the following for information retrieval when a user performs a search (of course, the logic for filtering will be more complex in practice): 按照我在上一个问题中发布的方法,我将在用户执行搜索时将Linq-To-SQL与类似于以下查询的查询一起用于信息检索(当然,过滤的逻辑实际上会更复杂):

var results = from s in db.Stuff
          where (s.Name.Contains(textFilter) ||
                 s.Title.Contains(textFilter))
          select s;

I would modify the query to look something like this: 我将查询修改为如下所示:

var results = from s in db.Stuff
              where (...existing filter logic...) ||
                    s.AdditionalFieldData.Any(afd => afd.FieldName.Contains(textFilter))
              select s;

So my question is: Is this reasonable? 所以我的问题是:这合理吗? Is there a better solution that I missed? 有没有我错过的更好的解决方案? Are there any implications of this approach that I should be aware of? 我应该注意这种方法的任何含义吗?

(Please forgive my silly identifiers, its early for me :) (请原谅我愚蠢的标识符,对我来说还早:)

I'm no LINQ expert, but from a database design point of view what you suggest seems just fine to me. 我不是LINQ专家,但是从数据库设计的角度来看,您的建议对我来说似乎很好。

An alternative which I've seen used a few times is to have a whole bunch of 'spare' fields defined on the Entities table, which the users can enable/disable through the admin interface. 我见过几次的替代方法是在Entities表上定义一堆“备用”字段,用户可以通过管理界面启用/禁用这些字段。 You gain some database performance this way (due to the smaller number of joins), but you have to choose up front how many of these spare fields you want, and you end up with large, mostly empty tables for most of the time. 您可以通过这种方式获得一些数据库性能(由于联接的数量较少),但是您必须预先选择所需的这些备用字段中的多少,并且在大多数情况下最终会得到大的,大部分为空的表。

Oh, and you can safely assume that if you choose to add N spare fields, your users will want N+1 ;-) 哦,您可以放心地假设,如果您选择添加N个备用字段,那么您的用户将需要N + 1 ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM