Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# static database class?

I have a Database class which contanins the follwing methods:

  • public bool ExecuteUDIQuery(string query) // UDI = Update Delete Insert
  • public bool ExecuteSelectQuery(string query)
  • public bool ExecuteSP(string sp, string[,] parms)
  • public int ExecuteSPReturnValue(string sp, string[,] parms)

The results of the methods are stored in private datasets or datatables. These objects are defined as getters.

There're about 10 classes which use the Database class. Every class creates an object of the class Database. Now i was thinking to make the Database class static. Is this a good idea? If so, why? Of not, why not?

like image 765
Martijn Avatar asked Mar 12 '09 08:03

Martijn


1 Answers

If I understand, the database class has some properties that store the result of the query? If so, you cannot make them static, since that is not thread-safe. If the result of a query is stored in these properties, what would happen if a second query would execute right after the first? It would be stored in the same static variable. The same goes for a web application: the result of another user browsing the site would change the results of the first user.

EDIT: To summarize, do NOT make the class static when you store the result of the queries in static variables, especially not when the class is used in a website, as the properties value will be shared amongst all visitors of your website. If 20 visitors do a query at the same time, visitor 1 will see the results of visitor 20's query.

like image 101
Razzie Avatar answered Sep 22 '22 21:09

Razzie