2013-12-12 64 views
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,行动一)

+0

我个人倒觉得它更具可读性总是从DAL方法返回一个平坦的枚举或集合,然后让调用者通过调用.ToDictionary(o => o.DealerTypeKey)构建字典。 – Joe

回答

1

创建一个简单的包装方法(或扩展方法)

public Dictionary<T1, T2> ReadEx<T1, T2>(...) where T2 : EntityBase 
{ 
    return Read<Dictionary<T1, T2>, T2>(...) 
} 
+0

得到它与T2:new()添加。谢谢 – Laurijssen

相关问题