2

How to capture user clicks in a winform application without making the code very complicated? is AOP the answer? How to track instrumentations?

4

2 回答 2

5

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

于 2008-10-24T10:24:11.930 回答
0

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.

于 2008-10-24T01:25:48.273 回答