- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要帮助使这个方法通用。重复大约十次以获得不同 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;
}
如果这还不够,请告诉我。我认为这需要我刚刚熟悉的更高级的语言功能。也许我应该让它们都使用同一个存储库?
感谢您的帮助。
编辑:感谢您的帮助。我没有任何同行的支持,所以这个董事会很棒,我从你们每个人那里学到了一些东西。我希望我能接受所有的答案。
最佳答案
你可以先像这样让你的函数更简洁一点:
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();
}
此时,该函数的大部分内容都与MyType
直接相关,因此如何进一步改进它很大程度上取决于MyType
与其他函数的关系涉及的类型。例如,这是一个假设版本,如果您的其他类型遵循(对我来说)看起来合理的契约(Contract),您可以编写:
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();
}
关于c# - 干这个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/920742/
我的主函数中有这两个函数。正如您将看到的,它们两者之间的唯一区别在于它们附加/编辑 html 的方式。我认为最好提出两个新函数,一个执行前半部分,另一个执行后半部分。我不确定这是否可以用 jQuery
我是编码新手,但一直在阅读有关 DRY - 不要重复自己的内容。 我有一个不适合 DRY 方法的 JavaScript if/else 语句,但我无法练习如何编写 JavaScript if/else
我的情况很简单: $("#check-in").dateDropper({ years_multiple: "10", format: "d-m-Y", minYear: "2
我想要一个方法在很多地方创建以下 json 字符串: {"daily_calendar":{"search":{"print_date":"2014-06-30"}}} 我有以下四行代码来制作散列:
我有这个正则表达式来检查字符串的格式是否为date, two or three dots ,date /\A(\d{1,2}-\d{1,2}-\d{4})...?(\d{1,2}-\d{1,2}-\d
我正在试验一些代码来创建我自己的 DOM 方法,但没有扩展实际的 DOM,类似于 jQuery 的工作方式。到目前为止,这是我的原型(prototype): function Lib( selecto
我是 Java 编程的新手。这里有人可以帮我让我的代码保持干燥吗? Button level01 = (Button) findViewById(R.id.level01); level01.setO
我正在通过一个数组来根据外部设置的状态应用不同的类。这就是我现在的做法,但我觉得我在重复自己很多次。有干燥机的方法吗?如果有帮助,类名可以是其他名称。 var children2 = Array.fr
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 4 年前。 Improve t
我正在尝试构建一个搜索页面,使用户能够找到满足特定阈值标准的模型的任何实例,并且在避免严重冗余代码方面遇到困难。我希望有更好的方法来做到这一点。这是一个稍微做作的示例,应该说明我正在尝试做什么,并在最
我是一名优秀的程序员,十分优秀!