MVC - Where do YOU put your async operations?


A

Anthony Paul

Hello everyone,

In order to keep my UI operations responsive, I've always had my
Controller run my Model's methods asynchronously via the ThreadPool.
But one day I started wondering whether or not the better approach was
to have the Model implement the methods asynchronously, possibly even
by implementing the CLR's Asynchronous Programming Model. Basically I
would like to know what the consensus is, whether people think the
better approach is for the Model to implement async methods (possibly
via the APM) or not.

To better illustrate what I'm talking about, I've included Example A
(what I currently do) and Example B (is this a better approach?).

Example A : Controller takes care of async

public class Model
{
public void RunSomeMethod()
{
// some long running operation
}
}

public class Controller
{
private Model MyModel;

public SomeMethod()
{
ThreadPool.QueueUserWorkItem
(
(WaitCallback) delegate
{
MyModel.RunSomeMethod();
}
);
}
}

Example B : Model takes care of async (could be via APM)

public class Model
{
public void RunSomeMethod()
{
ThreadPool.QueueUserWorkItem
(
(WaitCallback) delegate
{
// some long running operation
}
);
}
}

public class Controller
{
private Model MyModel;

public SomeMethod()
{
MyModel.RunSomeMethod();
}
}

Regards,

Anthony
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top