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
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.
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?