Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I make this class static?

Tags:

c#

In the projects I worked on I have classes that query/update database, like this one,

public class CompanyInfoManager
{
    public List<string> GetCompanyNames()
    {
         //Query database and return list of company names
    }
}

as I keep creating more and more classes of this sort, I realize that maybe I should make this type of class static. By doing so the obvious benefit is avoid the need to create class instances every time I need to query the database. But since for the static class, there is only one copy of the class, will this result in hundreds of requests contend for only one copy of static class?

Thanks,

like image 205
sean717 Avatar asked Jul 14 '10 21:07

sean717


3 Answers

I would not make that class static but instead would use dependency injection and pass in needed resources to that class. This way you can create a mock repository (that implements the IRepository interface) to test with. If you make the class static and don't pass in your repository then it is very difficult to test since you can't control what the static class is connecting to.

Note: The code below is a rough example and is only intended to convey the point, not necessarily compile and execute.

public interface IRepository
{
   public DataSet ExecuteQuery(string aQuery);
   //Other methods to interact with the DB (such as update or insert) are defined here.
}

public class CompanyInfoManager
{
   private IRepository theRepository;
   public CompanyInfoManager(IRepository aRepository)
   {
      //A repository is required so that we always know what
      //we are talking to.
      theRepository = aRepository;
   }

   public List<string> GetCompanyNames()
   {
      //Query database and return list of company names
      string query = "SELECT * FROM COMPANIES";
      DataSet results = theRepository.ExecuteQuery(query);
      //Process the results...
      return listOfNames;
   }
}

To test CompanyInfoManager:

//Class to test CompanyInfoManager
public class MockRepository : IRepository
{
   //This method will always return a known value.
   public DataSet ExecuteQuery(string aQuery)
   {
      DataSet returnResults = new DataSet();
      //Fill the data set with known values...
      return returnResults;
   }
}

//This will always contain known values that you can test.
IList<string> names = new CompanyInfoManager(new MockRepository()).GetCompanyNames();

I didn't want to ramble on about dependency injection. Misko Hevery's blog goes into great detail with a great post to get started.

like image 159
brainimus Avatar answered Oct 09 '22 02:10

brainimus


It depends. Will you ever need to make your program multithreaded? Will you ever need to connect to more than one database? Will you ever need to store state in this class? Do you need to control the lifetime of your connections? Will you need data caching in the future? If you answer yes to any of these, a static class will make things awkward.

My personal advice would be to make it an instance as this is more OO and would give you flexibility you might need in the future.

like image 2
DanDan Avatar answered Oct 09 '22 00:10

DanDan


You have to be careful making this class static. In a web app, each request is handled on its own thread. Static utilities can be thread-unsafe if you are not careful. And if that happens you are not going to be happy.

I would highly recommend you follow the DAO pattern. Use a tool like Spring to make this easy for you. All you have to do is configure a datasource and your DB access and transactions will be a breeze.

like image 1
hvgotcodes Avatar answered Oct 09 '22 00:10

hvgotcodes