namespace Kit.Helpers.Postgres { using System; using System.Collections.Generic; using System.Data; public class SqlServerHelperFluent { protected readonly string _connectionString; protected readonly string _sqlText; protected bool _isStoredProcedure; protected bool _isOutputParameter; protected List> _parameters; public SqlServerHelperFluent(string connectionString, string sqlText) { _connectionString = connectionString; _sqlText = sqlText; _isStoredProcedure = true; _isOutputParameter = false; _parameters = new List>(); } #region fluent methods public SqlServerHelperFluent AsStoredProcedure() { _isStoredProcedure = true; return this; } public SqlServerHelperFluent AsSqlText() { _isStoredProcedure = false; return this; } public SqlServerHelperFluent AddParameter(string name, object value) { _parameters.Add(new KeyValuePair(name, value)); return this; } public SqlServerHelperFluent AddParameter(string name, string value) { _parameters.Add(new KeyValuePair(name, value ?? string.Empty)); return this; } public SqlServerHelperFluent AddParameterNullable(string name, object value) { _parameters.Add(new KeyValuePair(name, value ?? DBNull.Value)); return this; } public SqlServerHelperFluent AddOutputParameter(string name, object value) { _parameters.Add(new KeyValuePair(name, new OutPutValue { Value = value ?? DBNull.Value })); return this; } public SqlServerHelperFluent AddParameters(IEnumerable> parameters) { _parameters.AddRange(parameters); return this; } #endregion #region finish methods public void ExecuteNonQuery() { _connectionString.ExecuteNonQuery(_sqlText, isStoredProcedure: _isStoredProcedure, parameters: _parameters); } public TResult ExecuteScalar() { return _connectionString.ExecuteScalar(_sqlText, isStoredProcedure: _isStoredProcedure, parameters: _parameters); } #endregion } public class SqlServerHelperFluent : SqlServerHelperFluent { protected List>> _converters; public SqlServerHelperFluent(string connectionString, string sqlText) : base (connectionString, sqlText) { _converters = new List>>(); } #region base fluent methods public new SqlServerHelperFluent AddParameter(string name, object value) { base.AddParameter(name, value); return this; } public new SqlServerHelperFluent AddParameterNullable(string name, object value) { base.AddParameterNullable(name, value); return this; } public new SqlServerHelperFluent AddParameters(IEnumerable> parameters) { base.AddParameters(parameters); return this; } public new SqlServerHelperFluent AsStoredProcedure() { base.AsStoredProcedure(); return this; } public new SqlServerHelperFluent AsSqlText() { base.AsSqlText(); return this; } #endregion public SqlServerHelperFluent AddConverter(Action> converter) { _converters.Add(converter); return this; } public IEnumerable ExecuteSelectMany() { return _connectionString.ExecuteSelectMany(_sqlText, isStoredProcedure: _isStoredProcedure, parameters: _parameters, converters: _converters.ToArray()); } public IEnumerableWithPage ExecuteSelectManyWithPage() { return _connectionString.ExecuteSelectManyWithPage(_sqlText, isStoredProcedure: _isStoredProcedure, parameters: _parameters, converters: _converters.ToArray()); } public IEnumerableWithOffer ExecuteSelectManyWithOffer() { return _connectionString.ExecuteSelectManyWithOffer(_sqlText, isStoredProcedure: _isStoredProcedure, parameters: _parameters, converters: _converters.ToArray()); } } public static class SqlServerHelperFluentExtentions { public static SqlServerHelperFluent PrepareExecute(this string connectionString, string sqlText) { return new SqlServerHelperFluent(connectionString, sqlText); } public static SqlServerHelperFluent PrepareExecute(this string connectionString, string sqlText) { return new SqlServerHelperFluent(connectionString, sqlText); } } }