【发布时间】:2013-11-23 00:52:57
【问题描述】:
我正在设计一个工厂类,并且想知道在根据不同参数检索对象集合时,可接受的方法命名约定是什么。
例子:
从我的数据源获取所有对象(#1 无参数):
public GetAll(){...}
获取所有具有匹配名称的对象(#2 名称参数):
public GetByName(string name){..}
获取所有匹配'another property'的对象(#3 'another property'参数):
public GetByAnotherProperty(string anotherProperty){...}
现在,我遇到麻烦的地方是当我想添加一个获取两个或多个属性的 get 方法时:
获取所有具有匹配名称和“另一个属性”的对象(#4 两个参数):
我的一些尝试:
public GetByNameAndAnotherProperty(string name, string anotherProperty){...}
public GetByNameByAnotherProperty(string name, string anotherProperty){...}
public Get(string name, string anotherProperty){...}
一旦我有两个或三个以上的参数,我可以看到前两种方法变得非常麻烦。示例:
public GetByNameByPropertyXByPropertyYByPropertyZ(string name, etc.){...}
有没有更好的方法来做到这一点?如何设计一个灵活的类来接受任意数量的参数并保持命名约定简洁明了?
任何帮助都会很棒!
【问题讨论】:
标签: c# methods overloading