I have this method with a lot if statementsin which I filter SharePoint listbased on the employee’s position. The result is query stringthat which is passed as a parameter to another method QuerySPList.
public List<Phone> GetListOfPhones(Employee emp)
{
List<Phone> records = new List<Phone>();
string query = string.Empty;
if (emp.Positions.Count == 1 && emp.Positions[0] == "Regular")
{
query = "";
}
if (emp.Positions.Count == 1 && emp.Positions[0] == "OfficeDirector")
{
query = "";
}
if (emp.Positions.Count == 1 && emp.Positions[0] == "Admin")
{
query = "";
}
if (emp.Positions.Count == 2 && emp.Positions.Contains("Regular") && emp.Positions.Contains("OfficeDirector"))
{
query = "";
}
var rawItems = QuerySPList(query);
foreach (SPListItem item in rawItems)
{
}
return records;
}}
, strategy pattern if, , , , - , , . , , .
, interface IRoleHandler 3 classes, RoleAdmin ,RoleRegular,RoleOfficeDirector. - :
public interface IRoleHandler<T>
{
string handleRole(T obj);
}
public class RoleAdmin:IRoleHandler<Employee>
{
public string handleRole(Employee emp)
{
if (emp.Positions.Count == 1 && emp.Positions[0] == "Admin")
{
}
}
}
dictionary, :
Dictionary<string, IRoleHandler<Employee>> strategyHandlers = new Dictionary<string, IRoleHandler<Employee>>();
strategyHandlers.Add("Admin", new RoleAdmin());
strategyHandlers.Add("Regular", new RoleRegularUser());