1

所以我想向你们提出这个问题,这很简单,但我想找到更好的方法。

我们的数据应始终为大写,但我们允许应转换的小写输入。

toUpper通常我们会在保存到数据库时做一个。

我在想toUpper在 getter 中可能会更好,这样逻辑对数据更紧密,只要你使用 Object 来查看这些数据,那么它总是大写的。

public string Mapping
{ 
    get
    {
        return mapping  == null ? mapping : mapping.ToUpper();
    }
    set;
}

这是哑巴吗?我知道Mapping.ToUpper()每次都会创建一个新字符串,所以看起来很愚蠢。还有其他建议吗?

4

1 回答 1

3

I would convert the data on the way in, unless there is some compelling reason to keep the original data around. That way you ensure that you can't accidentally use the "bad" version of the data (for example, in a private method that accesses the backing field).

Personally I prefer that property setters always preserve the value, so that:

x.MyProperty = someValue;
Assert(x.MyProperty == someValue);

If the setter would change the value it is passed, I instead write a separate method to do it.

In your example, I'd probably go for:

public void SetMapUppercase(string value);
于 2013-03-14T20:31:45.477 回答