如何将存储在数据库中的定义用于源代码中的定义?
我想了解如何将存储在数据库(由您的应用程序使用)中的定义使用到应用程序的源代码中。
示例:
Database
- Cars
- CarTypeDefinitions
Cars
列通过外键链接到 CarTypeDefinitions
中的一行,因此定义了汽车的类型包含在汽车
中。
Cars
包含诸如“Aston Martin DB8”、“Range Rover” 和“Mercedes Actros” 等条目。
CarTypeDefinitions
包含诸如“跑车”、“SUV” 和“卡车” 等条目。
源代码
现在我希望也能够在我的源代码中使用这些定义。因此,我们需要以某种方式在 CarTypeDefinitions 表中的行与源代码中(最好是)类型安全的实现之间创建某种映射。
一种可能的实现
我想到的第一件事(我特别在寻找其他解决方案或对此的反馈)是创建一个 Enum
ECarTypeDefinitions
。
public enum ECarTypeDefinitions
{
SportsCar = 1,
SUV = 2,
Truck = 3
}
现在我们有了一个类型安全的 Enum
我们可以像这样使用它:
public bool IsSportsCar(Car currentCar)
{
return (currentCar.CarType == ECarTypeDefinitions.SportsCar);
}
该枚举的内容将从 CarTypeDefinitions
表的内容自动生成(添加两个附加列用于枚举名称及其整数值)。
这也可以以其他方式工作,例如从 ECarTypeDefinitions
Enum
生成 CarTypeDefinitions
DB 表的内容。
我很想知道解决这个问题的其他方法。你是怎么处理这个问题的?
I would like to get some ideas how to use definitions which are stored in a DB (which is used by your application) into the source code of the application.
Example:
Database
- Cars
- CarTypeDefinitions
A column of Cars
links by a foreign key to a row in CarTypeDefinitions
, therefore defines the type of the car which is contained in Cars
.
Cars
contains entries like 'Aston Martin DB8', 'Range Rover' and 'Mercedes Actros'.
CarTypeDefinitions
contains entries like 'Sports car', 'SUV' and 'Truck'.
Source code
Now I would like to be able to use these definitions in my source code as well. So somehow we need to create some kind of mapping between a row in the CarTypeDefinitions
table and a (preferably) type-safe implementation in the source code.
One possible implementation
The first thing which comes to my mind (and I am especially looking for other solutions or feedback on this one) would be to create an Enum
ECarTypeDefinitions
.
public enum ECarTypeDefinitions
{
SportsCar = 1,
SUV = 2,
Truck = 3
}
Now that we have a type-safe Enum
we can use it e.g. like this:
public bool IsSportsCar(Car currentCar)
{
return (currentCar.CarType == ECarTypeDefinitions.SportsCar);
}
The contents of that enum would be auto-generated from the contents of the CarTypeDefinitions
table (add two additional columns for name of the enum and its integer value).
This would also work the other way, e.g. generate the content of the CarTypeDefinitions
DB table from the ECarTypeDefinitions
Enum
.
I'm keen to hear about other ways how to tackle this problem. How have you dealt with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我按照你建议的方式做。其他一些人更喜欢将所有常量组合到“查找”表中。您可以在此处查看一些优点和缺点的示例: http://weblogs.foxite.com/andykramek/archive/2009/05/10/8419.aspx
编辑:这里的一个想法可能有助于激发您进一步的想法。
为每种汽车类型创建一个类:
现在向 CarTypeDefinition 添加一个属性:
使用 typeof 填充新属性(对于您拥有的每种类型):
最后您的新 IsSportsCar 方法:
我不熟悉实体框架,所以也许它有办法让这种事情做得更加干净利落。对 C# 也有点生疏。
I do it the way you have suggested. Some others prefer to combine all constants into a "Lookup" table. You can look at an example of some of the pros and cons here: http://weblogs.foxite.com/andykramek/archive/2009/05/10/8419.aspx
Edit: Here's a thought that may help spark further ideas from you.
Create a class for each Car Type:
Now add an attribute to CarTypeDefinition:
Populate the new attribute (for each type you have) using typeof:
Finally your new IsSportsCar method:
I'm not familiar with Entity Framework so perhaps it has a way to allow this kind of thing to be more cleanly done. Also a little rusty on C#.