使用事件而不是异常来实现错误处理

Jer*_*Gee 10 c# events design-patterns exception

我正在研究一些在其业务和数据层中使用模式的代码,这些代码使用事件来发出错误信号,例如

resource = AllocateLotsOfMemory();
if (SomeCondition())
{    
    OnOddError(new OddErrorEventArgs(resource.StatusProperty));
    resource.FreeLotsOfMemory();    
    return;
}
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪,特别是当调用它的代码需要挂钩事件时(有四个或五个不同的!).

开发人员告诉我,这样他们就可以在错误处理代码中引用已分配资源的属性,并且在此层保留错误之后清理的责任.

这有点儿意义.

替代方案可能是这样的

resource = AllocateLotsOfMemory();
if (SomeCondition())
{   
    BigObject temporary = resource.StatusProperty;
    resource.FreeLotsOfMemory();
    throw new OddException(temporary);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. BigObject释放异常对象时释放此" ",我们是否需要这种模式?

  2. 有没有其他人体验过这种模式?如果是这样,你发现了什么陷阱?有什么优势?

谢谢!

Jon*_*eet 8

这对我来说似乎很奇怪.有一些优点 - 例如允许多个"处理程序",但语义与正常的错误处理明显不同.特别是,它不会自动传播到栈中这一事实让我感到担忧 - 除非错误处理程序本身抛出异常,否则逻辑将继续进行,就好像它应该可以中止当前操作一样.

另一种思考方式:假设该方法是为了返回一个值,但是你早就发现了一个错误.你有什么价值?例外沟通的事实,那就是没有合适的值返回...


use*_*375 3

看看Udi Dahan 的这篇文章。这是一种调度域事件的优雅方法。上一张海报正确地指出,您不应该使用事件机制来从致命错误中恢复,但它对于松散耦合系统中的通知来说是一种非常有用的模式:

public class DomainEventStorage<ActionType>
{
    public List<ActionType> Actions
    {
        get
        {
            var k = string.Format("Domain.Event.DomainEvent.{0}.{1}",
                                  GetType().Name,
                                  GetType().GetGenericArguments()[0]);
            if (Local.Data[k] == null)
                Local.Data[k] = new List<ActionType>();

            return (List<ActionType>) Local.Data[k];
        }
    }

    public IDisposable Register(ActionType callback)
    {
        Actions.Add(callback);
        return new DomainEventRegistrationRemover(() => Actions.Remove(callback)
            );
    }
}

public class DomainEvent<T1> : IDomainEvent where T1 : class
{
    private readonly DomainEventStorage<Action<T1>> _impl = new DomainEventStorage<Action<T1>>();

    internal List<Action<T1>> Actions { get { return _impl.Actions; } }

    public IDisposable Register(Action<T1> callback)
    {
        return _impl.Register(callback);
    }

    public void Raise(T1 args)
    {
        foreach (var action in Actions)
        {
            action.Invoke(args);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并消费:

var fail = false;
using(var ev = DomainErrors.SomethingHappened.Register(c => fail = true) 
{
   //Do something with your domain here
}
Run Code Online (Sandbox Code Playgroud)