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

2 回答 2

1

依赖管理是一种避免大型代码库变得不可维护的做法。如果您尝试了解代码库的结构,我们可以说当代码库看起来像一盘意大利面条时,它是不可维护的!

依赖管理包括将代码工件(如类)分组,在块命名组件中,并检查组件之间的依赖关系是否保持可理解和健全(通过避免依赖循环等缺陷)。本文中的更多详细信息:控制组件依赖关系以获得干净的架构

于 2010-08-29T19:05:56.737 回答
0

当然,如果您正在使用诸如调用数据库之类的资源密集型操作,您可能可以证明为了性能而打破设计模式是合理的。也就是说,我不确定这总是一个不好的做法 - 我很确定 .NET 框架中的示例(我敢打赌)中的示例非常紧密耦合,尽管我怀疑你会看到很多都是公开的。但是,如果您确实破坏了设计模式,您可能想要记录发生了什么以及为什么,特别是如果其他人会查看或维护此代码,并尽可能多地内化。

于 2010-08-26T17:36:02.713 回答