枚举绑定到数据库

编程入门 行业动态 更新时间:2024-10-24 16:30:49
本文介绍了枚举绑定到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我想到的所有环境的代码都是使用一个数据绑定枚举,其值将被绑定到它们所代表的数据项的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.

更多推荐

枚举绑定到数据库

本文发布于:2023-10-16 00:34:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!