1

我有一个支持我的客户表用户定义的字段的应用程序,我已经有这样的事情:设计模式与下拉列表支持用户定义的字段

CustomFields table 

    Id   Name     DataType 
    -------------------------------------------- 
    1    Hobbies    1 
    2    Number of Siblings 3 

上表显示包含的表用于应用程序范围的UDF。在数据类型列中的值转换为这样的事情:

1: string 
2: DateTime 
3: Integer 

下表包含了UDF的

CustomFieldsValues table 

CustomerId  FieldId  StringValue  NumericValue  DateValue 
1234   1   Fishing   NULL    NULL   
1234   2   NULL    6     NULL 
8834   1   Golfing   NULL    NULL   
8834   2   NULL    2     NULL 

值的现在,我想向大家介绍一个新的“数据类型”

4: DropDownList 

这实质上就像string数据类型,除了不是呈现文本框,而是我将有一个下拉列表,而值将是一个由管理员进入。

我能想到的此刻是有另一个表

FieldDropDownList table 

FieldId   DropDownItem 
1    Fishing 
1    Golf 
1    Swimming 
2    Random value #1 
2    Random value #2 
3    Random value #3 

而且随着数据类型4所有自定义字段,将其值保存在CustomFieldsValues表的StringValue

有什么建议和我应该做的考虑?

实现dropdownlist UDF最有效的方法是什么?

回答

0

有什么建议和我应该考虑的事项吗?

是的。重新开始,让DBMS做好工作!那DataType列是警告铃声,说有什么问题。 DBMS提供类型,类型安全性和类型转换。

将您的UDF分成CustomIntFields,CustomStrFieldsCustomDateFields。如果需要拉斯特,可以代表他们作为一个单一的视图,使用UNION

create view CustomFields as 
select 's' as type, FieldID, Name from CustomStrFields UNION 
select 'i' as type, FieldID, Name from CustomIntFields UNION 
select 'd' as type, FieldID, Name from CustomDateFields; 

只是个开始,这将让DBMS代表您确保日期有日期和整数有编号。

DropDowns表成为

create table DropDowns 
    (DropDownID int -- indicating the widget 
    , type char(1) 
    , FieldID int 
); 

引用三个UDF表的联合。

该设计可以添加字段而不会自动出现在下拉列表中,这可能不是您想要的。如果每个字段只应出现在一个特定的下拉列表中,则可以将下拉ID添加到三个字段表以及从该视图驱动的所有内容。

什么是最有效的方式

这东西都非常静态的,小的。我很难相信效率会成为一个问题。但我确实认为程序员和客户的满意度会通过按照预期的方式使用DBMS来提高。 :-)