Using Static methods or none static methods in Dao Class?

前端 未结 5 1495
庸人自扰
庸人自扰 2020-12-30 06:22

Hi I generate Dao classes for some DB operations

in this manner making methods of Dao class as static or none static is better?

Using sample dao class belo

5条回答
  •  天涯浪人
    2020-12-30 06:50

    It would result in a big mess. If you are adding 2 items simultaneously from different threads, you would certainly end up with very strange results, or even errors if one thread closes the DataAcessor before the other completes.

    I would use a local DataAcessor or create a new and use that in all the methods depending on how you want to manage the lifetime of DataAcessor.

    public class SampleDao
    {
      public void AddSampleItem(object[] params)
      {
          DataAcessor dataAcessor =new DataAcessor();
          // ...
      }
    
      public void UpdateSampleItem(object[] params)
      {
          DataAcessor dataAcessor =new DataAcessor();
          // ...
      }
    }
    

提交回复
热议问题