如果DLayer
仅在GetBLObject
方法中使用,我将在方法调用中注入工厂。类似于:(以@PaulPhillips 示例为基础)
public GetBLObject(string params, IDataLayerFactory dataLayerFactory)
{
using(DLayer dl = dataLayerFactory.Create()) // replace the "new"
{
//BL logic here....
}
}
但是,您真正想要在业务层中使用的似乎是DataSet
. 所以另一种方法是让in 方法调用而GetBLObject
不是. 为了完成这项工作,您可以创建一个类来处理从. 例如:DataSet
string param
DataSet
DLayer
public class CallingBusinesslayerCode
{
public void CallingBusinessLayer()
{
// It doesn't show from your code what is returned
// so here I assume that it is void.
new BLLayer().GetBLObject(new BreakingDLayerDependency().GetData("param"));
}
}
public class BreakingDLayerDependency
{
public DataSet GetData(string param)
{
using (DLayer dl = new DLayer()) //you can of course still do ctor injection here in stead of the new DLayer()
{
return dl.GetData(param);
}
}
}
public class BLLayer
{
public void GetBLObject(DataSet ds)
{
// Business Logic using ds here.
}
}
一个警告:模拟DataSet
(在此解决方案和 Paul Phillips 解决方案中都必须这样做)可能非常麻烦,因此对此进行测试是可能的,但不一定很有趣。