我继承了这段代码,原作者出于某种原因真的很喜欢静态只读引用。
我在下面概述了一般模式,这些静态只读引用有什么影响?
public class DbAccess
{
private static readonly ISomethingFactory = SomethingFactories.GetFactory("1");
private static IThing1Dao Thing1Dao { get {return ISomethingFactory.Thing1Dao; }}
private static IThing2Dao Thing2Dao { get {return ISomethingFactory.Thing2Dao; }}
}
public class SomethingFactories
{
public static ISomethingFactory GetFactory(string key)
{
switch(key)
{
case "...":
return new SomeFactory();
}
}
}
public class SomeFactory : ISomeFactory
{
public IThing1Dao Thing1Dao
{
get { return new Thing1Dao(); }
}
}
public class SomeService : ISomeService
{
private static readonly IThing1Dao thing1Dao = DbAccess.Thing1Dao
private static readonly IThing2Dao thing2Dao = DbAccess.Thing2Dao
public Thing1Object1 GetObject1(int id)
{
return thing1Dao.GetObject1(id);
}
public Thing1Object2 GetObject2(int id)
{
return thing1Dao.GetObject2(id);
}
}
.aspx 页面中的用法如下:
public class MyBasePage : System.Web.UI.Page
{
protected static readonly SomeService someService = new SomeService();
}
public class SomeAspxPage : MyBasePage
{
void btnAddObject1(...)
{
Thing1Object1 obj1 = someService.GetObject(1);
}
}