用于关联code到DB行建议的架构 [英] Suggested architecture for associating code to db rows

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

问题描述

我有一个情况我有一大堆的行中的数据库。例如,让说,我们有一个名为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.

行可能是:


  • 线图

  • 条形图

  • 电网

等。

我可以使用数据库来存储用户如何特别要查看特定报告。例如。弗兰克喜欢显示为条形图的利润报告。这将是可爱的,如果我能code。与在数据库中的条目相关联。换句话说,如果LineGraphRenderer类知道它与在ReportRenderType表行1有关这将是巨大的。为什么这将是有用的小例子是,你可以填充的ReportRenderType选项的下拉列表,然后选择一个值和一个Manager类很容易弄清楚如何给显示从DropDownList中选择的值报告。接下来的问题是 - 你如何db和code那块之间的关联

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.

有很多的选择:

一个。当你做出LineGraphRenderer类,你可以有它返回的相关行的主键的属性ReportRendererType。

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

乙。你可以有一个LineGraphRenderer返回一个枚举其中有主键的值(C#,您可以通过显式设置散列code值要做到这一点),这给静态类型,并为所有的数据库条目一个位置。

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.

℃。你可以有一个LineGraphRenderer财产ReportRendererType其中返回的主键,但检索的值从一个设置文件关联的行。这将有利于创造的关联,尤其是如果别人有不同的主键值。例如,A公司可能有线图存储在第1行,但乙公司可能存储在第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所有这些方面似乎也有缺点。我不知道如何解决这个问题的其他人解决,而如果他们已经找到做的事情,我错过了一些好办法。

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. 的值不变动频繁(这需要code编辑和重新构建)

  2. 数据库被正确地规范化,让你列举的主键,例如,ReportRendererType这是在数据库内用作外键(在你的客户表为例)。

这工作得非常好,你可以在数据库查询和更新直接引用枚举值。如果你提前些,你还可以创建枚举类型名,使他们可以通过将通过大写字母文本很容易地显示在用户界面。 光柱变成了棒图为例。

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.

取决于您是否希望您code是紧耦合到您的数据库或没有。我个人很舒服,但其他人可能没有。

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.

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

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