I'm using Linq to SQL for my DAL and have heard various things about using static methods in a web application (regarding threading/concurrency issues). At the moment, I created a test DAL, which seems to be functioning fine. However, are there any problems with the way I've created it, since it's static?
public static class TestDAL
{
public static bool GetUserAddress(string username)
{
testDBDataContext dbContext = new testDBDataContext();
//Linq code goes here
}
public static void InsertUserNumber(int userID)
{
testDBDataContext dbContext = new testDBDataContext();
//...
dbContext.UserDetails.InsertOnSubmit(nUser);
dbContext.SubmitChanges();
}
//etc... All the methods are created in the same way
}
Is this method fine for a web application, or will there be problems in a production environment?
Thanks.
As long as your static methods don't use any shared state (class-level state or other global state), they won't themselves cause any problems running in a multithreaded environment. Each static method invocation will create copies of its own local variables.
Personally I would avoid static methods as it will make this code much harder to test around. You won't be able to easily mock out the DAL when testing code that uses it. Note that this isn't unique to LINQ or data access layers, it's just a function of the code being a class method rather than an instance method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With