我正在为我的DAL使用
Linq to SQL,并且已经听说过在Web应用程序中使用静态方法的各种事情(关于线程/并发问题).目前,我创建了一个测试DAL,它似乎运行正常.但是,我创建它的方式有什么问题,因为它是静态的吗?
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 }
这种方法适用于Web应用程序,还是生产环境中存在问题?
谢谢.
解决方法
只要您的静态方法不使用任何共享状态(类级别状态或其他全局状态),它们本身就不会导致在多线程环境中运行任何问题.每个静态方法调用都将创建自己的局部变量的副本.