multithreading - Who should handle threading in MVC? -


Long-running tasks are usually performed in the background thread to keep the UI freezing. It seems that threading logic can be in any view or controller.

As an example (in C #), suppose that RunAsync is a method called in which a delegate runs a background thread, there are two ways to do this:

  // Option 1 See Public Category {Public Zero onButtonClicked () {RunAsync ((=) => Controller. DoSomething ()); }} Public Sector Controller {Public Zero DoSomething () {model.Foo (); }}   

or:

  // Option 2 See public category {Public Zero onButtonClicked () {controller.DoSomething (); }} Public Sector Controller {Public Zero DoSomething () {RunAsync ((=) => model.Foo ()); }}   

Is it an advantage to do one or the other?

I see two arguments for the controller responsible for thread security.

  1. The controller (at least perceptual) is reusable by several ideas. We repeat ourselves but put Runsink () in the controller instead of many scenes.
  2. Only the controller really "knows" whether there is any such threading required. Actually we can change the controller in the future. Therefore, we have a "single responsibility" approach to thinking that both the controller decides that RunAyncch () is required and in reality it is ensured that it is done.

Comments