0
我正在閱讀一本關於域驅動設計的書。有一個關於域名事件的章節。作者指出,如果在ASP.Net應用程序中使用具有[ThreadStatic]屬性的回調集合並引用此博客文章,則有理由關注:http://www.hanselman.com/blog/ATaleOfTwoTechniquesTheThreadStaticAttributeAndSystemWebHttpContextCurrentItems.aspxASP.Net中帶有ThreadStatic屬性的域事件回調
我閱讀了關於此博客的解釋。但我不明白究竟是什麼問題。 有沒有人使用這種域名事件的方法,並可以分享他的經驗?
這裏是一個域事件類的代碼(這是烏迪大漢):
public static class DomainEvents
{
[ThreadStatic]
private static List<Delegate> _actions;
private static List<Delegate> Actions
{
get
{
if (_actions == null)
{
_actions = new List<Delegate>();
}
return _actions;
}
}
public static IDisposable Register<T>(Action<T> callback)
{
Actions.Add(callback);
return new DomainEventRegistrationRemover(() => Actions.Remove(callback));
}
public static void Raise<T>(T eventArgs)
{
foreach (Delegate action in Actions)
{
Action<T> typedAction = action as Action<T>;
if (typedAction != null)
{
typedAction(eventArgs);
}
}
}
private sealed class DomainEventRegistrationRemover : IDisposable
{
private readonly Action _callOnDispose;
public DomainEventRegistrationRemover(Action toCall)
{
_callOnDispose = toCall;
}
public void Dispose()
{
_callOnDispose();
}
}
}