2013-11-23 34 views
0

我在設計一個工廠類,並想知道基於不同參數檢索對象集合的可接受方法命名約定是什麼。通過多個參數命名GetX的方法

實施例:

獲得從我的數據源中的所有對象(#1無參數):

public GetAll(){...} 

獲取的所有對象相匹配的名稱(#2名參數):

public GetByName(string name){..} 

獲得與'其他屬性'(#3'另一個屬性'參數)匹配的所有對象:

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.){...} 

有沒有更好的方法來做到這一點?如何設計一個靈活的類來接受任意數量的參數,並保持命名約定簡潔明瞭?

任何幫助將是巨大的!

回答

1

也許我誤解了這個問題,但在我看來,你正在尋找forthis:

http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.110).aspx

public FooClass GetByProperties(params string[] list) 
+0

謝謝。但是,我將如何區分列表中的項目是屬性A還是屬性B? –

+0

你不能。如果您必須區分它們,請創建一個代表方法輸入參數的類。如果您有超過5個輸入參數,也推薦使用這種方法。所以,它看起來像這樣:public FooClass GetByProperties(ParamClass parameters); –

+0

我決定使用可選/命名參數,以儘量減少對方法重載的需求。 [使用的技術。](http://msdn.microsoft.com/zh-cn/vstudio/ff467291.aspx)感謝您的幫助。 –

相關問題