Question

I currently started a little project, expieriencing the world of JS and HTML5.

I tried a few months ago, already, but I stopped, because I've had not enough time to create a MVC single page application from scratch. There were too many concepts and patterns, that had to be understood and I would have regretted losing all that knowledge from lack of use on my daily work. Use it or lose it!

Yesterday I just found this post on John Papa's blog and I thought that would be great to use as a start. Basically it's an MVC template, called HotTowel, which implements already great concepts like data-binding, minification and so forth. I would experience the code as far as I needed for the moment and would experience it further, as soon as I'd need to.

I'd like to build an application for fetching data from my works existing data model project. In our Silverlight application, we bootstrap it through preloading and initializing dictionaries and other properties and calling async Init() methods (e.g. for downloading XML files containing custom codes and put them into dictionaries). MEF is used to get rid of unhandy dependencies.

As far as I understood server side initialization has to be done in the Application_Start() method in the Global.asax file. I wonder how I'd await async calls in this method? What's the best practices? My queries on the client side heavily rely on these classes to be initialized. What options are there?

My thoughts were the following:

  • Application_Start() fires and forgets the async initialization process. If I'd perform a request (on a controller I guess) before the initialization finished, I'd have to wait for the callback of the initialization process and start queries as soon as it arrives. Advantage of this'd be, that initialization runs, while the user already can navigate through the application.
  • I'd implement some kind of lazy initialization. I'd process the initialization as soon as the first request is made. That may take long for the first request, though.
  • I'd run the initialization process synchronously in Application_Start(). The major disadvantage of this I've seen so far is, that the browser window seems freezed to the user. I'd be comfortable with this solution, if it was possible to let the user keep track of the current initialization status (some kind of splash screen).

Though I don't know how any of them would work concretly and would be glad if any of you could give me some advices for how and where to start.

Was it helpful?

Solution

You can use a Task<MyDataModel> to represent the data.

static Task<MyDataModel> dataTask;
public static Task<MyDataModel> LoadDataModelAsync()
{
  var ret = new MyDataModel();
  await ret.Init();
  return ret;
}

Kick it off in Application_Start (or a static constructor):

dataTask = LoadDataModelAsync();

Then each of your actions that needs it can await for it to complete:

MyDataModel data = await dataTask;
...

If it's already complete, the await will detect that and continue (synchronously).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top