可以/我应该使用模拟框架动态地向类添加事件吗?

时间:2021-06-17 06:53:19

Consider the following interface:

请考虑以下界面:

public interface IMyCallback
{
  void SomeEvent(int someArg);
}

which is the contract for a WCF callback that will be receiving "events" from a WCF service. My implementation for this interface looks like this

这是将从WCF服务接收“事件”的WCF回调的合同。我对此接口的实现如下所示

public class MyCallback : IMyCallback
{
  void IMyCallback.SomeEvent(int someArg)
  {
    OnSomeEvent(someArg);
  }

  protected virtual void OnSomeEvent(int someArg)
  {
    EventHandler<SomeEventArgs> handler = this.SomeEvent;

    if (handler != null)
    {
      handler(this, new SomeEventArgs(someArg));
    }
  }

  public event EventHandler<SomeEventArgs> SomeEvent;
}

which allows me to instantiate the callback and hook the SomeEvent event in my client code. Whenever the server calls my callback, I receive it a plain old .NET event. Everything works great.

这允许我实例化回调并在我的客户端代码中挂钩SomeEvent事件。每当服务器调用我的回调时,我都会收到一个普通的旧事件。一切都很好。

Here comes the question: I'd like to write a factory class to automate the creation of this callback so I easily re-use this approach in all my projects and with any interface. The factory would be called like this:

这里提出了一个问题:我想编写一个工厂类来自动创建这个回调,这样我就可以在所有项目和任何界面中轻松地重复使用这种方法。工厂会这样调用:

var myCallback = CallbackFactory.CreateCallback<IMyCallback>();

Can/should I use a mocking framework to dynamically create this class, or should I bite the bullet and emit IL directly? Or should I just hand code every implementation? Or is there another approach I'm not thinking of?

可以/我应该使用模拟框架来动态创建这个类,还是应该咬掉子弹并直接发出IL?或者我应该只为每个实现编写代码?还是有另一种我没想到的方法?

2 个解决方案

#1


You could use Castle Project's DynamicProxy which should give you all the infrastructure you need in order to generate a proxy. Then you don't have to worry about emitting IL which can have nasty side-effects if not done properly.

您可以使用Castle Project的DynamicProxy,它可以为您提供生成代理所需的所有基础结构。那么你不必担心发射IL,如果做得不好可能会产生令人讨厌的副作用。

I don't know how you want to attach to the event in user code when using the proxy, simply because your IMyCallback does not have the actual event in it only the one called. Or did I misunderstand your question?

我不知道你在使用代理时如何在用户代码中附加事件,只是因为你的IMyCallback中没有实际的事件只有被调用的事件。或者我误解了你的问题?

#2


I think that using a mocking framework is fine. In essence, a mocking/stub framework is nothing more than a way to easily provide implementations for type definitions for known inputs.

我认为使用模拟框架很好。从本质上讲,模拟/存根框架只不过是一种为已知输入轻松提供类型定义实现的方法。

Since that's exactly what you are doing here, I say go with that.

因为这正是你在这里所做的,所以我说顺其自然。

#1


You could use Castle Project's DynamicProxy which should give you all the infrastructure you need in order to generate a proxy. Then you don't have to worry about emitting IL which can have nasty side-effects if not done properly.

您可以使用Castle Project的DynamicProxy,它可以为您提供生成代理所需的所有基础结构。那么你不必担心发射IL,如果做得不好可能会产生令人讨厌的副作用。

I don't know how you want to attach to the event in user code when using the proxy, simply because your IMyCallback does not have the actual event in it only the one called. Or did I misunderstand your question?

我不知道你在使用代理时如何在用户代码中附加事件,只是因为你的IMyCallback中没有实际的事件只有被调用的事件。或者我误解了你的问题?

#2


I think that using a mocking framework is fine. In essence, a mocking/stub framework is nothing more than a way to easily provide implementations for type definitions for known inputs.

我认为使用模拟框架很好。从本质上讲,模拟/存根框架只不过是一种为已知输入轻松提供类型定义实现的方法。

Since that's exactly what you are doing here, I say go with that.

因为这正是你在这里所做的,所以我说顺其自然。