在事件聚合器中将枚举与事件类混合 [英] Mixing enums with event classes in an event aggregator

查看:114
本文介绍了在事件聚合器中将枚举与事件类混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试实现自己的简单事件聚合器.我从MSDN上的事件聚合器文章中获得了很多启发.关于MSDN上的事件聚合器,我注意到一件事,即事件实际上是它们自己的类.这根本不是一件坏事.但是,我发现总是为每个小事件创建一个新的空类很尴尬.

I attempted to implement my own simple event aggregator recently. I got a lot of inspiration from the event aggregator article on MSDN. There is one thing that I notice about the event aggregator on MSDN is the events are actually classes on their own. This isn't really a bad thing at all. However, I just find it awkward to always create a new empty class for every single little event.

我觉得很尴尬的原因是因为需要为每个颗粒事件创建一个类.例如,鼠标单击事件将具有double_click,single_click,left_click,right_click等.所有这些都将具有其自己的类.过了一会儿,它变得凌乱.

The reason I find it awkward is because of the need to create a class for every single granular event. A mouse click event, for instance, would have double_click, single_click, left_click, right_click, etc. And all of these are going to have a class of its own. It gets messy after a while.

因此,在我自己的实现中,我认为可以以ClickEvent为类的方式来实现,但是所有与Click事件相关的粒度事件都将成为ClickEvent的类型".在这种情况下,类型"为enum.用法看起来像这样:

So in my own implementation, I thought I could make it in such a way where the ClickEvent is a class, but all the granular events related to the Click event would then be "types" of the ClickEvent. In this case, the "types" are enum. The usage would look something like this:

//Publisher
eventAggregator.GetEvent<ClickEvent>.Publish(ClickEventType.Double_Click, eventArgs);

//Subscriber
eventAggregator.GetEvent<ClickEvent>.Subscribe(ClickEventType.Double_Click, handlerMethod);

但是,我不确定此实现是否会破坏具有强类型事件的整个目的?现在,ClickEvent似乎只是不同事件枚举类型的容器.

However, I'm not sure if this implementation defeats the whole purpose of having a strongly typed event? Now, it seems like the ClickEvent is merely a container for the different event enum types.

推荐答案

是的(好像是一个容器)-无论单击类型如何,您的处理程序都会触发,并且在处理程序中需要一些代码来确定点击的类型,这会使事情变得更加混乱.

Yes it does (seem like a container that is) - your handler will fire regardless of the click type and there will be some code required in the handler to determine the type of click, which makes things a little messier.

如果您的问题主要是文件/类的组织并保持代码整洁,为什么不直接将click事件创建为嵌套在主click类中的类

If your issue is mostly the organisation of the files/classes and keeping the code tidy, why not just create the click events as nested classes within a main click class

例如

public static class ClickEvents // Prevent instantiation
{
    public class SingleLeft { }
    public class SingleRight { }
    public class DoubleLeft { }
    public class DoubleRight { }
    // Are there any more click events possible?!
}

eventAggregator.GetEvent<ClickEvents.SingleLeft>.Publish();

除此之外,决定事件签名的是类型的唯一性,因此需要多种类型来满足此特定实现

Aside from that, it's the uniqueness of the type that determines the event signature, and therefore multiple types are required to satisfy this particular implementation

至少以上内容使您的处理程序代码保持清洁

At least the above keeps your handler code clean

void HandleLeftClick()
{
}

vs

void HandleClick(ClickArgs e) 
{
    if(e.ClickType == ClickType.Left)
    {
    }
}

还请记住,如果要处理多个单击类型,可以将多个事件订阅到同一个处理程序:

Also remember that you can subscribe multiple events to the same handler if you want to handle more than one click type:

eventAggregator.GetEvent<ClickEvents.SingleLeft>.Subscribe(HandlerMethod);
eventAggregator.GetEvent<ClickEvents.SingleRight>.Subscribe(HandlerMethod);

(在极少数情况下,订户不在乎单击哪个鼠标按钮的情况下,这是可行的)

(this would work in the rare situation that the subscriber didn't care which mouse button was clicked)

这篇关于在事件聚合器中将枚举与事件类混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆