using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.Common; namespace ChadSoft.DataAccess { public abstract class Database { internal const CommandType DefaultCommandType = CommandType.Text; internal const string DefaultDatabaseNameAppSetting = "DefaultDatabaseName"; protected string connectionName; protected Database() : this(ConfigurationManager.AppSettings[DefaultDatabaseNameAppSetting]) { } protected Database(string connectionName) { if (string.IsNullOrEmpty(connectionName)) throw new ArgumentNullException("connectionName", "Please provide a database connection name."); this.connectionName = connectionName; } /// /// Executes the specified command text as a nonquery. /// /// The command text. /// The strongly-typed result of the query public int ExecuteNonQuery(string commandText) { return ExecuteNonQuery(commandText, null); } /// /// Executes the specified command text as a non query. /// /// The command text. /// The parameters. /// The strongly-typed result of the query public int ExecuteNonQuery(string commandText, IDictionary parameters) { return ExecuteNonQuery(commandText, DefaultCommandType, parameters); } /// /// Executes the specified command text as a non query. /// /// The command text. /// Type of the command. /// The parameters. /// The strongly-typed result of the query public int ExecuteNonQuery(string commandText, CommandType commandType, IDictionary parameters) { return Execute(commandText, commandType, parameters, (cmd => cmd.ExecuteNonQuery())); } /// /// Executes the specified command text as a scalar. /// /// The query's result type /// The command text. /// The strongly-typed result of the query public T ExecuteScalar(string commandText) { return ExecuteScalar(commandText, null); } /// /// Executes the specified command text as a scalar. /// /// The query's result type /// The command text. /// The parameters. /// The strongly-typed result of the query public T ExecuteScalar(string commandText, IDictionary parameters) { return ExecuteScalar(commandText, DefaultCommandType, parameters); } /// /// Executes the specified command text as a scalar. /// /// The query's result type /// The command text. /// Type of the command. /// The parameters. /// The strongly-typed result of the query public T ExecuteScalar(string commandText, CommandType commandType, IDictionary parameters) { return Execute(commandText, commandType, parameters, (cmd => (T)cmd.ExecuteScalar())); } /// /// Executes the specified command text as a reader. /// /// The query's result type /// The command text. /// The conversion function. /// The strongly-typed result of the query public T ExecuteReader(string commandText, Func conversionFunction) { return ExecuteReader(commandText, null, conversionFunction); } /// /// Executes the specified command text as a reader. /// /// The query's result type /// The command text. /// The parameters. /// The conversion function. /// The strongly-typed result of the query public T ExecuteReader(string commandText, IDictionary parameters, Func conversionFunction) { return ExecuteReader(commandText, DefaultCommandType, parameters, conversionFunction); } /// /// Executes the specified command text as a reader. /// /// The query's result type /// The command text. /// Type of the command. /// The parameters. /// The conversion function. /// The strongly-typed result of the query public T ExecuteReader(string commandText, CommandType commandType, IDictionary parameters, Func conversionFunction) { var cmd = new DbDataReaderToObjectCommand(conversionFunction); return Execute(commandText, commandType, parameters, cmd.Execute); } /// /// Executes the specified command text. /// /// The command text. /// Type of the command. /// The parameters. /// The result processor. /// The result of the public object Execute(string commandText, CommandType commandType, IDictionary parameters, Func resultProcessor) { return Execute(commandText, commandType, parameters, resultProcessor); } /// /// Executes the specified command text. /// /// The query's result type /// The command text. /// Type of the command. /// The parameters. /// The result processor. /// The result of the public abstract T Execute(string commandText, CommandType commandType, IDictionary parameters, Func resultProcessor); /// /// Gets the connection string. /// /// Connection string to the database protected string GetConnectionString() { var connectionInfo = ConfigurationManager.ConnectionStrings[connectionName]; return connectionInfo.ConnectionString; } private class DbDataReaderToObjectCommand { private readonly Func conversionFunction; public DbDataReaderToObjectCommand(Func conversionFunction) { this.conversionFunction = conversionFunction; } public T Execute(DbCommand command) { using (var reader = command.ExecuteReader()) return conversionFunction(reader); } } } }