我需要帮助使这个方法通用.重复大约十次以获取不同Web列表控件的列表(将"MyType"替换为特定控件中使用的类型).
private static IList<MyType> GetList(RequestForm form)
{
// get base list
IMyTypeRepository myTypeRepository = new MyTypeRepository(new HybridSessionBuilder());
IList<MyType> myTypes = myTypeRepository.GetAll();
// create results list
IList<MyType> result = new List<MyType>();
// iterate for active + used list items
foreach (MyType myType in myTypes)
{
if (myType.Active || form.SolutionType.Contains(myType.Value))
{
result.Add(myType);
}
}
// return sorted results
result.OrderBy(o => o.DisplayOrder);
return result;
}
Run Code Online (Sandbox Code Playgroud)
如果这还不够,请告诉我.我认为这需要更多高级语言功能,我才刚刚熟悉.也许我应该让他们都使用相同的存储库?
谢谢你的帮助.
编辑:谢谢你的帮助.我没有任何同伴支持,所以这个板很棒,我从你们每个人那里学到了一些东西.我希望我能接受所有答案.
你可以先让你的函数更像这样简洁:
private static IList<MyType> GetList(RequestForm form)
{
// get base list
IMyTypeRepository myTypeRepository =
new MyTypeRepository(new HybridSessionBuilder());
IList<MyType> myTypes = myTypeRepository.GetAll();
return myTypes.Where(x => x.Active || form.SolutionType.Contains(x.Value))
.OrderBy(x => x.DisplayOrder).ToList();
}
Run Code Online (Sandbox Code Playgroud)
此时,函数的大部分内容都直接相关MyType,因此如何进一步改进它主要取决于MyType与其他类型相关的内容.例如,这是一个假设版本,如果您的其他类型遵循合理的(对我而言)合同,您可以编写:
private static IList<T> GetList(RequestForm form) where T : OrderedValueContainer
{
// we'll want to somehow genericize the idea of a TypeRepository that can
// produce these types; if that can't be done, we're probably better off
// passing a repository into this function rather than creating it here
var repository = new TypeRepository<T>(new HybridSessionBuilder());
IList<T> myTypes = repository.GetAll();
// the hypothetical OrderedValueContainer class/interface
// contains definitions for Active, Value, and DisplayOrder
return myTypes.Where(x => x.Active || form.SolutionType.Contains(x.Value))
.OrderBy(x => x.DisplayOrder).ToList();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |