一个简单的.NET事件总线
我想制作一个非常简单的事件总线,它允许任何客户机订阅特定类型的事件,当任何发布者使用一个简单的.NET事件总线,.net,generics,events,c#-2.0,bus,.net,Generics,Events,C# 2.0,Bus,我想制作一个非常简单的事件总线,它允许任何客户机订阅特定类型的事件,当任何发布者使用EventBus.PushEvent()方法在总线上推送事件时,只有订阅该特定事件类型的客户机才能获得事件 我正在使用C#和.NET 2.0。你应该看看Ayende的屏幕播放系列中的第3集-“实现事件代理” 它展示了如何使用Windsor来实现一个非常简单的事件代理。源代码也包括在内 建议的事件代理解决方案非常简单,但它不会花费太多的时间来扩充解决方案,以允许参数随事件一起传递。包括一个可能对您有用的事件代理。我
EventBus.PushEvent()
方法在总线上推送事件时,只有订阅该特定事件类型的客户机才能获得事件
我正在使用C#和.NET 2.0。你应该看看Ayende的屏幕播放系列中的第3集-“实现事件代理” 它展示了如何使用Windsor来实现一个非常简单的事件代理。源代码也包括在内
建议的事件代理解决方案非常简单,但它不会花费太多的时间来扩充解决方案,以允许参数随事件一起传递。包括一个可能对您有用的事件代理。我发现。这是一个简单的类。您还可以查看Unity扩展:
有更好的吗?另一个好的实现可以在以下位置找到: 可通过以下网址访问用例: /trunk/Squared/Util/UtilTests/Tests/EventTests.cs 此实现不需要外部库
一个改进可能是能够使用类型而不是字符串进行订阅。Tiny Messenger是一个不错的选择,我已经在一个实时项目中使用它2.5年了。Wiki中的一些代码示例(以下链接): 出版
messageHub.Publish(new MyMessage());
messageHub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); });
messageHub.Subscribe<MyMessageAgain>((m) => { MessageBox.Show("Message Received!"); }, (m) => m.Content == "Testing");
订阅
messageHub.Publish(new MyMessage());
messageHub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); });
messageHub.Subscribe<MyMessageAgain>((m) => { MessageBox.Show("Message Received!"); }, (m) => m.Content == "Testing");
我创造了这个:
与ninject之间存在依赖关系。
你有一个信息处理器。如果您想观察事件,如果您想处理命令,请实现“IObserver”实现“ICommandHandleer”另一个,灵感来自android的EventBus,但要简单得多:
public class EventBus
{
public static EventBus Instance { get { return instance ?? (instance = new EventBus()); } }
public void Register(object listener)
{
if (!listeners.Any(l => l.Listener == listener))
listeners.Add(new EventListenerWrapper(listener));
}
public void Unregister(object listener)
{
listeners.RemoveAll(l => l.Listener == listener);
}
public void PostEvent(object e)
{
listeners.Where(l => l.EventType == e.GetType()).ToList().ForEach(l => l.PostEvent(e));
}
private static EventBus instance;
private EventBus() { }
private List<EventListenerWrapper> listeners = new List<EventListenerWrapper>();
private class EventListenerWrapper
{
public object Listener { get; private set; }
public Type EventType { get; private set; }
private MethodBase method;
public EventListenerWrapper(object listener)
{
Listener = listener;
Type type = listener.GetType();
method = type.GetMethod("OnEvent");
if (method == null)
throw new ArgumentException("Class " + type.Name + " does not containt method OnEvent");
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length != 1)
throw new ArgumentException("Method OnEvent of class " + type.Name + " have invalid number of parameters (should be one)");
EventType = parameters[0].ParameterType;
}
public void PostEvent(object e)
{
method.Invoke(Listener, new[] { e });
}
}
}
+1对于TinyMessenger,它是一种微型消息总线,可以基于redis创建消息队列
public class OnProgressChangedEvent
{
public int Progress { get; private set; }
public OnProgressChangedEvent(int progress)
{
Progress = progress;
}
}
public class SomeForm : Form
{
// ...
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EventBus.Instance.Register(this);
}
public void OnEvent(OnProgressChangedEvent e)
{
progressBar.Value = e.Progress;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
EventBus.Instance.Unregister(this);
}
}
public class SomeWorkerSomewhere
{
void OnDoWork()
{
// ...
EventBus.Instance.PostEvent(new OnProgressChangedEvent(progress));
// ...
}
}