0

I'm doing a Database class for my application to connect to a Sql Server database and I'm thinking in centralize queries and procedures execution in it. But I'm in doubt between having one method for each action (this will probably get to a hundred methods) or creating a more "generic" method.

Examples:

1) One method for each action:

public bool TryLogin(string username, string password) {
[do query/procedure things and return]
}

2) A "generic" method:

public DataSet procedure(string name, Dictionary<string, object> values) {
[run through the values dictionary to 
define the parameters, run and return a dataset for caller class]
}

I thought about the first one, but I'm wondering if having too many methods wouldn't be bad for performance.

For the second one, I thought about using SqlParameter AddWithValue(string, object), though I'm not sure how it works.

Which of them is better? Or is there a better solution?

Thanks in advance and sorry for english errors.

1 Answers1

1

I prefer the first, although you could combine the two and have your TryLogin method call down to the procedure method to do the database call. Then you only need to re-implement the database call if there is a special need for one of your stored procedures. With a helper method to construct the values argument you may save a bit of code overall.

With the "generic" method alone you are forcing callers to construct Dictionaries and it looks like you're also forcing callers to know procedure and parameter names. So now you've created unnecessary complexity for callers, forfeited type checking on parameters and your data layer is leaking into your business objects.

I wouldn't encapsulate all of my database calls into a single class, either. That sounds like a maintenance nightmare. At the least I would break them down by their domain.

Matthew Jaspers
  • 1,546
  • 1
  • 10
  • 13
  • if I use the first way, will having too many methods hit the performance? In the case of breaking it, I would make a connection class and other smaller classes for procedures that uses the connection? Thanks – Guilherme G. Menaldo Feb 14 '15 at 12:23
  • Having too many methods in this case probably wouldn't hurt performance, but it is a [code smell](http://stackoverflow.com/questions/1415665/how-many-methods-can-a-c-sharp-class-have). Having one connection class with smaller classes that make use of that connection is how I would probably architect it. – Matthew Jaspers Feb 14 '15 at 13:57