【发布时间】:2010-11-22 09:26:23
【问题描述】:
我有一个使用另一个服务 (Service2) 的服务 (Service1)。我正在为这两个服务使用依赖注入,并且需要将 Service2 的代理注入到 Service1 中。
我不确定如何处理代理不是 IService2 类型的简单类而是继承自 ClientBase 的代理这一事实。显然,我的 Service1 实现需要打开代理,并且还应该在使用后关闭它,或者如果发生异常则中止它,但是如果我只是注入 IService2 的一个实例,那么我不能这样做(没有强制转换),因为 Open、Close 和 Abort 方法在基类上,而我的操作在接口上。
在测试 Service1 时,我希望只模拟接口,但如果 Service1 实现需要 Open、Close 和 Abort 方法,那么这很棘手。过去,我做过类似这样的骇人听闻的事情,但一定有更好的方法!
var proxyBase = _service2 as ClientBase;
if (proxyBase != null)
{
proxyBase.Open();
}
_service2.DoOperation("blah"); //the actual operation
if (proxyBase != null)
{
proxyBase.Close();
}
// repeat for Abort in exception handler(s).
其他人在做什么?
谢谢
【问题讨论】:
标签: wcf dependency-injection proxy mocking ioc-container