枚举绑定到数据库 [英] Enum Bound to Database

查看:170
本文介绍了枚举绑定到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为现有应用程序编写代码时,通常,开发数据库环境与生产环境不匹配 - 更糟糕的是,仍然存在覆盖环境的方法并不是一个选择。



我想到的所有环境的代码都是使用一个数据绑定枚举,其值将被绑定到它们所代表的数据项的ID。我无法使用枚举,但我能够通过抽象类来解决它。例如:

  public abstract class Colors 
{
private static readonly string c_red =red;
private static readonly string c_blue =blue;
private static readonly string c_yellow =yellow;
private static readonly string c_green =green;

private static int? _red = null;
private static int? _blue = null;
private static int? _yellow = null;
private static int? _green = null;

public static int Red
{
get
{
if(_red == null)
_red = GetColorID(c_red);

return(int)_red;
}
}
public static int Blue
{
get
{
if(_blue == null)
_blue = GetColorID(c_blue);

return(int)_blue;
}
}
public static int Yellow
{
get
{
if(_yellow == null)
_yellow = GetColorID(c_yellow);

return(int)_yellow;
}
}
public static int Green
{
get
{
if(_green == null)
_green = GetColorID(c_green);

return(int)_green;
}
}

private static int GetColorID(string identifier)
{
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [Demo ] .ConnectionString))
{
conn.Open();

using(SqlCommand cmd = new SqlCommand(spGetColorId,conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(Name,identifier);

return Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
}

这样做,我可以在这个例子中调用 Colors.Red 来获取红色的ID,而不管我是否在开发,测试或生产中。



我的问题是:这真的是完成这个的理想方法吗?有没有一个本机到C#的数据绑定枚举方法,或者与上面我所做的相似的东西?

解决方案

枚举意味着价值观很少,如果有的话,会发生变化。您可以将其视为关闭的值列表(如星期几等)。由于枚举的这种性质,我觉得可以让这个冗余的枚举基础值被指定两次(一次在数据库中,另一个在枚举中)本身)。



如果您担心差异,您可以在应用程序启动时运行对值的验证,并检查值是否具有正确的相应ID,并且枚举中的值数与DB中的值的数量相匹配。


When writing code for existing applications, often times the development database environment does not match the production environment - and even worse, there are times still where overlaying the environments is just not an option.

One idea I had in mind to code for all environments would be to use a databound enum, whose values would be bound to the ID of the data item they represent. I couldn't get that to work with an Enum but I was able to solve it via abstract classes. For example:

public abstract class Colors
{
    private static readonly string c_red    = "red";
    private static readonly string c_blue   = "blue";
    private static readonly string c_yellow = "yellow";
    private static readonly string c_green  = "green";

    private static int? _red    = null;
    private static int? _blue   = null;
    private static int? _yellow = null;
    private static int? _green  = null;

    public static int Red
    {
        get
        {
            if (_red == null)
                _red = GetColorID(c_red);

            return (int)_red;
        }
    }
    public static int Blue
    {
        get
        {
            if (_blue == null)
                _blue = GetColorID(c_blue);

            return (int)_blue;
        }
    }
    public static int Yellow
    {
        get
        {
            if (_yellow == null)
                _yellow = GetColorID(c_yellow);

            return (int)_yellow;
        }
    }
    public static int Green
    {
        get
        {
            if (_green == null)
                _green = GetColorID(c_green);

            return (int)_green;
        }
    }

    private static int GetColorID(string identifier)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Demo"].ConnectionString))
        {
            conn.Open();

            using (SqlCommand cmd = new SqlCommand("spGetColorId", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("Name", identifier);

                return Convert.ToInt32(cmd.ExecuteScalar());
            }
        }
    }
}

By doing it this way, I'm able to call Colors.Red in this example to get the ID of Red regardless of if I'm in Dev, Testing, or Production.

My question is: is this really the ideal method of accomplishing this? Is there a native-to-C# method of databinding enums, or something equivalent to what I'm doing above?

解决方案

Having an enum implies that the values are rarely, if ever, changes. you can think of it as a closed list of values (like days of the week etc.).

Because of this nature of enums, I find it acceptable to have this redundancy of having the enum underlying value being specified twice (once in the DB and the other in the enum itself).

If you are worried about discrepancies, you can run a validation for the values when your application starts and check that the values has the correct corresponding ids and that the number of values in the enum matches the number of values in the DB.

这篇关于枚举绑定到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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