views:

134

answers:

3

Hi!

I want to calculate how much queries are executed when i request a page in asp.net mvc. Difficults in page logic: sum queries are executed in main controller action, but others - in widget controller actions, which are called by Html.ActionLink in master page.

I wrote base class for all controllers in which i'm encapsulate query called function to increase queries counter - static variable. On page loading it work nice, all queries are counted, but when i requested second page my static counter doesn't reset, what should i do?

+1  A: 

If possible avoid using a static variable. An instance variable on the base controller would work just as easily if all the methods shared the same controller instance. I assume that you're not instantiating new controllers to execute the other actions.

EDIT: After thinking about it, a simple static variable won't work as it will be shared across all requests. You'll need to use some sort of dictionary keyed by some unique id identifying the request. Perhaps based on a combination of the HostAddress and a timestamp created when the request is initiated. Perhaps using the application cache would work so it's available from everywhere and automatically cleaned up when you are done. This is only going to get more complicated as you consider all the possible way that multiple threads may interact. Again, I'd say trying to use an instance variable may be the best way to handle this.

tvanfosson
locks will come to delays, it bad, perhaps there is some context of page execution in which i can store static variable?
hippout
sorry, what do you mean by instance variable?
hippout
Instead of declaring it static, just make it a field/property in the class so that it belongs only to that instance of the class, not to the class itself. Each instance will have it's own copy of the variable.
tvanfosson
i'm stupid=) i'm want to pass one database connection object thru all page (many controllers) - instance variable isn'y what i want
hippout
I prefer the Unit of Work pattern, instantiating a new (in my case) data context for each logically, related group of database "activities". I probably wouldn't use a single context for the entire lifecycle, especially if some of the page may be loaded via AJAX callbacks to other controllers. Perhaps you're trying to optimize (and measure) something that doesn't really need it.
tvanfosson
yes, u are right. i'm go from php to asp and i'm intuitively think that in asp work with databases very slow. i don't wnat to open several cnnections to db for one page
hippout
Depending on your provider, it might pool the connections for you and reuse/keep open connections. If you are connecting to SQL server, you shouldn't bother too much about keeping connections open as the pool manager will handle the connections for you.
tvanfosson
A: 

extend your MvcApplication Class (in your global.asax) with

 public static int QueryCount { get; set; }

then create a handler for the MvcApplication.BeginRequest event or extend your existing handler with

this.QueryCount = 0;

and in your BaseController Class you can increment your counter with

MvcApplication.QueryCount++;

hth

marc.d
This will be shared by all requests. What is really needed is a per-request variable. If multiple controllers are used I don't see a good way to handle it except possibly via the cache using some sort of deterministic key based on the individual request.
tvanfosson
oh, it's very expensive. i begin to hate asp.net mvc architecture...
hippout
A: 

Are you using SQL Server? If so you can use the SQL Server Profiler from the Tools menu to see how many queries are run for a specific page.

MHinton
No, i'm using MySql, but if so, i can need this opportunity not only in db context. i want to solve this problem in general way
hippout