简体   繁体   中英

Separating hundreds of event handlers

This is an issue of code management that I've run into several times. Say I've got a winforms application, and there is a lot of content - hundreds of controls worth of content, most of which have an attached event handler and method. When looking for recommendations on how to separate out such a large file, the 2 big ones that come to mind are regions & partial classes. However, these are both damned by almost every top answer out there.

So I'm wondering, what is the best practice for separating out something like this? It wouldn't make sense to separate them into new classes as they're simply control event handlers. Regions are nice but can still be extremely messy with so much code. I'm kind of at a loss, I can't figure out how to apply the SRP here and any recommendation I see seems to be considered bad practice.

The 'proper' way would be to split the form up into user controls.

Partial classes are meant to separate generated code from user code and regions are just layout (and useless in my opinion, but that is just an opinion)

Splitting the window in user controls promotes proper encapsulation and re-use.

Depending on the project, it might be too expensive to clean it up but splitting out couple of easy ones might help the readability and might save you some time when debugging. Also, when adding new controls and logic, consider adding user controls and start the process of splitting up that way. The cost will then be spread over a longer period and will only be paid for part that you actually use and need to be readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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