Example #1
0
        /// <summary>
        /// Executes the specified query.
        /// </summary>
        /// <typeparam name="T">Type of model result.</typeparam>
        /// <param name="query">The query.</param>
        /// <returns>
        /// IEnumerable of models, which was materialized by query
        /// </returns>
        /// <exception cref="ArgumentNullException">If query is null.</exception>
        public IEnumerable <T> Execute <T>(IQuery <T> query)
        {
            Check.NotNull(query, nameof(query));

            Data.ConnectionHelper cnHelper = OpenConnection();
            DbCommand             command  = CreateCommand(query.Expression);

            _logger.LogCommand(command);
            IDataReader reader = new ModelBuilder.QueryDataReader(command, cnHelper.CloseConnection);

            return(_modelBuilder.Materialize <T>(reader));
        }
        /// <inheritdoc cref="IQueryProvider.ExecuteStoredProcedure{TResult}(string, CommandParameterCollection)"/>
        public TResult ExecuteStoredProcedure <TResult>(string storedProcedureName, CommandParameterCollection parameters)
        {
            CheckCommandParameters(parameters);

            TResult result = default(TResult);

            Data.ConnectionHelper cnHelper = null;
            DbCommand             command  = null;
            bool callDispose = true;

            try
            {
                cnHelper            = OpenConnection();
                command             = CreateCommand(storedProcedureName, parameters);
                command.CommandType = CommandType.StoredProcedure;
                _logger.LogCommand(command);

                DbParameter returnParameter = GetOrAddCommandReturnParameter(command);

                if (typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
                {
                    IDataReader reader = new ModelBuilder.QueryDataReader(command, null, cnHelper.CloseConnection);
                    result      = MaterializeStoredProcedureResult <TResult>(reader);
                    callDispose = false;
                }
                else
                {
                    result = MaterializeStoredProcedureResult <TResult>(command, returnParameter);
                }
                FillOutputParameters(command, parameters);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (callDispose)
                {
                    command?.Dispose();
                    cnHelper?.Dispose();
                }
            }

            return(result);
        }