这是松结对编程的第22篇(专栏目录)。
接前文
public static MvcHtmlString StatusFiltersDropdownList(WebViewPage page) { var allStatuses = Status.AllStatuses().ToList(); const string key = "statusIds"; var currentStatusIds = page.ParameterOf(key); //Status groups filters on the top. var linksLeft = new Dictionary<string, IEnumerable<Status>> { {"所有状态", allStatuses}, {"所有正常", allStatuses.Where(i => i.IsNormal)}, {"所有开放", allStatuses.Where(i => !i.IsClosed)}, {"所有故事板", allStatuses.Where(i => i.IsDisplayedOnKanban)}, }; var linksListLeft = AddToList(page, key, currentStatusIds, linksLeft); var linksRight1 = new Dictionary<string, IEnumerable<Status>> { {"0", null}, {"所有已放弃或推迟", allStatuses.Where(i => !i.IsNormal)}, {"所有关闭", allStatuses.Where(i => i.IsClosed)}, {"所有非故事板", allStatuses.Where(i => !i.IsDisplayedOnKanban)} }; var linksListRight1 = AddToList(page, key, currentStatusIds, linksRight1); //Single status filters on the bottom. linksListLeft.Add(new MvcHtmlString("<hr/>")); linksListLeft.AddRange(allStatuses.Where(i => i.Value >= 0) .Select(status => status.Link(outerLink: page.MergeParameter(key, "_" + status.ID + "_"), title: status.Value.ToString(), displayAsBoldText: "_" + status.ID + "_" == currentStatusIds))); linksListLeft.Add(new MvcHtmlString("<hr/>")); linksListLeft.AddRange(allStatuses.Where(i => i.Value < 0) .Select(status => status.Link(outerLink: page.MergeParameter(key, "_" + status.ID + "_"), title: status.Value.ToString(), displayAsBoldText: "_" + status.ID + "_" == currentStatusIds))); //Current value text var currentValueText = ""; MvcHtmlString currentValue = null; currentValueText = linksLeft.SingleOrDefault(i => i.Value != null && currentStatusIds == i.Value.Aggregate<Status, string>(null, (current, status) => current + "_" + status.ID + "_")) .Key ?? currentValueText; currentValueText = linksRight1.SingleOrDefault(i => i.Value != null && currentStatusIds == i.Value.Aggregate<Status, string>(null, (current, status) => current + "_" + status.ID + "_")) .Key ?? currentValueText; if (!string.IsNullOrEmpty(currentValueText)) { currentValue = new MvcHtmlString("<b>" + currentValueText + "</b>"); } else { var status = allStatuses.SingleOrDefault(i => currentStatusIds == "_" + i.ID + "_"); currentValue = status != null ? MFCUI.Image(status.Title, "/MFC/Items/STAT/STAT16.png", showText: true, cssClassOfText: "bold", textColor: status.Color) : new MvcHtmlString("<b style=\"color: #169; \">请选择</b>"); } var ddl = MFCUI.DropdownListHtml(page, currentValue, linksListLeft, linksListRight1, null, "200px"); return new MvcHtmlString("状态:" + ddl); }有一个被调用的函数在这里:
private static List<MvcHtmlString> AddToList(WebViewPage page, string key, string currentStatusIds, Dictionary<string, IEnumerable<Status>> linkList) { var linksList = new List<MvcHtmlString>(); foreach (var link in linkList) { var statusIds = link.Value == null ? "" : link.Value.Aggregate<Status, string>(null, (current, status) => current + "_" + status.ID + "_"); linksList.Add(link.Value == null ? null : MFCUI.Link(link.Key, page.MergeParameter(key, statusIds), displayAsBoldText: statusIds == currentStatusIds)); } return linksList; }这样封装的代码没有复用价值,但可以减少维护时的阅读量。
subItems = statusIds == null ? ItemsUnder(repository, rootID, includeHidden: includeHidden) : ItemsUnder(repository, rootID, includeHidden: includeHidden).Where(i => i.Status != null && statusIds.Contains("_" + i.Status.ID + "_"));这是它唯一被真正使用的地方。尽管我们会对很多东西进行筛选,但都不需要写任何一行代码了,因为这行代码位于“很多东西”的底层。日后如果技术上有什么变动,就修改它就可以了。