2

I have a long running background worker control that exists as a part of my main form. Up until now, this worker's method hasn't had a lot to do. But the addition of some new features and some better error detection, within the worker method, has meant that it has now bloated a little, with a number of methods that have been extracted out of the worker method.

The main job that the background worker does, is execute methods on many instances of a user control that are loaded and displayed in a panel control. The background worker and all the user controls are children of the main form. As the background worker is now getting quite large, I thought it was time to extract the background worker (or at least the methods) into its own class to make future changes easier, improve readability and simplify access to data between the background worker methods.

My question is this, how should I structure the new class with respect to the user controls and the main form?

My thoughts have been...

  • To pass in all the references of the user controls into the new class as the background worker begins. This would maintain the parent-child-sibling relationships as they were before but I don't know if passing UI element references into a non-UI class is a bad idea. Also not very abstract.
  • Use events from within the new class to have the main form execute the methods in the user controls. This feels messy, unnecessary and easy to break. Would be abstract.
  • Some how make the new class a parent of all instances of the user control. I feel like the main form should be the parent of the user controls as they are both UI elements whereas the new class would have no UI elements. Though I feel this offers better structure but less abstraction (see below).

I think that I'm struggling with the program hierarchy in terms of UI and function. Is the display of controls more important than the function or the other way round? I feel like the function should be as abstract as possible from the UI, that the UI is really just a fancy way of setting arguments in what could be a command-line program. I also think abstraction is my buzz philosophy at the moment (I've learned programming through micro-controllers where it doesn't count as much.)

It's been a long day and I'm not sure if any of that makes sense. Please feel free to correct me and I'll try to clarify anything too confusing. Thanks in advance.

4

1 に答える 1