1
映射枚舉考慮下面的代碼片段爲何在調試時不能在運行時檢查時,我得到一個異常/什麼是從DB
public class Class1
{
public enum TestEnum
{
Value1 = 1,
Value2 = 2
}
public void TestCall()
{
/*some standard DB code returning an SqlDataReader...*/
SqlDataReader rdr = com.ExecuteReader();
Item item = new Item();
/*original code*/
/*Database "Type" is a varchar() field containing an integer, please dont ask why :)*/
if (rdr["Type"].GetType() == typeof(DBNull))
{
item.Type = TestEnum.Value1;
}
else if ((string)rdr["Type"] == "1")
{
item.Type = TestEnum.Value2;
}
else if ((string)rdr["Type"] == "2")
{
item.Type = TestEnum.Value1;
}
else
{
item.Type = TestEnum.Value1;
}
/*suggested code*/
item.Type = rdr["Type"] as TestEnum? ?? TestEnum.Value1; //<- default/null value to use
}
}
public class Item
{
public Class1.TestEnum Type;
}
在代碼審查,我的一個同事的最佳實踐指出我可以用一行代碼(「原代碼」)替換級聯IF(「原代碼」)
雖然建議的代碼運行得很好,但在檢查「rdr [」Type「]時,我得到一個NullReferenceException作爲TestEnum?「在調試時。
我想知道這是否是建議代碼的底層問題的標誌,將數據庫值映射到枚舉的最佳方式是什麼,以及您對這類代碼的總體思路是什麼。
正常的方法是閱讀數據庫值作爲int並將其轉換爲枚舉,如果它們具有相同的基礎值。 –
c#代碼的字段是一個整數(更具體地說是一個枚舉),但數據庫類型是一個varchar();這是奇怪的,不推薦,但超出我的控制(數據庫來自客戶端,不能修改) –
然後,我建議的方法可能是你應該做的。如果你只有幾個值(比如那些2),那麼你的方法無論如何是好的 –