Question

Imagine you have a View within ASP.Net MVC which have got several Partial Views embedded in it which work independently and each load their own model through Html.Action (Child Actions). Now each separate Partial View would need to load certain items from the database. In terms of performance it would be much more efficient to group all such database calls together and preload all the items of the same type with one query rather than one by one through each separate Child Action.

For example, lets say we have the following structure:

  • Index (View)
    • Main Menu (Child Action / Partial View)
      • This would load Sections with IDs 1 & 2
    • Main Content (Child Action / Partial View)
      • This would load Sections with IDs 3, 4, & 5
    • Related Sections (Child Action / Partial View)
      • This would load Sections with IDs 6 & 7
    • ...

Now in the above scenario, each child action above would separately make the respective database calls to load the sections required by its own model.

In order to improve on performance, we need a way whereby before we execute each Action or Child Action, such database item requests are 'registered' and then be able to load all the items together. Then when the Action or Child Action would need to use such database items, they would have already been preloaded altogether.

We were thinking of using Action Filters which are attached to each Action & Child Action stating which items are requested but unfortunately the OnActionExecuting method is called separately prior to each action being call.

We would like something similar to Action Filters but rather than being executed prior to each call, they are executed all in the beginning of the request before any Action / Child Action is called. This way we can register all such database item requirements and load them together.

Something similar to the OnInit event within Asp.Net Web Forms whereby first the OnInit for all user controls & controls is called followed by the other events within the ASP.Net Web Forms life cycle.


Or maybe you can offer a different solution which performs similarly to what we require?

Was it helpful?

Solution 2

Actually there is no way to predict which child actions will be invoked in a view. Also you may have nested calls to child actions, invocations from the Layout, etc.

In you situation I see two ways to optimize performance: 1) Use caching - it will improve performance for the further requests, but not the first one 2) Instead of having only child actions - use partial views. In this case your main Index action will load all necessary items, build a model containing properties MenuItems, Content, RelatedSections and pass them to Index view. Then from index view you will invoke RenderPartial and pass corresponding data as a model to this partial. If you need AJAX functionality to retrieve updated part of the page - then you need to create separate actions for the each partial view.

In a short - use RenderPartial for the normal requests, and Actions for the each view in case of AJAX requests.

If you are not planning to use AJAX - then it's better to use only RenderPartial method, since Action is heavier and will work a little bit slower, since routing, instantioation of a controller, etc is involved to render HTML for child action.

OTHER TIPS

Child actions are explicitly intended for unrelated content: things you actually couldn't reasonably combine in a single DB query, anyways. If it's all related things and you can query all at once, then just add it all to a view model, and use partial views with just RenderPartial.

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