我的需求
我希望我们的内部标准产品在事情发生时发动不同的事件。 在不同自定义解决方案的全局asax中,我想在需要时联系这些事件并做出反应。
现有模块
我一直在寻找asp.net的事件聚合器,但我不确定要使用什么。 我读过关于Prism的内容,但似乎这是针对WPF / Silverlight而不是asp.net。
然后就是这个人,似乎已将聚合器移植到他自己的版本中,独立于WPF:http://weblogs.asp.net/rashid/archive/2009/03/05/use-event-aggregator-to-make-your-application-more-extensible.aspx
问题
有没有人使用asp.net的事件聚合器?这是用于生产用途,所以我不想在网上使用一些随机人员的家庭编码聚合器:)
提前谢谢你。
编辑1: 看起来,NServiceBus有点矫枉过正。我创建了一个EventAggregator类来完成这个技巧。
班级:
/// <summary>
/// A event aggregator.
/// </summary>
public class EventAggregator
{
/// <summary>The object to use when locking.</summary>
private readonly object _lock = new object();
/// <summary>Holder of registered event handlers</summary>
private readonly Dictionary<Type, List<object>> _handlers = new Dictionary<Type, List<object>>();
/// <summary>Registers the specified handler.</summary>
/// <typeparam name="T"></typeparam>
/// <param name="handler">The handler.</param>
public void Register<T>(EventHandler<T> handler) where T : EventArgs
{
lock (_lock)
{
if (!_handlers.ContainsKey(typeof (T)))
_handlers.Add(typeof (T), new List<object>());
_handlers[typeof (T)].Add(handler);
}
}
/// <summary>Publishes the specified event.</summary>
/// <typeparam name="T"></typeparam>
/// <param name="sender">The sender.</param>
/// <param name="eventToPublish">The event to publish.</param>
public void Publish<T>(object sender, T eventToPublish) where T : EventArgs
{
lock (_lock)
{
if (!_handlers.ContainsKey(typeof (T)))
return; // No listers for event
foreach (EventHandler<T> handler in _handlers[typeof (T)])
handler.Invoke(sender, eventToPublish);
}
}
}
事件类:
public class EntityDeleted : EventArgs
{
}
在全局asax中注册事件处理程序:
aggregator.Register<EntityDeleted>((s, e) => {
// Do stuff here
});
举办活动:
aggregator.Publish(this, new EntityDeleted());
编辑2:
这是我对受害者的单元测试:
/// <summary>
/// Unit tests for EventAggregator
/// </summary>
[TestClass]
public class EventAggregatorTest
{
/// <summary>Tests that no exceptions are thrown when calling an event with no handlers.</summary>
[TestMethod]
public void EmptyAggregatorTest()
{
var aggregator = new EventAggregator();
aggregator.Publish(this, new TestEventOne() { Property = "p1" });
}
/// <summary>Tests the aggregator using a single, registered handler.</summary>
[TestMethod]
public void SingleListenerTest()
{
var aggregator = new EventAggregator();
int calls = 0;
aggregator.Register<TestEventOne>((sender, e) =>
{
Assert.AreEqual("p1", e.Property);
calls ++;
});
Assert.AreEqual(0, calls);
aggregator.Publish(this, new TestEventOne(){Property = "p1"});
Assert.AreEqual(1, calls);
}
/// <summary>Tests the aggregator using multiple registered handlers.</summary>
[TestMethod]
public void MultipleListenersTest()
{
var aggregator = new EventAggregator();
int p1Calls = 0;
int p2Calls = 0;
aggregator.Register<TestEventOne>((sender, e) =>
{
Assert.AreEqual("p1", e.Property);
p1Calls++;
});
aggregator.Register<TestEventOne>((sender, e) =>
{
Assert.AreEqual("p1", e.Property);
p1Calls++;
});
aggregator.Register<TestEventTwo>((sender, e) =>
{
Assert.AreEqual("p2", e.Property);
p2Calls++;
});
Assert.AreEqual(0, p1Calls);
aggregator.Publish(this, new TestEventOne() { Property = "p1" });
Assert.AreEqual(2, p1Calls);
Assert.AreEqual(0, p2Calls);
aggregator.Publish(this, new TestEventTwo() { Property = "p2" });
Assert.AreEqual(1, p2Calls);
Assert.AreEqual(2, p1Calls);
}
}
/// <summary>
/// Dummy test event 1
/// </summary>
public class TestEventOne : EventArgs
{
public string Property { get; set; }
}
/// <summary>
/// Dummy test event 2
/// </summary>
public class TestEventTwo : EventArgs
{
public string Property { get; set; }
}
编辑3:
感谢Steven Robbins指出,聚合器不是线程安全的,我在发布和注册方法中添加了锁定。
答案 0 :(得分:1)
我有类似的要求,为此,我使用NServiceBus开源,提供大型社区和优秀的文档,以获取更多信息,请尝试此链接
答案 1 :(得分:0)
如果你想在EA中放一个简单的(单个cs文件),比你家的滚动更多(上面的那个看起来不是线程安全的,不确定这是否是你的问题)你可以看看TinyMessenger。