如何为枚举char(1)数据库字段实现C#枚举? [英] How to implement C# enum for enumerated char(1) database field?

查看:119
本文介绍了如何为枚举char(1)数据库字段实现C#枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个char(1)类型的数据库字段,其中包含少量可能的状态代码(例如'F'=失败,'U'=未知等).我想拥有一个与这些状态相对应的C#枚举类.我可以做到:

OK, so I have a database field of type char(1) that has a small number of possible state codes (e.g. 'F'= Failure, 'U'=Unknown, etc.). I'd like to have a C# enum class that corresponds to these states. I can do:

public enum StatusCode : byte {
    Unknown = (byte) 'U',
    Failure = (byte) 'F',
    // etc.
}

到目前为止,一切都很好.但是在从数据库返回的DataTable中,列值是System.Data.SqlTypes.SqlString实例.从C#字符串(甚至C#字符)转换为C#字节显然存在一些问题(因为C#字符实际上是UTF-16代码点).但是在这种情况下,我知道这些值被限制在一个小的集合中,并且如果该集合之外的值通过,则代码应引发异常.

So far so good. But in the DataTable returned from the database, the column values are System.Data.SqlTypes.SqlString instances. There are obviously some issues converting from a C# string (or even a C# char) to a C# byte (since C# char is really a UTF-16 codepoint). But in this case I know the values are constrained to a small set, and the code should throw an exception if a value outside this set comes through.

牢记这一点,最好的方法是什么?从SqlString强制转换为字节是否安全? Convert.ToByte()会更好吗?仅仅使用switch/case构造将值横穿枚举会更好吗?

With that in mind, what's the best way of doing this? Is it safe to cast from a SqlString to a byte? Would Convert.ToByte() be better? Would it be better to simply use a switch/case construct to crosswalk the values into the enum?

我正在寻找最佳"方法,不仅要获得正确的结果,而且还要使代码清晰.我想我也可以只使用一些常量,例如

I'm looking for the "best" way to do this, not only in terms of getting the right results but also for code clarity. I suppose I could also just use some constants like

public const char UnknownStatus = 'U';
public const char FailureStatus = 'F';

但是如果可能的话,我宁愿使用一个枚举.有什么想法吗?

But I'd rather use an enum if possible. Any thoughts?

为了阐明我要怎么做,我希望在我的代码中经常使用这些值.例如,我希望能够执行以下操作:

To clarify what I want do do with this, I'm expecting to use these values frequently throughout my code. For example, I want to be able to do things like:

public void DoSomething(StatusCode currentStatus) {
    if(currentStatus == StatusCode.Failure) {
        throw new SomeException();
    }

    switch(currentStatus) {
        case StatusCode.Unknown:
            // do something
            break;
    }
}

以此类推.我特别想避免这样的事情:

And so forth. I particularly want to avoid things like:

public void DoSomething(char currentStatus) {
    if(currentStatus == 'F') {
        // do something
    }
}

因为在这种情况下,我到处都在使用等于魔数"的数字.特别是,这实际上将使迁移到其他状态标记系统成为不可能.这有道理吗?

Since in this case I'm using what amounts to "magic numbers" all over the place. In particular, this would make migrating to some other state-flagging system virtually impossible. Does that make sense?

推荐答案

也许是常量"对象?

public sealed class StatusCode {
    private char value;

    public static readonly StatusCode Unknown = new StatusCode('U');
    public static readonly StatusCode Failure = new StatusCode('F');

    private StatusCode(char v) {
        value = v;
    }

    public override string ToString() {
        return value.ToString();
    }

}

然后,在您的代码后面,您可以像枚举一样使用它:StatusCode.Unknown.您还可以提供一种内部方法,将收到的值解析"为StatusCode对象.

Then, later in your code, you could use it like an enum: StatusCode.Unknown. You could also provide an internal method to 'parse' a received value into an object of StatusCode.

这篇关于如何为枚举char(1)数据库字段实现C#枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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