asp.net c# public statics and concurrent users
I have a MyClassFile.cs file that looks like this:
public class MyClass1
{
public static DataTable ReadData(string Connection, string Command, Dictionary Parameters)
{
//ADO.NET stuff here to .Fill() the DataTable Below
return dtTable;
}
public static void WriteData(string Connection, string Command, Dictionary Parameters)
{
//ADO.NET stuff to write to DB
}
}
I use it in my Default.aspx.cs like this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var Parameters = new Dictionary<string, object>
{
{ "?Parameter1", TextBox1.Text },
{ "?Paramet开发者_如何学JAVAer2", TextBox2.Text }
};
DataTable dtTable = MyClass1.ReadData(Connection, Command, Parameters);
ListView1.DataSource = dtTable;
ListView1.DataBind();
}
}
This is for a website. Will I run into problems with this code especially since ReadData() is public static? What if there are a lot of users concurrently using it?
My goal is to have a reusable ".ReadData(x,y,z)" and ".WriteData(x,y,z)"
It looks like those methods don't have any shared state - they're not using any static variables - so you should be okay.
I'd personally prefer to have the data layer injected into the presentation layer (or have another layer between) to make testing easier - static methods are often the enemy of testing - but that's a slightly different matter.
Static methods are inherently threadsafe, as long as they're not accessing objects that are shared by other parts of your application. So in your ReadData/WriteData methods, if you're just accepting parameters, creating a DB connection, executing statements and returning something then you should be OK.
精彩评论