如何让这个重复的代码更优雅?

leo*_*ora 0 c# refactoring

我有下面这个重复的代码,我假设这可以合并,但如果你注意到每个字典是不同的通用字典:

dictionary1是类型的

Dictionary<int, ContinuousIntegrationSolution>  
Run Code Online (Sandbox Code Playgroud)

而dictionary2的类型是:

Dictionary<int, BugTracker>

      DataTable dt = GetDataTable("CI");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            int id = Convert.ToInt32(dr["id"]);
            string name = dr["name"].ToString();
            _dictionary1[id] = new ContinuousIntegrationSolution(){Name = name};
        }

        DataTable dt1 = GetDataTable("Bug_Tracking");

        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            DataRow dr = dt1.Rows[i];
            int id = Convert.ToInt32(dr["id"]);
            string name = dr["name"].ToString();
            _dictionary2[id] = new BugTracker() { Name = name };
        }

        DataTable dt2 = GetDataTable("SDLC");

        for (int i = 0; i < dt2.Rows.Count; i++)
        {
            DataRow dr = dt2.Rows[i];
            int id = Convert.ToInt32(dr["id"]);
            string name = dr["name"].ToString();
            _dictionary3[id] = new SDLCProcess() { Name = name };
        }
Run Code Online (Sandbox Code Playgroud)

注意:我修复了下面提到的一些拼写错误.

Shu*_*oUk 13

public interface INameable
{
    string Name {get;set;}
}

public static IDictionary<int, T> ReadTable<T>(string tableName)
    where T : INameable, new()
{
    DataTable dt = GetDataTable(tableName);
    var dictionary = new Dictionary<int, T>();

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataRow dr = dt.Rows[i];
        int id = Convert.ToInt32(dr["id"]);
        string name = dr["name"].ToString();
        dictionary[id] = new T() { Name = name };
    }
    return dictionary;
}
Run Code Online (Sandbox Code Playgroud)

如果您拥有c#4.0动态,则可以避免因某些(轻微)类型安全性丢失而导致INameable

另一个类似于Hakon的答案,但没有暴露字典

public IDictionary<int,T> ReadTable<T>(
    string tableName, Action<T, string> onName) 
    where T : new()
{
    var dictionary = new Dictionary<int,T>();
    DataTable table = GetDataTable(tableName);
    foreach (DataRow row in table.Rows) 
    {
        int id = Convert.ToInt32(row["id"]);
        string name = row["name"].ToString();
        var t = new T();
        onName(t, name);
        dictionary[id] = t;
    }
    return dictionary;
}
Run Code Online (Sandbox Code Playgroud)

然后像这样消耗:

var ci   = ReadTable<ContinuousIntegrationSolution>("CI", 
              (t, name) => t.Name = name);  
var bt   = ReadTable<BugTracker >("Bug_Tracking", 
              (t, name) => t.Name = name);  
var sdlc = ReadTable<SDLCProcess>("SDLC", 
              (t, name) => t.Name = name);  
Run Code Online (Sandbox Code Playgroud)

一种替代的,更灵活的方法,但由于类型推断在呼叫站点使用仍然相当简单,将是:

public IDictionary<int,T> ReadTable<T>(string tableName, Func<string,T> create) 
{
    DataTable table = GetDataTable(tableName);
    var dictionary = new Dictionary<int,T>()
    foreach (DataRow row in table.Rows) 
    {
        int id = Convert.ToInt32(row["id"]);
        string name = row["name"].ToString();
        dictionary[id] = create(name);
    }
    return dictionary;
}
Run Code Online (Sandbox Code Playgroud)

然后像这样消耗:

var ci   = ReadTable("CI", 
               name => new ContinuousIntegrationSolution() {Name = name});  
var bt   = ReadTable("Bug_Tracking", 
               name => new BugTracker() {Name = name});
var sdlc = ReadTable("SDLC", 
               name => new SDLCProcess() {Name = name});
Run Code Online (Sandbox Code Playgroud)

如果你采用lambda方法,我会建议后者.

  • 我同意,但这将改变所提供的程序的语义(我承认一个微妙的变化)所以我倾向于在没有必要的时候纠正这些事情. (2认同)