2010-08-26 129 views
1

我不確定我真的理解依賴管理。這是否意味着你只依賴於另一個班級的細節?這並不意味着與通話本身有任何關係?我一直聽到做更小的更具體的課,但他們不能相互依賴,這對我來說似乎不可能。有人可以試着簡單地向我解釋這一點。我在下面列舉了一些例子。依賴管理問題

//Bad Dependency 
public class TestOne 
{ 
    TestTwo testTwo; 
    public void TestOneMethod() 
    { 
      testTwo = new TestTwo(); 
      testTwo.SomeProperty = "Value"; 
      testTwo.SomeMethodThatWorksWithSomeProperty(); 
    } 
}
//Bad dependency?? 
public class TestOne 
{ 
    TestTwo testTwo; 
    public void TestOneMethod() 
    { 
      int myInt = 0; 
      TestThree testThree = new TestThree(); 
      //... Some Code that works with testThree 

      testTwo = new TestTwo(); 
      myInt = testTwo.GetSomeInteger(testThree); 
    } 
}

只能有設置每次運行設置,所以我爲什麼要不停的按數據庫每次一個新類叫?這是一個壞的依賴


public static class Application 
{ 
    public static int SomeSetting = 0; 
    public static GetSettingsFromDatabase() 
    { 
     //loads the settings for this store 
     DatabaseClass dbClass = DatabaseClassDataSource.LoadForStore(); 
     SomeSetting = dbClass.SomeSetting; 
    } 
} 

public class MyClass 
{ 
    public void MethodOne() 
    { 
     if(Application.SomeSetting == 1) { //... } 
    } 
} 

public class MyClassTwo 
{ 
    public void MethodOne() 
    { 
     if(Application.SomeSetting == 1) { //... } 
    } 
} 

回答

0

當然,如果你使用像調用數據庫資源密集型操作,你也許可以證明打破了設計模式對性能的緣故。也就是說,我不確定這總是一個糟糕的做法 - 我敢肯定在.NET框架中有些例子(我敢打賭)有很好的耦合,但我懷疑你會看到很多暴露給公衆的東西。但是,如果您確實打破了設計模式,那麼您可能需要記錄正在發生的事情以及原因,特別是如果其他人會查看或維護此代碼並儘可能多地進行內部化。

1

依賴管理是一種避免大型代碼庫變得無法維護的做法。我們可以說,如果你試圖瞭解它是如何構造的,那麼當它看起來像一盤意大利麪時,代碼庫是不可維護的!

依賴管理在於分組代碼工件等的類,在塊命名的組件,並檢查組件之間的依賴性保持可以理解的,神志正常(通過避免像依賴性的週期缺陷)這篇文章中。更多細節:Control component dependencies to gain clean architecture