user8360241
user8360241

Reputation:

Handling async situations in an .NET MVC application

I wanted to get some basic .NET MVC understanding so I started working on a very simple application that is somewhat similar to Yelp. It consists of a User object and Business object. A user can add businesses to their "watch list" and when someone creates a review for that business the application would look through a table of "user id-business id" relations and email the users that are watching that business for new reviews (so that they can respond to them).

In my ReviewsController, I have a Create action that creates a review. Before that action returns, I would like to run a method that checks if there are any watches for the business the review was just created for.

This is where I get stuck because I'm not sure what would be a good way of doing this in MVC setting(?).

What I do know is that the user who is creating the review does not need to wait for the application to look through the "user-business" relation table.

Would it be a good idea to create this lookup functionality as an async method? I also looked into events but seems like events are not ideal in MVC setting. Are there any well-established patters in MVC world for situations like these?

Upvotes: 1

Views: 99

Answers (3)

user8360241
user8360241

Reputation:

@Jonathan Tyson pointed out this article which shows some great options for background taks.

I am thinking of going with QueueBackgroundWorkItem as it seems to be a good fit for my scenario. It is already build in with .Net 4.5.2 and it does everything I am looking for.

Beware of the 90 second limit on it (might not be a great solution for everyone). In my case it is more than enough time.

Upvotes: 2

oopsdazie
oopsdazie

Reputation: 761

Another way of doing it, is to use ajax in View. Ajax is designed to be async, therefore, it is suitable for your use.

Whenever the post review button is clicked, an ajax request is sent to server to perform the CheckWatches action in parallel.

Here is a link to a simple example of an ajax call:

Making a Simple Ajax call to controller in asp.net mvc

Upvotes: 1

anmarti
anmarti

Reputation: 5143

You can use the Task Parallel Library to create asyncronous tasks. The following code executes the CreateReview method, when it finishes creates a thread and executes the method CheckWatches. At the same time it returns the action Create whitout waiting the new thread finishes.

public ActionResult Create()
{
     // User related task
     CreateReview();
     Task.Factory.StartNew(() =>
      {
               // system related task 
             CheckWatches();       
      });

    return View();
}

Upvotes: -2

Related Questions