【发布时间】:2014-04-02 13:40:16
【问题描述】:
我得到了一个接受如下集合的方法
public IList<CountryDto> ApplyDefaults(IList<CountryDto> dtos)
{
//Iterates the collection
//Validates the items in collection
//If items are invalid
//Removes items e.g dtos.Remove(currentCountryDto)
return dtos;//Do I need to do this?
}
我的问题是,对集合的引用没有改变,我应该从方法中再次返回集合吗?
- 对于:通过返回集合,我在签名中明确表示并且用户知道集合中的项目可能与原始来源不同。避免歧义。
- 反对:由于验证不会更改集合的引用,因此在技术上返回它没有意义。
在这种情况下,最好的方法是什么?
注意:我不确定这个问题是否基于意见。我想我可能在设计方面遗漏了一些东西。
【问题讨论】:
-
您是否关心通知用户是否应用了验证/默认值?
-
好点。否。此服务应用对用户透明的默认值。
-
您是否关心创建与 LINQ-to-Objects 兼容的 fluent-API?例如:
ApplyDefaults(myList).Where(dto => dto.Name == "Canada").Select(dto => dto.Currency).ToList()?编辑:或者您只是希望用户像“一劳永逸”一样就地过滤/验证列表:var myCountries = DtoService.GetCountries(); ApplyDefaults(myCountries); DoSomething(myCountries); -
ApplyDefaults 中发生的事情要复杂得多。我正在调用其他服务来执行注入类的验证。所以我不认为 fluent-api 是我的选择。