Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static methods appropriate for a Linq To SQL DAL?

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.

like image 911
Skoder Avatar asked Feb 10 '10 17:02

Skoder


2 Answers

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.

like image 97
Jeff Sternal Avatar answered Nov 14 '22 23:11

Jeff Sternal


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.

like image 40
tvanfosson Avatar answered Nov 14 '22 22:11

tvanfosson