如何构建可扩展的数据模型 [英] How do you build extensible data model

查看:16
本文介绍了如何构建可扩展的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用 NHibernate 和 Fluent NHibernate 构建具有可扩展数据模型的电子商务应用程序.通过拥有可扩展的数据模型,我能够定义一个 Product 实体,并允许应用程序中的用户使用具有不同数据类型(包括自定义数据类型)的新字段/属性来扩展它.

I'm thinking of building a ecommerce application with an extensible data model using NHibernate and Fluent NHibernate. By having an extensible data model, I have the ability to define a Product entity, and allow a user in the application to extend it with new fields/properties with different data types including custom data types.

示例:产品可以有一个附加字段,如:大小 - 整数颜色 - 字符串价格 - 十进制ColoredImage 的集合 - 名称、图像(例如Red"、red.jpg(二进制文件))

Example: Product can have an addition fields like: Size - int Color - string Price - decimal Collection of ColoredImage - name, image (e.g. "Red", red.jpg (binary file))

另一个要求是能够通过这些附加/扩展字段过滤产品.我应该如何实施?

An additional requirement is to be able to filter the products by these additional/extended fields. How should I implement this?

提前致谢.

推荐答案

其中一个选项是 EAV 模型(Entity-Attribute-Value).

One of the options is EAV model (Entity-Attribute-Value).

如果您的域中只有一个类,则该模型很适合应用,这种表表示会导致表很宽(大量列,许多空值)

This model is good to apply if you have a single class in your domain, which table representation would result in a wide table (large number of columns, many null values)

它最初是为医学领域设计的,其中对象可能有数千列(症状).

It's originally designed for medical domain, where objects may have thousands of columns (sympthoms).

基本上你有

实体 (Id)(例如您的产品表)属性(ID,列名)值(EntityId, AttributeId, value)

Entity (Id) (for example your Product table) Attribute(Id, ColumnName) Value(EntityId, AttributeId, value)

您可以拥有一些额外的元数据表.

You can have some additional metadata tables.

值最好是多个表,一个类型.例如:ShortStringValue(EntityId, AttributeId, Value nvarchar(50));LongStringValue(EntityId, AttributeId, Value nvarchar(2048));MemoValue(EntityId, AttributeId, Value nvarchar(max));IntValue(EntityId, AttributeId, Value int);

Value should better be multiple tables, one for a type. For example: ShortStringValue(EntityId, AttributeId, Value nvarchar(50)); LongStringValue(EntityId, AttributeId, Value nvarchar(2048)); MemoValue(EntityId, AttributeId, Value nvarchar(max)); IntValue(EntityId, AttributeId, Value int);

甚至是一个完整的类型:ColorComponentsValue(EntityId, AttributeId, R int, G int, B int );

or even a comple type: ColorComponentsValue(EntityId, AttributeId, R int, G int, B int );

根据我的经验,其中一件事是您不应该对所有内容都使用 EAV.只需为单个类使用 EAV,例如产品.如果你必须对不同的基类使用可扩展性,让它成为一组单独的 EAV 表.

One of the things from my experience is that you should not have EAV for everything. Just have EAV for a single class, Product for example. If you have to use extensibility for different base classes, let it be a separate set of EAV tables.

另一件事是你必须为你的对象发明一个智能的物化策略.不要将这些值转换为宽行集,只需根据您的查询条件需要转换少量列,然后为每个选定对象返回一个窄的值行集合.否则,旋转将涉及大量连接.

Onother thing is that you have to invent a smart materialization strategy for your objects. Do not pivot these values to a wide row set, pivot just a small number of collumns for your query criteria needs, then return a narrow collection of Value rows for each of the selected objects. Otherwise pivoting would involve massive join.

有几点需要考虑:.每个值都占用外键的存储空间.例如,对于此类查询,行级锁定的行为会有所不同,这可能会导致性能下降..可能会导致更大的索引大小.

There are some points to consider: . Each value takes storage space for foreign keys . For example row-level locking will behave different for such queries, which may result in performance degradation. . May result in larger index sizes.

实际上,在一个浅薄的地狱世界中,我的 EAV 解决方案在包含 4 列条件的查询中的 20 列表上的表现优于它的静态对应项.

Actually in a shallow hellow world test my EAV solution outperformed it's static counterpart on a 20 column table in a query with 4 columns involved in criteria.

这篇关于如何构建可扩展的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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