c# - 各种对象的可重复逻辑

标签 c# .net linq oracle refactoring

代码如下:

return new DistrictInfo { 
    rid = Convert.ToUInt32(((OracleNumber)o.GetOracleValue("rid")).Value), 
    doc = Convert.ToUInt16(((OracleNumber)o.GetOracleValue("doctor")).Value), 
    secdoc = Convert.ToUInt16(((OracleNumber)o.GetOracleValue("secdoctor")).Value), 
    num = Convert.ToUInt16(((OracleNumber)o.GetOracleValue("num")).Value), 
    docname = o.GetOracleValue("doctorname") as string, 
    slpuname = o.GetOracleValue("lpuname") as string, 
    reason = o.GetOracleValue("reason") as string, 
    secdocname = o.GetOracleValue("secdocname") as string 
};

现在我需要重写这段代码来检查对象属性是否存在。这应该是这样一段代码:

DistrictInfo di;
if (!(o["rid"].Equals(DBNull.Value)) && !(o.GetOracleValue("rid").Equals(DBNull.Value)) && (((OracleNumber)o.GetOracleValue("rid")).Value != null))
{
    di.rid = Convert.ToUInt32(((OracleNumber)o.GetOracleValue("rid")).Value);
}

但是我发现这段代码有点笨拙而且不够优雅。我做了很多检查,因为我想逃避异常。 所以问题是我们如何重构这段代码?告诉我你的想法。我认为没有必要进行如此多的检查。另一件事我们需要指定各种对象属性名称来为所有各种成员做一个代码块。我认为可以为此使用 LINQ。同样在第一段代码中,我们看到了不同的转换,因此我们需要在新代码中提及它。 提前谢谢大家!

附言用于处理数据库的库是来自 devArt 的 dotConnect for Oracle。

最佳答案

我建议您使用 UInt32.TryParse()它提供了一种将原始字符串值转换为 UInt32 的安全方法,有两种情况 - 值可以转换为 UInt32 或不能。所以基本上检查 TryParse() 方法的返回值以查看值是否已成功转换。

所以

string rawValue = o["rid"].ToString();
UInt32 parsedValue;
if (UInt32.TryParse(rawValue, out parsedValue))
{
    // was converted successfully
}
else
{
    // was not converted
}

关于通过名称处理多个属性这一过程的自动化,您还需要考虑属性类型,因此我看不到利用 LINQ 实现此目的的一个不错的方法。

编辑:添加了关于字段转换自动化的建议

您可以利用 Extension Methods .NET 的特性,通过一组有用的方法来装饰 OracleObject 类型。

public static class OracleObjectExtensions
{
    public static UInt32 GetUInt32Value(this OracleObject oracleObject, string fieldName)
    {
        UInt32 returnValue = default(UInt32);

        if (oracleObject[fieldName] != null)
        {
            string rawValue = oracleObject[fieldName].ToString();
            UInt32.TryParse(rawValue, out returnValue);                
        }

        return returnValue;
    }

    public static UInt16 GetUInt16Value(this OracleObject oracleObject, string fieldName)
    {
        UInt16 returnValue = default(UInt16);

        if (oracleObject[fieldName] != null)
        {
            string rawValue = oracleObject[fieldName].ToString();
            UInt16.TryParse(rawValue, out returnValue);
        }

        return returnValue;
    }
}

编辑 2: Extension Methods描述

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

关于c# - 各种对象的可重复逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442879/

相关文章:

c# - SingleOrDefault 异常处理

c# - 如何填充元素数量由变量确定的元组列表

c# - 从 .Net 发送电子邮件

.net - LINQ 和 .COUNT 超时

c# - 内存中集合的 Linq 性能

arrays - LINQ - 根据数组 B 上的值从数组 A 中选择元素

c# - 如何读取文本文件并循环遍历重复部分?

c# - 在 Windows Phone c# 中解析 JSON

c# - 迭代数组并将强类型添加到列表中

c# - 集合内的 Xml 序列化