我不确定我是否真的了解依赖管理。这是否意味着你不依赖于另一个类的细节?这并不意味着与调用本身有关吗?我一直听到要制作更小更具体的课程,但他们不能相互依赖,这对我来说似乎是不可能的。有人可以尝试简单地向我解释这一点。我在下面举了一些例子。
//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) { //... }
}
}