Ejemplo n.º 1
0
        /// <summary>
        /// Either read all items or use a using block to ensure all
        /// resources are fully cleaned up.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static async Task <IManagedEnumerable <T> > AsEnumerableAsync <T>(
            this IDbCmd source) where T : new()
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            source.CommandBehavior(CmdBehavior.SingleResult);
            var cmd = await source.CreateAndOpenAsync().ConfigureAwait(false);

            var reader = await cmd.ExecuteReaderAsync(source).ConfigureAwait(false);

            return(new ManagedEnumerable <T>(source, cmd, reader));
        }
Ejemplo n.º 2
0
        public static async Task <int> ExecuteNonQueryAsync(
            this IDbCmd source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            using (var cmd = await source.CreateAndOpenAsync().ConfigureAwait(false))
            {
                return(source.CancellationToken.HasValue
                    ? await cmd.ExecuteNonQueryAsync(source.CancellationToken.Value).ConfigureAwait(false)
                    : await cmd.ExecuteNonQueryAsync().ConfigureAwait(false));
            }
        }
Ejemplo n.º 3
0
        public static async Task <object> ExecuteScalarAsync(
            this IDbCmd source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            using (var cmd = await source.CreateAndOpenAsync().ConfigureAwait(false))
            {
                var obj = source.CancellationToken.HasValue
                    ? await cmd.ExecuteScalarAsync(source.CancellationToken.Value).ConfigureAwait(false)
                    : await cmd.ExecuteScalarAsync().ConfigureAwait(false);

                return(DBNull.Value.Equals(obj) ? null : obj);
            }
        }
Ejemplo n.º 4
0
        public static async Task <T> ExecuteAsync <T>(
            this IDbCmd source,
            Func <DbCommand, T> func)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            using (var dbCmd = await source.CreateAndOpenAsync().ConfigureAwait(false))
            {
                return(await source.CreateTask(func, dbCmd).ConfigureAwait(false));
            }
        }
Ejemplo n.º 5
0
        public static async void ExecuteAsync(
            this IDbCmd source,
            Action <DbCommand> action)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using (var dbCmd = await source.CreateAndOpenAsync().ConfigureAwait(false))
            {
                action(dbCmd);
            }
        }