namespace LaaProduction.SQL { using LaaProduction.SQL.Interfaces; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; public class SQLCommand : ISQLCommand { private readonly SQLConnection sqlConnection; private readonly IDictionary parameters; public SQLCommand(SQLConnection sqlConnection, string commandText) { this.sqlConnection = sqlConnection.EnsureNotNull(nameof(sqlConnection)); this.CommandText = commandText.EnsureNotNullOrWhiteSpace(nameof(commandText)); this.parameters = new Dictionary(); } internal string CommandText { get; } internal SqlParameter[] Parameters => this.parameters.Values.ToArray(); public int ExecuteNonQuery() => this.sqlConnection.ExecuteNonQuery(this); public IEnumerable ExecuteReader(Func expression) { foreach (var result in this.sqlConnection.ExecuteReader(this, expression)) { yield return result; } } public T ExecuteScalar(Func expression) => this.sqlConnection.ExecuteScalar(this, expression); public T FirstOrDefault(Func expression) => this.sqlConnection.FirstOrDefault(this, expression); public ISQLCommand SetParameter(string name, object value) { name.ThrowIfNullOrWhiteSpace(nameof(name)); if (value is null) { value = DBNull.Value; } var parameter = new SqlParameter(name, value); this.parameters[name] = parameter; return this; } public ISQLCommand SetParameters(IEnumerable> parameters) { foreach (var kvp in parameters) { this.SetParameter(kvp.Key, kvp.Value); } return this; } } }