使用 DataSource 和 DataBinding 重构代码

Refactoring code with DataSource and DataBinding

我有一个简单的应用程序,它正在检查 Employee Position ,然后它 returns html table(实际上我使用了中继器而不是 html table) 根据一些业务 rule.Everything 的信息很容易,但后来我想到如果员工在公司有 2 个职位怎么办,我需要 return html table 的信息根据 positions.Now 我有职位列表 listPositions 可能包含超过 1 position.My 代码(业务逻辑)看起来像这样:

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 作为参数并 returning 我绑定到 rptEmployees repeatar 的 EmployeeInfo 对象。

其余代码(业务逻辑)我认为应该是这样的:

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

但是很明显的bug,因为当列表中的位置超过1个时,这部分代码先绑定repeater第一个位置的信息,第二次绑定后,这些信息就丢失了。 是否可以在不更改 Dictionary 段中的代码的情况下重构业务逻辑并获取两个职位的信息?还是我应该尝试不同的方法? 谢谢!

我找到了解决我的问题的方法 :) 这真的很简单。 首先,我创建了列表 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();