Esempio n. 1
0
        /// <summary>
        /// 异步执行查询文本并将结果以一个 <see cref="IEnumerable{T}"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <param name="cancellationToken">取消操作的通知。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public async virtual Task <IEnumerable <T> > ExecuteEnumerableAsync <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null, CancellationToken cancellationToken = default)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            rowMapper = rowMapper ?? RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();

            var reader = await ExecuteReaderAsync(queryCommand, segment, parameters, cancellationToken);

            return(new AsyncEnumerable <T>(this, reader, rowMapper));
        }
Esempio n. 2
0
        /// <summary>
        /// 执行查询文本并将结果以一个 <see cref="IEnumerable{T}"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public virtual IEnumerable <T> ExecuteEnumerable <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            rowMapper ??= RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();
            using var reader        = ExecuteReader(queryCommand, segment, parameters, CommandBehavior.Default);
            while (reader.Read())
            {
                yield return(rowMapper.Map(this, reader));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 异步执行查询文本并将结果以一个 <see cref="IEnumerable&lt;T&gt;"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public async virtual Task <IEnumerable <T> > ExecuteEnumerableAsync <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null)
        {
            Guard.ArgumentNull(queryCommand, "queryCommand");

            var reader = ExecuteReaderAsync(queryCommand, segment, parameters);

            return(await new Task <IEnumerable <T> >(() =>
            {
                var list = new List <T>();
                rowMapper = rowMapper ?? RowMapperFactory.CreateRowMapper <T>();
                rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();
                return new List <T>(new AsyncEnumerable <T>(reader.Result, rowMapper));
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// 执行查询文本并将结果以一个 <see cref="IEnumerable&lt;T&gt;"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public virtual IEnumerable <T> ExecuteEnumerable <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null)
        {
            Guard.ArgumentNull(queryCommand, "queryCommand");

            rowMapper = rowMapper ?? RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();
            using (var reader = ExecuteReader(queryCommand, segment, parameters))
            {
                while (reader.Read())
                {
                    yield return(rowMapper.Map(reader));
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 异步的,执行查询文本并将结果以一个 <see cref="IEnumerable{T}"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <param name="cancellationToken">取消操作的通知。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public async virtual IAsyncEnumerable <T> ExecuteEnumerableAsync <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null, CancellationToken cancellationToken = default)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            rowMapper = rowMapper ?? RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();
            using (var reader = (DbDataReader)(await ExecuteReaderAsync(queryCommand, segment, parameters, cancellationToken)))
            {
                while (await reader.ReadAsync(cancellationToken))
                {
                    yield return(rowMapper.Map(this, reader));
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 异步的,执行查询文本并将结果以一个 <see cref="IEnumerable{T}"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <param name="cancellationToken">取消操作的通知。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public async virtual IAsyncEnumerable <T> ExecuteAsyncEnumerable <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            rowMapper ??= RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();
            using var reader        = (InternalDataReader) await ExecuteReaderAsync(queryCommand, segment, parameters, CommandBehavior.Default, cancellationToken);

            while (await reader.ReadAsync(cancellationToken))
            {
                yield return(rowMapper.Map(this, reader));
            }
        }