代码或查找表的最佳实践
[更新] 选择的方法如下,作为对这个问题的答复
嗨,
我一直在这个主题中四处寻找,但我找不到我正在寻找的东西......
对于代码表,我的意思是:像'这样的东西婚姻状况、性别、特定的法律或社会状态……更具体地说,这些类型仅具有固定属性,并且这些项目不会很快改变(但可能)。 属性是 Id、名称和描述。
我想知道如何在以下技术中最好地处理这些问题:
在数据库中(多个表,一个具有不同代码键的表...?)
创建类(可能类似于使用 ICode.Name 和 ICode.Description 继承 ICode)
为此创建视图/演示者:应该有一个包含所有这些的屏幕,因此有一个类型列表(性别、婚姻状况...),然后是该类型的值列表,其中包含姓名& 值列表中每个项目的描述。
这些是出现在每个项目中的事情,因此必须有一些关于如何处理这些的最佳实践...
郑重声明,我不太喜欢在这些情况下使用枚举...关于在这里使用它们的任何争论也很受欢迎。
[跟进]
好的,我从 CodeToGlory 和 Ahsteele 那里得到了很好的答案。 让我们细化这个问题。
假设我们不是在谈论性别或婚姻状况,这些价值观肯定不会改变,而是在谈论具有名称和描述的“东西”,仅此而已。 例如:社会地位、法律地位。
用户界面: 我只想要一个屏幕。 包含可能的 NameAndDescription 类型的列表框(我将这样称呼它们)、包含所选 NameAndDescription 类型的可能值的列表框,以及所选 NameAndDescription 类型项的名称和描述字段。
如何在 View & 中处理这个问题? 主持人? 我发现这里的困难是需要从类名中提取名称和描述类型?
D B: 多个查找表与单个查找表的优缺点是什么?
[UPDATE] Chosen approach is below, as a response to this question
Hi,
I' ve been looking around in this subject but I can't really find what I'm looking for...
With Code tables I mean: stuff like 'maritial status', gender, specific legal or social states... More specifically, these types have only set properties and the items are not about to change soon (but could). Properties being an Id, a name and a description.
I'm wondering how to handle these best in the following technologies:
in the database (multiple tables, one table with different code-keys...?)
creating the classes (probably something like inheriting ICode with ICode.Name and ICode.Description)
creating the view/presenter for this: there should be a screen containing all of them, so a list of the types (gender, maritial status ...), and then a list of values for that type with a name & description for each item in the value-list.
These are things that appear in every single project, so there must be some best practice on how to handle these...
For the record, I'm not really fond of using enums for these situations... Any arguments on using them here are welcome too.
[FOLLOW UP]
Ok, I've gotten a nice answer by CodeToGlory and Ahsteele. Let's refine this question.
Say we're not talking about gender or maritial status, wich values will definately not change, but about "stuff" that have a Name and a Description, but nothing more. For example: Social statuses, Legal statuses.
UI:
I want only one screen for this. Listbox with possibe NameAndDescription Types (I'll just call them that), listbox with possible values for the selected NameAndDescription Type, and then a Name and Description field for the selected NameAndDescription Type Item.
How could this be handled in View & Presenters? I find the difficulty here that the NameAndDescription Types would then need to be extracted from the Class Name?
DB:
What are pro/cons for multiple vs single lookup tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用数据库驱动的代码表非常有用。 您可以执行诸如定义数据生命周期(使用开始和结束日期)、将数据实时添加到表中这样就不必部署代码之类的操作,并且可以允许用户(当然具有正确的权限)通过管理屏幕添加数据。
我建议始终使用自动编号主键而不是代码或描述。 这允许您在不同的时间段使用多个代码(名称相同但描述不同)。 另外,大多数 DBA(根据我的经验)宁愿使用自动编号而不是基于文本的主键。
我会为每个编码列表使用一个表。 您可以将多个代码全部放入一个不相关的表中(使用某种矩阵),但这会变得混乱,而且我只发现它甚至有用的几种情况。
Using database driven code tables can very useful. You can do things like define the life of the data (using begin and end dates), add data to the table in real time so you don't have to deploy code, and you can allow users (with the right privileges of course) add data through admin screens.
I would recommend always using an autonumber primary key rather than the code or description. This allows for you to use multiple codes (of the same name but different descriptions) over different periods of time. Plus most DBAs (in my experience) rather use the autonumber over text based primary keys.
I would use a single table per coded list. You can put multiple codes all into one table that don't relate (using a matrix of sorts) but that gets messy and I have only found a couple situations where it was even useful.
这里有几件事:
使用明确且不会改变的枚举。 例如,MaritalStatus、Gender 等。
对未按上述固定且可能随时间变化、增加/减少的项目使用查找表。
数据库中存在查找表是非常典型的。 在您的业务层中定义可与您的视图/演示文稿配合使用的键/值对象。
Couple of things here:
Use Enumerations that are explicitly clear and will not change. For example, MaritalStatus, Gender etc.
Use lookup tables for items that are not fixed as above and may change, increase/decrease over time.
It is very typical to have lookup tables in the database. Define a key/value object in your business tier that can work with your view/presentation.
我决定采用这种方法:
其中:
数据库:
替代文本 http://lh3.ggpht.com/_cNmigBr3EkA/SeZnmHcgHZI/AAAAAAAAAFU/2OTzmtMNqFw/codetables_1.JPG
类代码:
类 CodeKey:
类 CodeValue:
我发现迄今为止最简单、最有效的方法:
我仍在考虑的唯一一件事是丢弃 GUID Id 并使用字符串(nchar)代码以提高业务逻辑中的可用性。
感谢您的回答! 如果对此方法有任何意见,请提出!
I have decided to go with this approach:
Where:
DB:
alt text http://lh3.ggpht.com/_cNmigBr3EkA/SeZnmHcgHZI/AAAAAAAAAFU/2OTzmtMNqFw/codetables_1.JPG
Class Code:
Class CodeKey:
Class CodeValue:
I find by far the easiest and most efficent way:
The only thing I'm still considering is throwing out the GUID Id's and using string (nchar) codes for usability in the business logic.
Thanks for the answers! If there are any remarks on this approach, please do!
我倾向于对此类数据使用表格表示。 最终,如果您需要捕获数据,则需要存储它。 出于报告目的,最好有一个可以通过键提取数据的地方。 出于标准化目的,我发现单一用途查找表比多用途查找表更容易。
也就是说,对于像性别等不会改变的事情,枚举非常有效。
I lean towards using a table representation for this type of data. Ultimately if you have a need to capture the data you'll have a need to store it. For reporting purposes it is better to have a place you can draw that data from via a key. For normalization purposes I find single purpose lookup tables to be easier than a multi-purpose lookup tables.
That said enumerations work pretty well for things that will not change like gender etc.
为什么大家都想让码表复杂化? 是的,它们有很多,但它们很简单,所以就保持这种方式。 就像对待其他物体一样对待它们。 它们是域的一部分,因此将它们建模为域的一部分,没什么特别的。 如果当他们不可避免地需要更多属性或功能时您不这样做,您将不得不撤消当前使用它的所有代码并重新编写它。
当然,每个课程一张表(为了引用完整性,以便它们可用于报告)。
对于课程,当然还是每个课程一个,因为如果我编写一个方法来接收“性别”对象,我不希望能够意外地向它传递“婚姻状态”! 让编译帮助您清除运行时错误,这就是它存在的原因。 每个类都可以简单地继承或包含 CodeTable 类或其他类,但这只是一个实现助手。
对于 UI,如果它实际上使用继承的 CodeTable,我想您可以使用它来帮助您并只需在一个 UI 中维护它。
一般来说,不要搞乱数据库模型,不要搞乱业务模型,但如果你不想在 UI 模型中搞乱一点,那也不错。
Why does everyone want to complicate code tables? Yes there are lots of them, but they are simple, so keep them that way. Just treat them like ever other object. Thy are part of the domain, so model them as part of the domain, nothing special. If you don't when they inevitibly need more attributes or functionality, you will have to undo all your code that currently uses it and rework it.
One table per of course (for referential integrity and so that they are available for reporting).
For the classes, again one per of course because if I write a method to recieve a "Gender" object, I don't want to be able to accidentally pass it a "MarritalStatus"! Let the compile help you weed out runtime error, that's why its there. Each class can simply inherit or contain a CodeTable class or whatever but that's simply an implementation helper.
For the UI, if it does in fact use the inherited CodeTable, I suppose you could use that to help you out and just maintain it in one UI.
As a rule, don't mess up the database model, don't mess up the business model, but it you wnt to screw around a bit in the UI model, that's not so bad.
我想考虑进一步简化这种方法。 与其使用 3 个定义代码(Code、CodeKey 和 CodeValue)的表,不如只使用一张同时包含代码类型和代码值的表? 毕竟,代码类型只是另一个代码列表。
也许像这样的表定义:
可能有一条 CodeType=0 的根记录,Code=0 表示 CodeType 的类型。 所有CodeType记录将具有CodeType=0和Code>=1。 以下是一些可能有助于澄清问题的示例数据:
可以将检查约束添加到 Code 表中,以确保将有效的 CodeType 输入到表中:
函数 IsValidCodeType 可以这样定义:
已经提出的一个问题是如何确保具有代码列的表具有该代码类型的正确值。 这也可以通过使用函数的检查约束来强制执行。
这是一个包含性别列的人员表。 最佳实践是使用代码类型描述(本例中为性别)来命名所有代码列,后跟单词 Code:
IsValidCode 可以这样定义:
可以创建另一个函数来在查询时提供代码描述。具有代码列的表。 这是一个
查询Person表的例子:
这都是从防止数据库中查找表激增并提供一张查找表的角度来构思的。 我不知道这个设计在实践中是否表现良好。
I'd like to consider simplifying this approach even more. Instead of 3 tables defining codes (Code, CodeKey and CodeValue) how about just one table which contains both the code types and the code values? After all the code types are just another list of codes.
Perhaps a table definition like this:
There could be a root record with CodeType=0, Code=0 which represents the type for CodeType. All of the CodeType records will have a CodeType=0 and a Code>=1. Here is some sample data that might help clarify things:
A check constraint could be added to the Code table to ensure that a valid CodeType is entered into the table:
The function IsValidCodeType could be defined like this:
One issue that has been raised is how to ensure that a table with a code column has a proper value for that code type. This too could be enforced by a check constraint using a function.
Here is a Person table which has a gender column. It could be a best practice to name all code columns with the description of the code type (Gender in this example) followed by the word Code:
IsValidCode could be defined this way:
Another function could be created to provide the code description when querying a table that has a code column. Here is an
example of querying the Person table:
This was all conceived from the perspective of preventing the proliferation of lookup tables in the database and providing one lookup table. I have no idea whether this design would perform well in practice.