Ejemplo n.º 1
0
        public void SetSuccessResult_ShouldSetExpectedProperties()
        {
            //---------------Set up test pack-------------------
            var actionResults = new List <object>
            {
                new object(), new object()
            };

            var actionResult = new DbContextActionResult <object>();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            actionResult.SetSuccessResult(actionResults);
            //---------------Test Result -----------------------
            actionResult.ActionResult.Should().Be(DbContextActionResult.Success);
            actionResult.Results.Should().BeEquivalentTo(actionResults);
            actionResult.ActionMessage.Should().BeNullOrWhiteSpace();
            actionResult.Exception.Should().BeNull();
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public override async Task <IDbContextActionResult <T> > ExecuteActionAsync <T>(DbContextAction dbContextAction,
                                                                                        T dataModel           = default(T),
                                                                                        string sqlCommandText = null,
                                                                                        IEnumerable <IDataAccessParameter> dataParameters = null)
        {
            var actionResult      = new DbContextActionResult <T>();
            var mapData           = true;
            var wasConnectionOpen = DbConnection?.State == ConnectionState.Open;

            try
            {
                var dbCommand = DbConnection.CreateCommand();
                dbCommand.CommandTimeout = CommandTimeout;
                dbCommand.CommandType    = CommandType.Text;

                switch (dbContextAction)
                {
                case DbContextAction.Retrieve:
                    dbCommand.CommandText = _sqlStatementBuildProvider.BuildSelectStatement(dataModel);
                    break;

                case DbContextAction.Create:
                    dbCommand.CommandText = _sqlStatementBuildProvider.BuildInsertStatement(dataModel);
                    mapData = false;
                    break;

                case DbContextAction.StoredProcedure:
                    dbCommand.CommandType = CommandType.StoredProcedure;
                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(dbContextAction), (int)dbContextAction, typeof(DbContextActionResult));
                }

                await OpenAsync();

                var resultData = new List <T>();
                if (mapData)
                {
                    var dataReader = dbCommand.ExecuteReader();
                    while (dataReader.Read())
                    {
                        var rowData = Enumerable.Range(0, dataReader.FieldCount)
                                      .ToDictionary(i => dataReader.GetName(i), i => dataReader.GetValue(i));

                        resultData.Add((T)await _dataModelPopulateProvider.PopulateAsync(typeof(T), rowData, dbContextAction));
                    }

                    dataReader.Close();
                }
                else
                {
                    dbCommand.ExecuteNonQuery();
                }

                if (!wasConnectionOpen)
                {
                    await CloseAsync();
                }

                actionResult.SetSuccessResult(resultData);
            }
            catch (Exception runtimeException)
            {
                actionResult.SetExceptionResult(runtimeException);
            }

            return(actionResult);
        }