winform 的 AOP
如何在winform应用程序中捕获用户点击而不使代码变得非常复杂? AOP 是答案吗? 如何跟踪仪器?
How to capture user clicks in a winform application without making the code very complicated? is AOP the answer? How to track instrumentations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实取决于您的实际要求。
对于简单/样本/原型 winforms 应用程序,我建议基本 WinForms 事件处理程序,在处理程序方法中包含代码(只要它不是太重)。 如果代码很重,包含严格的业务规则并且不与表单交互,那么最好将该代码移动到另一个类并从事件处理程序调用它。
面向方面编程更进一步。 它通常最适合大型项目。 您仍然需要表单元素的事件处理程序(我建议坚持使用基于控件的事件处理程序 - 不要尝试创建自己的全局事件处理工具),但它们应该调用 Command 对象(请参阅 GangOfFour 中的命令模式)。 然后可以从应用程序中的任何位置调用这些命令对象。 如果您需要检测,则检测应该位于这些 Command 对象上,而不是 WinForms 事件上。 您可以利用控制反转容器,例如Castle Windsor 将日志记录/审核注入到命令中,而无需使用 拦截器模式。 这是 David Hayden 的示例
This does depend rather on your actual requirements.
For a simple/sample/prototype winforms app, I'd suggest basic WinForms Event Handlers, with the code (providing it's not too heavy) in the handler methods. If code is heavy, contains hard business rules and doesn't interact with the form then it's best to move that code to another class and call it from the event handler.
Aspect Oriented Programming takes this futher. It's generally best applied in larger-scale projects. You still need event handlers for your form elements (and I recommend sticking to the control-based event handlers - don't try creating your own global event handling facility), but they should make calls to Command objects (see Command Pattern in GangOfFour). Those command objects can then be invoked from anywhere in your application. If you want instrumentation, the instrumentation should be on those Command objects rather than the WinForms events. You can leverage Inversion of Control containers like Castle Windsor to inject logging/auditing into the commands without having to change your application at all using the Interceptor pattern. Here's an example from David Hayden
在 winforms 中捕获点击的一般方法是处理 Control.MouseClick。 您还可以查看 Control.MouseDown 和 Control.MouseUp如果您想了解更多详细信息。
如果您提供有关您正在尝试执行的操作的更多详细信息,我们可能会提出更有针对性的答案。
The general method for capturing clicks in winforms is handling Control.MouseClick. You can also look at Control.MouseDown and Control.MouseUp if you want more detailed information.
If you provide more details about what you're trying to do, we can probably come up with a more targeted answer.