2015-12-15 39 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();