0
考慮這個DAL類似的代碼:擺脫泛型類型參數:是否有可能?
mDealerTypes = Read<Dictionary<string, DealerType>, DealerType>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.DealerTypeKey, o));
mCountries = Read<Dictionary<string, Country>, Country>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.Code, o));
private C Read<C, T>(Func<SqlConnection, IDataReader> func, Action<C, T> a)
where C : ICollection, new()
where T : EntityBase, new()
{
C objects = new C();
using (IDataReader reader = func(connection))
{
while (reader.Read())
{
T obj = new T();
obj.Init(reader);
a(objects, obj);
}
}
return objects;
}
爲了增強可讀性,我想以某種方式改變閱讀<的類型參數列表中沒有重複兩次被存儲在集合在此,以使對象>。
mCountries = Read<Dictionary<string, Country>>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.Code, o));
但是如何重寫讀取<>然後呢?這甚至有可能嗎?
私人??閱讀(Func鍵FUNC,行動一)
我個人倒覺得它更具可讀性總是從DAL方法返回一個平坦的枚舉或集合,然後讓調用者通過調用.ToDictionary(o => o.DealerTypeKey)構建字典。 – Joe