/// <inheritdoc /> public StoredProcedureExecutionResult Execute( ExecuteStoredProcedureOp operation) { var outputParametersWithExecutionResult = new Dictionary <string, ISqlOutputParameterResult>(); using (var sqlConnection = this.sqlServerLocator.OpenSqlConnection(this.defaultConnectionTimeout)) { using (var command = sqlConnection.BuildSqlCommand(operation.Name, (int)this.defaultCommandTimeout.TotalSeconds)) { command.CommandType = CommandType.StoredProcedure; var outputParameters = new List <Tuple <SqlParameter, OutputParameterDefinitionBase> >(); foreach (var parameterRepresentation in operation.Parameters) { var parameter = parameterRepresentation.ToSqlParameter(); if (parameter.Direction == ParameterDirection.Output) { if (parameterRepresentation is OutputParameterDefinitionBase outputParameterRepresentation) { outputParameters.Add(new Tuple <SqlParameter, OutputParameterDefinitionBase>(parameter, outputParameterRepresentation)); } else { throw new NotSupportedException(FormattableString.Invariant($"Cannot have a {nameof(SqlParameter)} with {nameof(SqlParameter.Direction)} equal to {nameof(ParameterDirection.Output)} and the representation not be a {nameof(OutputParameterDefinitionBase)}, it was a {parameterRepresentation.GetType().ToStringReadable()}.")); } } command.Parameters.Add(parameter); } command.ExecuteNonQuery(); foreach (var outputParameter in outputParameters) { var outputParameterWithResult = outputParameter.Item2.CreateResult(outputParameter.Item1.Value); outputParametersWithExecutionResult.Add(outputParameter.Item2.Name, outputParameterWithResult); } } } var result = new StoredProcedureExecutionResult(operation, outputParametersWithExecutionResult); return(result); }
static ExecuteStoredProcedureOpTest() { ConstructorArgumentValidationTestScenarios .RemoveAllScenarios() .AddScenario(() => new ConstructorArgumentValidationTestScenario <ExecuteStoredProcedureOp> { Name = "constructor should throw ArgumentNullException when parameter 'name' is null scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <ExecuteStoredProcedureOp>(); var result = new ExecuteStoredProcedureOp( null, referenceObject.Parameters); return(result); }, ExpectedExceptionType = typeof(ArgumentNullException), ExpectedExceptionMessageContains = new[] { "name", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <ExecuteStoredProcedureOp> { Name = "constructor should throw ArgumentException when parameter 'name' is white space scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <ExecuteStoredProcedureOp>(); var result = new ExecuteStoredProcedureOp( Invariant($" {Environment.NewLine} "), referenceObject.Parameters); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "name", "white space", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <ExecuteStoredProcedureOp> { Name = "constructor should throw ArgumentNullException when parameter 'parameters' is null scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <ExecuteStoredProcedureOp>(); var result = new ExecuteStoredProcedureOp( referenceObject.Name, null); return(result); }, ExpectedExceptionType = typeof(ArgumentNullException), ExpectedExceptionMessageContains = new[] { "parameters", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <ExecuteStoredProcedureOp> { Name = "constructor should throw ArgumentException when parameter 'parameters' contains a null element scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <ExecuteStoredProcedureOp>(); var result = new ExecuteStoredProcedureOp( referenceObject.Name, new ParameterDefinitionBase[0].Concat(referenceObject.Parameters).Concat(new ParameterDefinitionBase[] { null }).Concat(referenceObject.Parameters).ToList()); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "parameters", "contains at least one null element", }, }); }
/// <inheritdoc /> public Task <StoredProcedureExecutionResult> ExecuteAsync( ExecuteStoredProcedureOp operation) { // TODO: mirror fully ASYNC code here throw new System.NotImplementedException(); }