I have a desktop application with each window decopuled into presentation layer (containing the Form/Controls etc.) and application layer (Controller/Presenter - naming is irrelevant to the question). Presentation layer is passive, it only displays the data provided by application layer and comunicates with it using events. Application layer does all the plumbing connecting events to methods which populate the presentation layer with data from DB.

I use threads or BackgroundWorker to do time-consuming loading in background. My question is: which layer should be aware of the background operation implemenation and start the background threadS?

If i put the BackgroundWorker in the presentation layer, than controller's implementation is greatly simplified - it doesn't care if particular event was fired in background or not. However this complicates the code of the Form, which already tends to be bloated with it's controls event handling methods (click etc.).

I would like to know where and why the background-thread aware code should be.

  • If you start using async-await approach, then depends on what kind of loading you do, you can get rid of threads at all. – Fabio Oct 3 '16 at 9:41
  • 2
    I think UI need to know minimum details about how data provided. But if reason for starting some loading method in new thread or BackgroundWorker is relevant for UI logic, then start it in UI. Other case it is responsibility of controller/presenter logic. – Fabio Oct 3 '16 at 9:46
  • @Fabio i cannot use async, i'm stuck with legacy .NET :(. Could you explain why it's responsiblity of controller/presenter? That's the clarification i want. If you could make it as an answer i'll accept it than. – Mariusz Jamro Oct 3 '16 at 13:34
  • This is not solution - just suggestion: if you working with .NET 3.5 or 4 you can try using NuGet package AsyncBridge, which provide support for async-await. – Fabio Oct 3 '16 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.