Data Access Objects (DOAs)

In my experience, I have found that the best way to treat DAOs is to consider them to be a specialized type of factory. We can use the DAO to access a database or other service, retrieve the data, and then return a new instance of a class that contains the retrieved data. Typically, a DAO is a singleton, since we only want one a single point of connection to any given external resource. In this case, we simply make the actual connection a singleton, and pass that into the DAO. namespace FunctionalLayer.Daos { public class Database { const string queryString = "storedProc"; ... // private fields for database connection private DBDataFactory _factory; public Database(SingletonConnection databaseConnection) { ... } public virtual ReturnType ExecuteQuery(...parameters...) { var returned = Construct(); // execute query and fill out values in returned return returned; } protected ReturnType Construct() { return new ReturnType(); } protected virtual void Log(LogLevel level, string logMessage) {} } } namespace LoggingLayer.Daos { public class Database : FunctionalLayer.Daos.Database { private ILogger _logger; public DataBase(SingletonConnection databaseConnection, ILogger logger) : base(databaseConnection) { _logger = logger; } protected ReturnType Construct() { return new LoggingLayer.Objects.ReturnType(_logger); } protected override void Log(LogLevel level, string logMessage) { _logger.WriteLine(level, logMessage); } } }

Next: Meta-Factories