将代码关联到数据库行的建议架构 [英] Suggested architecture for associating code to db rows

查看:13
本文介绍了将代码关联到数据库行的建议架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过数据库中有一堆行的情况.例如,假设我们有一个名为 ReportRendererType 的表.

I have a situation where I have a bunch of rows in a database. For example, let say we have a table called ReportRendererType.

行可能是:

  • 线图
  • 条形图
  • 网格

我可以使用数据库来存储特定用户想要查看特定报告的方式.例如.Frank 喜欢显示为条形图的利润报告.如果我能将代码与数据库中的条目相关联,那就太好了.换句话说,如果 LineGraphRenderer 类知道它与 ReportRenderType 表中的第 1 行相关联,那就太好了.一个简单的例子说明为什么这会很有用是你可以填充一个 ReportRenderType 选项的下拉列表,然后选择一个值,一个 Manager 类可以很容易地确定如何在给定从下拉列表中选择的值的情况下显示报告.那么问题是 - 你如何在数据库和那段代码之间建立关联.

I can use the database to store how particular users want to view particular reports. Eg. Frank likes the Profit Report displayed as a Bar Graph. It would be lovely if I could associate code with the entry in the db. In other words, it would be great if the LineGraphRenderer class knew that it was associated with row 1 in the ReportRenderType table. One trivial example of why this would be useful is that you could populate a dropdown list of ReportRenderType options, and then select a value and a Manager class could easily figure out how to display the report given the value selected from the dropdownlist. The question is then - how do you make the association between the db and that chunk of code.

有很多选择:

A.当您创建 LineGraphRenderer 类时,您可以拥有一个属性 ReportRendererType,它返回关联行的主键.

A. When you make the LineGraphRenderer class, you could have a property ReportRendererType which returned the primary key of the associated row.

B.您可以让 LineGraphRenderer 返回一个枚举,该枚举具有主键的值(C# 允许您通过显式设置哈希代码值来执行此操作),这提供了静态类型和所有数据库条目的单个位置.

B. You could have a LineGraphRenderer return an enumeration which had a value of the primary key (C# allows you to do this by setting the hash code value explicitly) this gives static typing, and a single location for all the db entries.

C.您可以在 LineGraphRenderer 上拥有一个 ReportRendererType 属性,该属性返回主键但从设置文件中检索关联行的值.这将有助于创建关联,特别是如果其他人具有不同的主键值.例如,A 公司可能将 LineGraph 存储在第 1 行,但公司 B 可能将 LineGraph 存储在第 2 行.

C. You could have a ReportRendererType property on LineGraphRenderer which returned the primary key but retrieved the value for the associated row from a settings file. This would facilitate creating the associations, especially if someone else has different primary key values. For example, Company A might have LineGraph stored in row 1, but Company B might have LineGraph stored in row 2.

不幸的是,所有这些方法似乎也有缺点.我想知道其他人是如何解决这个问题的,以及他们是否找到了一些我所缺少的做事的好方法.

Unfortuanately all these ways also seem to have drawbacks. I'm wondering how this problem has been tackled by others, and if they've found some great ways of doing things that I'm missing.

推荐答案

我广泛使用选项 B.这取决于两件事:

I use option B extensively. This relies on two things:

  1. 这些值不会经常更改(这需要编辑和重新构建代码)
  2. 数据库已正确规范化,因此您可以枚举例如 ReportRendererType 的主键,它在数据库中用作外键(例如在 Customer 表中).

这非常有效,因为您可以在数据库查询和更新中直接引用枚举值.如果您提前计划一下,您还可以创建枚举类型名称,以便通过按大写字母拆分文本来轻松地在 UI 中显示它们.例如,条形图"变为条形图".

This works very well as you can reference the enumerated values directly in database queries and updates. If you plan ahead a bit you can also create the enumerated type names so that they can be easily displayed in a UI by splitting the text by capital letter. 'BarGraph' becomes 'Bar Graph' for example.

取决于您是否希望代码与数据库紧密耦合.我个人对此感到满意,但其他人可能不满意.

Depends whether you want your code that tightly coupled to your database or not. Personally I'm comfortable with it but others may not be.

这篇关于将代码关联到数据库行的建议架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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