コード例 #1
0
        /// <summary>
        /// 使用主键值查询返回一个实体。
        /// </summary>
        /// <param name="primaryValues">主键的值。数组的长度必须与实体所定义的主键相匹配。</param>
        /// <param name="func">对实体对象进行修饰的方法。</param>
        /// <returns>一个实体对象。</returns>
        public IEntity First(object[] primaryValues, Func <object, object> func)
        {
            if (primaryValues == null || primaryValues.Length == 0)
            {
                return(null);
            }

            var parameters = new ParameterCollection();
            var context    = CreateContext(parameters);
            var sql        = EntityPersistentQueryBuilder.BuildGetFirstQuery(context, entityType, primaryValues);

            if (sql.IsNullOrEmpty())
            {
                return(null);
            }

            var mapper = RowMapperFactory.CreateMapper(entityType);

            mapper.RecordWrapper = database.Provider.GetService <IRecordWrapper>();
            using (var reader = database.ExecuteReader(sql, parameters: parameters))
            {
                if (reader.Read())
                {
                    return(func(mapper.Map(reader).InitializeEnvironment(environment)) as IEntity);
                }
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// 执行一个查询,返回一个序列。
        /// </summary>
        /// <param name="database"></param>
        /// <param name="elementType"></param>
        /// <param name="queryCommand"></param>
        /// <param name="segment"></param>
        /// <param name="parameters"></param>
        /// <param name="initializer"></param>
        /// <param name="setter"></param>
        /// <returns></returns>
        internal static IEnumerable InternalExecuteEnumerable(this IDatabase database, Type elementType, IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, Action <object> initializer = null, Action <IDataReader, object> setter = null)
        {
            var mapper = RowMapperFactory.CreateMapper(elementType);

            Guard.ArgumentNull(mapper, "mapper");

            mapper.RecordWrapper = database.Provider.GetService <IRecordWrapper>();
            mapper.Initializer   = initializer;

            using (var reader = database.ExecuteReader(queryCommand, segment, parameters))
            {
                while (reader.Read())
                {
                    var item = mapper.Map(reader);
                    setter?.Invoke(reader, item);

                    yield return(item);
                }
            }
        }