views:

186

answers:

3

Hey,

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.

+3  A: 

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.

tvanfosson
@tvanfosson: Thanks for the advice. This current project is quite small, so testing isn't a deciding factor. However, I will certainly bear instance methods in mind when working on a larger project.
Skoder
+4  A: 

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.

Jeff Sternal
@Jeff: Thanks for the reply. No, the methods are pretty much self-contained in that regard. I just remember seeing a few L2S articles, and they seemed to have a non-static class with only one DataContext instance (which is used by the other methods). I just wanted to make sure that the method I have, with new DataContexts being created per method, wouldn't be problematic.
Skoder
Sorry, forgot to ask. If one method invokes another method, would that count as variables being shared (where either method can also be called individually)? Or would each method still create their own local variable anyway?
Skoder
Static methods can invoke one another without causing threading problems, as long as both methods only use local variables (and method parameters).To learn about what's at stake when running in a multithreaded environment (like a web app), and what can go wrong, I recommend reading Joseph Albahari's oft-recommended "Threading in C#" (at http://www.albahari.com/threading/) and Jon Skeet's "Multi-threading in .NET" (http://www.yoda.arachsys.com/csharp/threads/).
Jeff Sternal
Thanks Jeff, those are great links
Skoder
+2  A: 

From the snippet you provided it is not very clear what the various methods do. The way I see it, as long as you are using local variables, it is safe.

Darin Dimitrov
@Darin: Thanks for the reply. The other methods are pretty generic, but none of them share any global variables.
Skoder