简化嵌套if else条件

Dev*_*iya 1 c# asp.net if-statement nested-if

我有这个嵌套的if else条件.我想要的检查流程在下面的代码中描述.

if (HiringManagerAPPROVED)
{ 
    //email reporting gropu
}
else if (ReportingGroupAPPROVED)
{ 
    //email Hiringmanager
}
else if (HiringManagerReAPPROVED)
{ 
    //email PPO
}                }
else if (PpoAPPROVED)
{ 
    //email Finance
}
else if (FinanceAPPROVED)
{ 
    //email president & COO
}
else if (PresidentCooAPPROVED)
{ 
    //email hr
}
else if (HRAPPROVED)
{
    //email Hiring Manager
}
Run Code Online (Sandbox Code Playgroud)

如何减少支票数量,保持支票流量不变.

Sam*_*ica 5

Finance,Ppo以及HiringManager可能是用户,但是他们看我看上去更像角色.

无论哪种方式,您最终可能会做的是拥有一组Role对象,每个对象都有一个IsApproved字段

public class Role
{
    public RoleType Type { get; set; }
    public bool IsApproved { get; set; }
    public string EmailAddress { get; set; }
    //etc
}

public enum RoleType
{
    Finance,
    HiringManager,
    //etc
}
Run Code Online (Sandbox Code Playgroud)

执行此操作后,您可以使用此替换if语句

List<Role> roles = new List<Role>();
// populate your roles how you like

foreach (Role role in roles)
{
    if (role.IsApproved)
    {
        // email roll
    }
}
Run Code Online (Sandbox Code Playgroud)

请记住,如果您将要处理这些角色,如果它们获得批准,则会发送电子邮件,那么您不会使用此方法获得太多优势.

当您必须根据角色的批准方式或根据其他标准对角色执行其他操作时,优势就来了.