简体   繁体   English

最佳实践:事件的动作枚举用法

[英]Best-Practices: action enum usage for events

I find my self hard to design action enums for events. 我发现自己很难为事件设计动作枚举。

f.ex making calculation processor. f.ex制造计算处理器。

so I should have enum like this ? 所以我应该有这样的枚举? :

public enum CalculatorCoreActions
{
    ProcessStarted,
    ProcessFinished,
    ProcessFailure,
    ProcessFailed,
    SubstractionStarded,
    SubstractionFinished,
    SubstractionFailure,
    SubstractionFailed,
    <etc.>...
}

Or I should make two enums ?, f.ex: 或者我应该做两个枚举?,f.ex:

public enum CalculatorAction
{
    Process,
    Substraction,
    Division
}

public enum CalculationActionResult
{
   Started,
   Finished,
   Failure,
   Failed
}

Or even I should create an new classes ? 甚至我应该创建一个新的类? :

public class CalculatorActionEventArgs : EventArgs {<...>}
public class CalculatorActionFailedEventArgs : EventArgs {<...>}
public class etc... : EventArgs {<...>}

Which method is the best in your opinion? 您认为哪种方法最好?

What are you trying to do? 你想做什么? What are you going to use the enums for? 您打算将枚举用于什么? Your first example may be OK for very few combinations, whereas the second will be more complex but much cleaner and easier to extend. 您的第一个示例对于很少的组合可能是可以的,而第二个示例将更复杂,但更干净,更易于扩展。

My guess is that you want to be able to display the state of calculation, and perhaps act on the state in eg error handling. 我的猜测是,您希望能够显示计算状态,并可能在例如错误处理中对状态进行操作。 You might consider the State or State Machine patterns rather than enums, in order to avoid huge switch statements on your enums. 您可能会考虑使用StateState Machine模式而不是枚举,以避免枚举上出现大量switch语句。

Of the two choices for enumerations that you presented I would go with the second set of enumerations. 在您提出的两个枚举选择中,我将使用第二组枚举。

Why, well when you cross (literaly multiply) the states with the actions you come up with a large growth pattern. 为什么呢,当您将状态与行动进行交叉(字面上乘)时,您会想到一个大的增长模式。 When you want to add either a state or a feature in the future you no longer add one item but 1xM or Nx1 items. 如果您将来想添加状态或功能,则不再添加一项,而是添加1xM或Nx1项。 For other references to this style of problem see Martin Fowler's book Refactoring and object hierachy entanglement. 有关这种问题样式的其他参考,请参阅Martin Fowler的书《 重构和对象层次纠缠》。

Now for the event args, use the generic EventArgs and give it the action and state in an object. 现在,对于事件args,使用通用EventArgs并将其操作和状态赋予对象。 Don't create more things than you have to and it will minimize the number of headaches you create. 不要创造比您需要更多的东西,它可以最大程度地减少您制造头痛的次数。

it depends how you are designing your structure... to me events should be different but one should try not to use more EventArgs eg like you are using one for failed one for Action etc.... instead try to go for bare minimum amount of event arguments as its only your data passing mechanism... 这取决于您如何设计结构...对我来说,事件应该有所不同,但应该尽量不要使用更多的EventArgs,例如,像对失败的事件使用一个那样的ActionArgs等。事件参数是其唯一的数据传递机制...

now regarding enumeration part i am not exactly able to pick up your scenario.... why you want so much different enumerations? 现在关于枚举部分,我还无法完全掌握您的情况...。为什么要这么多的枚举?

if you meant to use it as replacement for multiple events than... if i were you than i will go for events... but if your structure requires a huge amount of events say 10 or more than go for a mix blend of events with enum as minimum event args... say in case of 10 reduce events to 3 with 4 or 5 enums 如果您打算用它代替多个事件...如果我是您,那么我将去参加事件...但是如果您的结构需要大量事件,则说10个或更多事件来代替混合事件用枚举作为最小事件参数...说在10个事件的情况下将事件减少为3个,具有4个或5个枚举

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM