asp.net 的事件聚合器

Ste*_*yer 6 c# asp.net events aggregator

我的需求

我希望我们的内部标准产品在事情发生时触发不同的事件。在不同自定义解决方案的 global asax 中,我想在需要时连接这些事件并做出反应。

现有模块

我一直在寻找用于 asp.net 的事件聚合器,但我不确定要使用什么。我读过 Prism,但它似乎针对 WPF/Silverlight 而不是 asp.net。

然后是这个人,他似乎将聚合器移植到他自己的版本中,独立于 WPF:http : //weblogs.asp.net/rashid/archive/2009/03/05/use-event-aggregator-to-使您的应用程序更可扩展.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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

一个事件类:

public class EntityDeleted : EventArgs
{
}
Run Code Online (Sandbox Code Playgroud)

在全局 asax 中注册事件处理程序:

aggregator.Register<EntityDeleted>((s, e) => {
// Do stuff here
});
Run Code Online (Sandbox Code Playgroud)

引发事件:

aggregator.Publish(this, new EntityDeleted());
Run Code Online (Sandbox Code Playgroud)

编辑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; }
}
Run Code Online (Sandbox Code Playgroud)

编辑 3:

感谢 Steven Robbins 指出聚合器不是线程安全的,我向 Publish 和 Register 方法添加了锁定。

Bob*_*bby 2

我有类似的要求,为此我使用 NServiceBus 其开源的大型社区和出色的文档来获取更多信息,请尝试此链接

http://docs.pspecial.net/