<p>I've seen projects where the classes in the DB layer have just static functions in them and other projects where those classes need to be instantiated to get access to the member functions. </p>

<blockquote>
  <p>Which is "better" and why?</p>
</blockquote>

[{'answer_id': 16342, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s all about the purpose of the DB Layer.\nIf you use an instance to access the DB layer, you are allowing multiple versions of that class to exist. This is desirable if you want to use the same DB layer to access multiple databases for example.</p>\n\n<p>So you might have something like this:</p>\n\n<pre><code>DbController acrhive = new DbController("dev");\nDbController prod = new DbController("prod");\n</code></pre>\n\n<p>Which allows you to use multiple instances of the same class to access different databases.</p>\n\n<p>Conversely you might want to allow only one database to be used within your application at a time. If you want to do this then you could look at using a static class for this purpose.</p>\n'}, {'answer_id': 16349, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': -1, 'selected': False, 'text': "<p>It depends which model you subscribe to.  ORM (Object Relational Model) or Interface Model.  ORM is very popular right now because of frameworks like nhibernate, LINQ to SQL, Entity Framework, and many others.  The ORM lets you customize some business constraints around your object model and pass it around with out actually knowing how it should be committed to the database.  Everything related to inserting, updating, and deleting happens in the object and doesn't really have to worry the developer too much.</p>\n\n<p>The Interface Model like the Enterprise Data Pattern made popular by Microsoft, requires you to know what state your object is in and how it should be handled.  It also requires you to create the necessary SQL to perform the actions.</p>\n\n<p>I would say go with ORM.</p>\n"}, {'answer_id': 16357, 'author': 'Barrett Conrad', 'author_id': 1227, 'author_profile': 'https://Stackoverflow.com/users/1227', 'pm_score': 2, 'selected': False, 'text': '<p>I like a single object to be correlated to a single record in the database, i.e. an object must be instantiated.  This is your basic <a href="http://martinfowler.com/eaaCatalog/activeRecord.html" rel="nofollow noreferrer">ActiveRecord</a> pattern.  In my experience, the one-object-to-one-row approach creates a much more fluid and literate presentation in code.  Also, I like to treat objects as records and the class as the table. For example to change the name of a record I do:</p>\n\n<pre><code>objPerson = new Person(id)\n\nobjPerson.name = "George"\n\nobjPerson.save()\n</code></pre>\n\n<p>while to get all people who live in Louisiana I might do</p>\n\n<pre><code>aryPeople = Person::getPeopleFromState("LA")\n</code></pre>\n\n<p>There are plenty of criticisms of Active Record.  You can especially run into problems where you are querying the database for each record or your classes are tightly coupled to your database, creating inflexibility in both.  In that case you can move up a level and go with something like <a href="http://martinfowler.com/eaaCatalog/dataMapper.html" rel="nofollow noreferrer">DataMapper</a>. </p>\n\n<p>Many of the modern frameworks and <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" rel="nofollow noreferrer">ORM\'s</a> are aware of some of these drawbacks and provide solutions for them.  Do a little research and you will start to see that this is a problem that has a number of solutions and it all depend on your needs. </p>\n'}, {'answer_id': 16361, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 0, 'selected': False, 'text': "<p>As lomaxx mentioned, it's all about the purpose of the DB model.</p>\n\n<p>I find it best to use static classes, as I usually only want one instance of my DAL classes being created. I'd rather use static methods than deal with the overhead of potentially creating multiple instances of my DAL classes where only 1 should exist that can be queried multiple times.</p>\n"}, {'answer_id': 16652, 'author': 'Brian G Swanson', 'author_id': 1795, 'author_profile': 'https://Stackoverflow.com/users/1795', 'pm_score': 0, 'selected': False, 'text': '<p>I would say that it depends on what you want the "DB layer" to do...</p>\n\n<p>If you have general routines for executing a stored procedure, or sql statement, that return a dataset, then using static methods would make more sense to me, since you don\'t need a permanent reference to an object that created the dataset for you.</p>\n\n<p>I\'d use a static method as well if I created a DB Layer that returned a strongly-typed class or collection as its result.</p>\n\n<p>If on the other hand you want to create an instance of a class, using a given parameter like an ID (see @barret-conrad\'s answer), to connect to the DB and get the necessary record, then you\'d probably not want to use a static method on the class.  But even then I\'d say you\'d probably have some sort of DB Helper class that DID have static methods that your other class was relying on.</p>\n'}, {'answer_id': 36490, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': '<p>Another "it depends".  However, I can also think of a very common scenario where static just won\'t work.  If you have a web site that gets a decent amount of traffic, and you have a static database layer with a shared connection, you could be in trouble.  In ASP.Net, there is <em>one</em> instance of your application created by default, and so if you have a static database layer you may only get <em>one</em> connection to the database for <em>everyone</em> who uses your web site.</p>\n'}]
['https://Stackoverflow.com/questions/16320', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']