假設我們有一個工廠用於返回包含選擇特定邏輯的邏輯部分。我想一些人委派負責一個工廠,然後寫控制器內整齊碼:在工廠內部創建局部視圖
[HttpGet]
public PartialViewResult GetQueryItemForCategory(string categoryName, bool campaignSelected)
{
return QueryItemBuilderFactory.BuildPartial(categoryName, campaignSelected);
}
但我真的不能調用工廠內PartialView()方法。
public static class QueryItemBuilderFactory
{
private static Dictionary<string, Func<bool, PartialViewResult>> _builderActions =
new Dictionary<string, Func<bool, PartialViewResult>>();
static QueryItemBuilderFactory()
{
_builderActions.Add("Data Field", campaignSelected =>
{
return PartialView("_DataFieldQueryItemPartial");
});
}
public static PartialViewResult BuildPartial(string categoryName, bool campaignSelected)
{
return _builderActions[categoryName](campaignSelected);
}
}
有什麼辦法可以實現它嗎?
最後一行真的有把戲 –