存储和使用动态属性
因此,我正在开发一个电子商务应用程序,我的客户希望能够创建类别和产品......显然。现在,假设客户有大约 100 个类别和 20,000 个产品。
客户端需要能够创建对过滤有意义的类别属性......因此,硬盘驱动器类别可能具有以下属性:
- 容量
- RPM
- 外形尺寸
而投影仪类别可能具有以下属性:
- 亮度
- 对比度
- 原始分辨率
我的问题是如何解决允许动态创建自定义属性,但能够使用这些属性进行搜索、过滤和报告的问题?
为每个类别创建单独的表和对象是不可能的,因为我不知道它们将创建什么类别(即 HardDriveProperties、ProjectorProperties)。
我想也许我可以在数据库中创建一个额外的列并将自定义属性序列化为 JSON,但这仍然需要我为每个类别创建一个特定的属性对象以将 JSON 反序列化为 ProductProperties 的通用列表... .我认为使用它可能会非常昂贵。
其他人是如何解决这个问题的?
So, I'm working on an eCommerce application and my customer wants the ability to create categories and products...obviously. Now, lets say the client will have about 100 categories and 20,000 products.
The client needs the ability to create category properties that make sense for filtering....so, the category Hard Drives might have properties like:
- Capacity
- RPMs
- Form Factor
While the category Projectors might have properties like:
- Brightness
- Contrast Ratio
- Native Resolution
My question is how does one solve the problem of allowing custom properties to be created dynamically, but the ability to use those properties to search, filter and report on?
Creating a seperate table and object for each category isn't possible since I have no idea what categories they will create (i.e. HardDriveProperties, ProjectorProperties).
I thought maybe I could create an extra column in the database and serialize the custom properties as JSON, but that would still require me to either create a specific properties object for each category to deserialize the JSON into or a generic list of ProductProperties....which I think could be pretty expensive to work with.
How are others solving this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Entity-Attribute-Value 模型(又名“开放架构”)是一种方法来解决这个问题。
该模式的本质是将列变成行。而不是看起来像这样的
HardDrive
(和Projector
)表:您有
Category
、CategoryProperties
和CategoryPropertyValues
表:The Entity-Attribute-Value model (aka 'open schema') is one way to solve this problem.
The essence of the pattern is that you make columns into rows. Instead of
HardDrive
(andProjector
) tables that look like this:You have
Category
,CategoryProperties
, andCategoryPropertyValues
tables:创建一个 Product 表,其中包含 ProductID 列。
创建一个 ProductAttribute 表,其中包含以下列:Product(引用的 ProductID)、Name、Value。
我看到的一个网站有几个“值”列,一个文本和一个数字,以便可以按一系列数值进行有效搜索。
Create a table Product which has a column ProductID.
Create a table ProductAttribute which has these columns: Product (referenced ProductID), Name, Value.
One site I saw had several Value columns, one text and one number, to allow efficiently searching by a range for numeric values.
假设您使用关系数据库,我可能会使用类似这样的内容:
这样,您可以定义在添加/编辑属于特定类别的产品时哪些属性应该可用,并且产品可以具有无限的属性。
这比仅仅序列化 JSON 值更好,因为搜索/过滤序列化值可能非常痛苦。但是,这种方法确实使用相对规范化的数据库结构,因此如果您有很多产品、属性等,全文搜索可能会很慢。
Assuming that you use a relational database, I would probably use something like in the lines of this:
That way, you can define which properties should be available when adding/editing a product that belongs to a specific category and a product could have inifinite properties.
This would be better than just serializing JSON values, because searching/filtering for the serialized values may be really painful. However, this approach does use a relatively normalized database structure and thus may might be slow for a full-text search if you have a lot of products, properties, ...
我认为你的最后一段将是我要走的路线。我主要关心的是大量记录的序列化(反序列化)的性能。
I think that your last paragraph would be the route that I would go. My main concern would be with the performance of (de)serialization of large numbers of records.