2010-03-25 60 views
0

請參閱以下方法。關於方法超載的建議

public static ProductsCollection GetDummyData(int? customerId, int? supplierId) 
    { 
     try 
     { 
      if (customerId != null && customerId > 0) 
      { 
       Filter.Add(Customres.CustomerId == customerId); 
      } 

      if (supplierId != null && supplierId > 0) 
      { 
       Filter.Add(Suppliers.SupplierId == supplierId); 
      } 

      ProductsCollection products = new ProductsCollection(); 

      products.FetchData(Filter); 

      return products; 
     } 
     catch 
     { 
      throw; 
     } 
    } 

    public static ProductsCollection GetDummyData(int? customerId) 
    { 
     return ProductsCollection GetDummyData(customerId, (int?)null); 
    } 

    public static ProductsCollection GetDummyData() 
    { 
     return ProductsCollection GetDummyData((int?)null); 
    } 

1-請教如何爲CustomerId和SupplierId重載,因爲只有一個重載可以用GetDummyData(int?)創建。我應該添加另一個參數來提及第一個參數是CustomerId或SupplierId,例如GetDummyData(int?,string)。或者我應該使用枚舉作爲第二個參數,並提到第一個參數是CustoemId或SupplierId。

2 - 這一條件是正確的或只是檢查> 0就足夠了 - >如果(!客戶ID = NULL & &客戶ID> 0)

3-使用try /像這樣的前提條件是正確的嗎?

4-傳遞(int?)null是正確的或任何其他更好的方法。

編輯:

我已經發現了一些其他職位像這樣因爲我沒有泛型的知識,這就是爲什麼我面對這個問題。我對嗎?以下是帖子。

Overloaded method calling overloaded method

回答

3
  1. 爲什麼不創建單獨GetCustomerData(int)GetSupplierData(int)方法呢? (隨着GetData()GetData(int,int)如果需要的話)。

  2. 如果你改變你的方法參數int然後而非int?你只需要檢查(由您自己決定),他們是大於0

  3. 在這種情況下,不需要try...catch。如果你正在做的是重新拋出異常,那麼不要着急抓住它。

  4. 請參閱上面的1和2。

編輯:也許是這樣的......

public static ProductsCollection GetData() 
{ 
    return GetDataImpl(-1, -1); 
} 

public static ProductsCollection GetData(int customerId, int supplierId) 
{ 
    if (customerId <= 0) 
     throw new ArgumentOutOfRangeException("customerId"); 

    if (supplierId <= 0) 
     throw new ArgumentOutOfRangeException("supplierId"); 

    return GetDataImpl(customerId, supplierId); 
} 

public static ProductsCollection GetCustomerData(int customerId) 
{ 
    if (customerId <= 0) 
     throw new ArgumentOutOfRangeException("customerId"); 

    return GetDataImpl(customerId, -1); 
} 

public static ProductsCollection GetSupplierData(int supplierId) 
{ 
    if (supplierId <= 0) 
     throw new ArgumentOutOfRangeException("supplierId"); 

    return GetDataImpl(-1, supplierId); 
} 

private static ProductsCollection GetDataImpl(int customerId, int supplierId) 
{ 
    if (customerId > 0) 
     Filter.Add(Customers.CustomerId == customerId); 

    if (supplierId > 0) 
     Filter.Add(Suppliers.SupplierId == supplierId); 

    ProductsCollection products = new ProductsCollection(); 
    products.FetchData(Filter); 

    return products; 
} 
+0

感謝您的建議,請您評論我的編輯? – Kashif 2010-03-25 11:58:11

+0

我不認爲在這種情況下泛型或您編輯中提到的其他問題是相關的。 – LukeH 2010-03-25 12:08:28

+0

感謝您的時間和意見。如果這不打擾你可以請告訴我爲什麼我必須創建單獨的方法。爲什麼我不應該重載這個方法。如果我必須創建單獨的方法來編寫大致相同的代碼,那麼方法重載的目的是什麼。 – Kashif 2010-03-25 12:35:08

1

你是不是做與嘗試捕捉任何東西,那麼爲什麼它列入。無論如何,你的異常會觸發調用方法,所以只需刪除無關的try catch代碼即可。

我會拋棄第二種和第三種方法,因爲他們沒有真正做任何事情。除非你有一些理由讓他們保持身邊,比如他們是遺留代碼,否則我會刪除它們。它只會使代碼對於未來的支持變得更加複雜。

+0

我不需要參數,獲取數據,而無需過濾器,或我要取的客戶ID或供應商ID或兩個數據,這就是爲什麼我我正在創建Overloads。我不需要這些嗎? – Kashif 2010-03-25 11:54:47