如何将存储在数据库中的定义用于源代码中的定义?

发布于 2024-11-03 09:12:07 字数 1249 浏览 0 评论 0原文

我想了解如何将存储在数据库(由您的应用程序使用)中的定义使用到应用程序的源代码中。

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2024-11-10 09:12:07

我按照你建议的方式做。其他一些人更喜欢将所有常量组合到“查找”表中。您可以在此处查看一些优点和缺点的示例: http://weblogs.foxite.com/andykramek/archive/2009/05/10/8419.aspx

编辑:这里的一个想法可能有助于激发您进一步的想法。

为每种汽车类型创建一个类:

public class SportsCarType
{
}

现在向 CarTypeDefinition 添加一个属性:

public class CarTypeDefinition
{
    ...
    public string typeName;
}

使用 typeof 填充新属性(对于您拥有的每种类型):

...
carTypeDefinition.typeName = typeof(SportsCarType).Name;
...

最后您的新 IsSportsCar 方法:

public bool IsSportsCar(Car currentCar)
{
    return (currentCar.CarType.typeName == typeof(SportsCarType).Name);
}

我不熟悉实体框架,所以也许它有办法让这种事情做得更加干净利落。对 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:

public class SportsCarType
{
}

Now add an attribute to CarTypeDefinition:

public class CarTypeDefinition
{
    ...
    public string typeName;
}

Populate the new attribute (for each type you have) using typeof:

...
carTypeDefinition.typeName = typeof(SportsCarType).Name;
...

Finally your new IsSportsCar method:

public bool IsSportsCar(Car currentCar)
{
    return (currentCar.CarType.typeName == typeof(SportsCarType).Name);
}

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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文