This is really a structure question of code as I am a bit unsure of how to proceed bearing in mind performance.
In the application I have a method which has to be run on every request which goes to the DB and gets some values back based on the url, before anything else can happen (Think CMS re need to know which site is being looked at).
I have always done this in the 'BaseController' on Initialize
with all other controllers inheriting from this and that seemed to work just fine. I.e. I put the result in a public variable and was able to access it in any ActionResult
or whatever I neeeded. The key was that it was only called once per page request.
Now we are kind of fundamentally changing the structure to a more 'widget' style. Every widget would need this data, and there could be anywhere from 1 to ... alot of widgets on the page.
When I get each widget's model I don't want to hit the DB every time to get the same info before getting the 'actual' data for the widget.
So should I get the 'data' once pre the loop and feed it into the method calls for each widget .... or is this a case where some type of dependency injection can help? (I am a bit 'green' in that area :P )
It's just I don't really want to write each widgets method to be e.g.
public List<string> WidgetAResult (object TheCoreObj, ... other stuff ...){}
and I also don't really want each widgets 'sql' script to have the same code at the begining because if something changes in the 'core' call then I would need to update all of them...
Any thoughts would be appreciated
Cheers
Robin