views:

16

answers:

0

I want to create an async version of my page to speed it up. However, the number of async requests I need depends on the user's personal settings.

For example, I have a dashboard that can display numerous things. I want to do something like this:

public void IndexAsync(string city) {
AsyncManager.OutstandingOperations.Increment(3);

NewsService newsService = new NewsService();
newsService.GetHeadlinesCompleted += (sender, e) =>
{
    AsyncManager.Parameters["headlines"] = e.Value;
    AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetHeadlinesAsync();

SportsService sportsService = new SportsService();
sportsService.GetScoresCompleted += (sender, e) =>
{
    AsyncManager.Parameters["scores"] = e.Value;
    AsyncManager.OutstandingOperations.Decrement();
};
sportsService.GetScoresAsync();

WeatherService weatherService = new WeatherService();
weatherService.GetForecastCompleted += (sender, e) =>
{
    AsyncManager.Parameters["forecast"] = e.Value;
    AsyncManager.OutstandingOperations.Decrement();
};
weatherService.GetForecastAsync();
}

public ActionResult IndexCompleted(string[] headlines, string[] scores, string[] forecast) {
return View("Common", new PortalViewModel  {
    NewsHeadlines = headlines,
    SportsScores = scores,
    Weather = forecast
});
}          
}

My issue is that I don't know how many "parameters" I will need to do. It's hard to explain exactly, but I really need the following:

public void IndexAsync(string city) {

  AsyncManager.OutstandingOperations.Increment(items.Count());
  foreach(var item in items){

  //here we check what the user needs      
  switch(item){

  case "headlines":{    

    NewsService newsService = new NewsService();
    newsService.GetHeadlinesCompleted += (sender, e) =>
    {
        AsyncManager.Parameters["headlines"] = e.Value;
        AsyncManager.OutstandingOperations.Decrement();
    };
    newsService.GetHeadlinesAsync();
}
  case "scores": {

    SportsService sportsService = new SportsService();
    sportsService.GetScoresCompleted += (sender, e) =>
    {
        AsyncManager.Parameters["scores"] = e.Value;
        AsyncManager.OutstandingOperations.Decrement();
    };
    sportsService.GetScoresAsync();
 }
   case "forecase": {
    WeatherService weatherService = new WeatherService();
    weatherService.GetForecastCompleted += (sender, e) =>
    {
        AsyncManager.Parameters["forecast"] = e.Value;
        AsyncManager.OutstandingOperations.Decrement();
    };
    weatherService.GetForecastAsync();
    }
 }

    public ActionResult IndexCompleted(string[] headlines, string[] scores, string[] forecast) {
    return View("Common", new PortalViewModel  {
        NewsHeadlines = headlines,
        SportsScores = scores,
        Weather = forecast
    });
    }          
  }
 }
}

The issue is that the user might need two "headlines" for two different cities, and so in the IndexCompleted I can't necessarily hardcode the parameters that I will have. E.g.

I might have

public ActionResult IndexCompleted(string[] headlines, string[] scores, string[] forecast)

or I might have

public ActionResult IndexCompleted(string[] headlines_denver, string[] headlines_newyork, string[] scores, string[] forecast)

depending on the user's settings (and in this example, for what cities they want on their dashboard).

Any suggestions on how to make this async?