2015-12-15 42 views
0

我有這個簡單的應用程序正在檢查Employee Position,然後根據某些業務規則返回html表(實際上我使用repeater而不是html表)。 ,但後來我知道如果員工在公司中有2個職位,並且我需要根據兩個職位的信息返回html表。現在我有位置listPositions的列表,其中可能包含多於1個職位。我的代碼(業務邏輯)是這樣的:使用DataSource和DataBinding重構代碼

Dictionary<string, Action> actions = new Dictionary<string, Action>() 
{ 
    {"Admin", new Action(() => 
      {rptEmployees.DataSource = spc.GetEmployeeInfo(Models.PhoneNumbers.AllEmployee); 
      rptEmployees.DataBind();}) }, 

    {"OfficeDirector", new Action(() => 
     {rptEmployees.DataSource = spc.GetEmployeeInfo(Models.PhoneNumbers.OfficeEmployee); 
     rptEmployees.DataBind();})}, 

    {"RegularUser", new Action(() => 
      {rptEmployees.DataSource = spc.GetEmployeeInfo(Models.PhoneNumbers.OfficeDepartmentEmployee); 
      rptEmployees.DataBind();})} 

    }; 

我的方法GetemployeeInfo()正在SPListItemCollection作爲參數和返回,我結合牛逼EmployeeInfo對象重複使用rptEmployees。代碼(業務邏輯)的

休息我想應該是這樣的:

foreach (var position in listPositions) 
     { 
      if (actions.ContainsKey(position)) 
      { 
       actions[position](); 
      } 
     }   

但很明顯的錯誤,因爲當出現在列表中超過1點的位置,這部分代碼首先結合repeater與第一個位置和第二個綁定後的信息都會丟失。 有沒有任何posibility重構業務邏輯和獲取兩個職位的信息,而不改變Dictionary段的代碼?或者我應該嘗試不同的方法? 謝謝!

回答

0

我找到了解決我的問題的方法:)它非常簡單。 首先,我創建的列表List<Models.EmployeeInfo> Employees

那麼簡單以前的代碼進行修改:

Dictionary<string, Action> actions = new Dictionary<string, Action>() 
{ 
    {"RegularUser", new Action(() => { Employees.AddRange(spc.GetEmployeeInfo(Models.PhoneNumbers.OfficeDepartmentEmployee));})}, 
    {"DeliveryManager", new Action(() => {Employees.AddRange(spc.GetEmployeeInfo(Models.PhoneNumbers.OfficeDepartmentEmployee));})},     
    {"OfficeDirector", new Action(() => {Employees.AddRange(spc.GetEmployeeInfo(Models.PhoneNumbers.OfficeEmployee));})}, 
    {"Admin", new Action(() => {Employees.AddRange(spc.GetEmployeeInfo(Models.PhoneNumbers.AllEmployee));})} 

}; 

    foreach (var position in listPositions) 
    { 
     if (actions.ContainsKey(position)) 
     { 
     actions[position](); 
     } 
    } 

    rptEmployees.DataSource = Employees; 
    rptEmployees.DataBind();