0
基本上我想知道是否使用代碼合約來確定ConcurrentDictionary中是否存在密鑰是代碼合同的可接受使用。它不適合我,因爲它不僅僅是參數檢查,因爲它取決於運行時字典的狀態。正確使用CodeContract需要
public class MyClass
{
private ConcurrentDictionary<string, object> someItems =
new ConcurrentDictionary<string, object>();
public object GetItem(string itemName)
{
Contract.Requires<ArgumentNullException>(!String.IsNullOrWhiteSpace(itemName));
// ?? Is this a correct alternative to checking for null???
Contract.Requires<KeyNotFoundException>(someItems.ContainsKey(itemName));
return someItems[itemName];
}
}
但如果它是好的,它有2個Contract.Requires和一個返回,在低於傳統方式清潔方法。
public class MyClass
{
private ConcurrentDictionary<string, object> someItems =
new ConcurrentDictionary<string, object>();
public object GetItem(string itemName)
{
Contract.Requires<ArgumentNullException>(!String.IsNullOrWhiteSpace(itemName));
// Traditional null check
var item = someItems[itemName];
if (item == null)
{
throw new KeyNotFoundException("Item " + itemName + " not found.");
}
return item;
}
}
KeyNotFoundException對一個方法GetItem有一個參數幾乎告訴我什麼是錯誤的;然而,我傾向於對這個特殊的測試本身並不是合同有同樣的感覺。拋出與返回null是API的設計考慮因素,而不是真正的代碼合同問題。總的來說,我認爲你已經用良好的觀點驗證了我的同樣關切。謝謝! – GoClimbColorado 2013-03-16 18:00:52