コード例 #1
0
ファイル: IMapper.cs プロジェクト: hahn-kev/khahn.common
        private Func <object, object> Converter(ReflectionFieldDefinition reflectionFieldDefinition)
        {
            //will build Lambda Expression to convert value and compile and cache on field defintion
            Delegate del;

            Type t = reflectionFieldDefinition.FieldType;

            if (t == typeof(string))
            {
                return(o =>
                {
                    if (o == null ||
                        o == DBNull.Value)
                    {
                        return null;
                    }
                    return o.ToString();
                });
            }
            if (t == typeof(int))
            {
                return(o => { return Convert.ToInt32(o); });
            }
            return(null);
        }
コード例 #2
0
ファイル: IMapper.cs プロジェクト: hahn-kev/khahn.common
        public T MapItem <T>(ReflectedEntityKey key, EntityFetchExpression fetchExpression, IDataReader dataReader)
            where T : IEntity
        {
            IEntityDefinition definition = _entityDefinitionProvider.GetEntityDefinition(key);
            T entity = (T)definition.GenerateEntity();
            IReadOnlyList <IFieldDefinition> fields = fetchExpression.Fields(_entityDefinitionProvider);

            for (var i = 0; i < fields.Count; i++)
            {
                ReflectionFieldDefinition fieldDefinition = (ReflectionFieldDefinition)fields[i];
                Field field          = fieldDefinition.GetField(entity);
                var   valueConverter = Converter(fieldDefinition);
                field.Value = valueConverter(dataReader.GetValue(i));
            }
            return(default(T));
        }