I have switch
statement that is switching on an enum
to build a ContextMenu
depending on what the selected enum
value is. While the various enum
values require different operations in their case
s of the switch
, some of them require some of the same operations.
All of the MenuItem
s are declared as readonly
and initialized in the constructor of the class. This method is called whenever the QueryType
value changes, and is set to a property that is bound to by a ContextMenu
in my view.
Switching to if/else
would allow for 'circuit simplification' per se, but at the expense of some readability (and the ease of ordering the MenuItem
s).
private List<MenuItem> GenerateContextMenuItems()
{
var contextMenuItems = new List<MenuItem>();
switch (QueryType)
{
case QueryType.ContextComputerGroups:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
break;
case QueryType.ContextDirectReportDirectReports:
contextMenuItems.Add(
_directReportGetDirectReportsMenuItem);
contextMenuItems.Add(_directReportGetGroupsMenuItem);
contextMenuItems.Add(_directReportGetSummaryMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextDirectReportGroups:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextGroupComputers:
contextMenuItems.Add(_computerGetGroupsMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
break;
case QueryType.ContextGroupUsers:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextGroupUsersDirectReports:
contextMenuItems.Add(
_directReportGetDirectReportsMenuItem);
contextMenuItems.Add(_directReportGetGroupsMenuItem);
contextMenuItems.Add(_directReportGetSummaryMenuItem);
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextGroupUsersGroups:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextUserDirectReports:
contextMenuItems.Add(
_directReportGetDirectReportsMenuItem);
contextMenuItems.Add(_directReportGetGroupsMenuItem);
contextMenuItems.Add(_directReportGetSummaryMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextUserGroups:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.OuComputers:
contextMenuItems.Add(_computerGetGroupsMenuItem);
contextMenuItems.Add(_computerGetSummaryMenuItem);
break;
case QueryType.OuGroups:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_groupGetSummaryMenuItem);
break;
case QueryType.OuUsers:
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.OuUsersDirectReports:
contextMenuItems.Add(
_directReportGetDirectReportsMenuItem);
contextMenuItems.Add(_directReportGetGroupsMenuItem);
contextMenuItems.Add(_directReportGetSummaryMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.OuUsersGroups:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
contextMenuItems.Add(_userGetSummaryMenuItem);
break;
case QueryType.ContextComputerSummary:
contextMenuItems.Add(_computerGetGroupsMenuItem);
break;
case QueryType.ContextDirectReportSummary:
contextMenuItems.Add(
_directReportGetDirectReportsMenuItem);
contextMenuItems.Add(_directReportGetGroupsMenuItem);
break;
case QueryType.ContextGroupSummary:
contextMenuItems.Add(_groupGetComputersMenuItem);
contextMenuItems.Add(_groupGetUsersMenuItem);
contextMenuItems.Add(_groupGetUsersDirectReportsMenuItem);
contextMenuItems.Add(_groupGetUsersGroupsMenuItem);
break;
case QueryType.ContextUserSummary:
contextMenuItems.Add(_userGetDirectReportsMenuItem);
contextMenuItems.Add(_userGetGroupsMenuItem);
break;
default:
throw new ArgumentOutOfRangeException();
}
return contextMenuItems;
}