我有这个简单的应用程序正在检查Employee Position,然后它根据一些业务规则返回html表(实际上我使用了repeater而不是html表)和信息。一切都很简单,但后来我有了想法如果员工在公司有2个职位怎么办?我需要根据两个职位返回带有信息的html表。现在我有可能包含1个以上职位的职位列表listPositions
。我的代码(业务逻辑)看起来像这个:
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
作为参数并返回我绑定到rptEmployees
repeatar的EmployeeInfo对象。
其他代码(业务逻辑)我认为应该是这样的:
foreach (var position in listPositions)
{
if (actions.ContainsKey(position))
{
actions[position]();
}
}
但这显然是错误,因为当列表中有多个位置时,这部分代码首先将repeater
与第一个位置的信息绑定,然后在第二个绑定后,这些信息将丢失。
是否有可能重构业务逻辑并获取两个职位的信息而无需更改Dictionary
段中的代码?或者我应该尝试不同的方法?
谢谢!
答案 0 :(得分: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();