Exemple #1
0
        public Func <object[], TEntity> GetReader()
        {
            if (cachedReader != null)
            {
                return(cachedReader);
            }
            var factory = lookup.GetFactory <TEntity>();

            if (factory == null)
            {
                factory = codeGenerator.GetFactory <TEntity>();
            }
            var mappings       = lookup.GetMappings();
            var memberMappings = getMemberMappings(mappings);
            var setter         = codeGenerator.GetReader <TEntity>(memberMappings);
            var nullChecker    = getNullChecker(memberMappings);
            var nestedMappers  = getNestedMappers(mappings);

            if (nestedMappers.Any())
            {
                cachedReader = (values) =>
                {
                    var entity = factory();
                    nullChecker(values);
                    setter(entity, values);
                    foreach (var nestedMapper in nestedMappers)
                    {
                        var nestedReader = nestedMapper.GetReader();
                        var result       = nestedReader(values);
                        nestedMapper.Member.SetValue(entity, result);
                    }
                    return(entity);
                };
            }
            else
            {
                cachedReader = (values) =>
                {
                    var entity = factory();
                    nullChecker(values);
                    setter(entity, values);
                    return(entity);
                };
            }
            return(cachedReader);
        }
Exemple #2
0
        public Func <IRecordContext, object[], TEntity> GetReader()
        {
            if (cachedReader != null)
            {
                return(cachedReader);
            }
            var factory        = lookup.GetFactory <TEntity>() ?? codeGenerator.GetFactory <TEntity>();
            var mappings       = lookup.GetMappings();
            var memberMappings = GetReaderMemberMappings(mappings);
            var deserializer   = codeGenerator.GetReader <TEntity>(memberMappings);
            var nullChecker    = GetNullChecker(memberMappings);
            var nestedMappers  = GetNestedMappers(mappings);

            if (nestedMappers.Any())
            {
                cachedReader = (recordContext, values) =>
                {
                    var entity = factory();
                    nullChecker(values);
                    deserializer(recordContext, entity, values);
                    foreach (var nestedMapper in nestedMappers)
                    {
                        var nestedReader = nestedMapper.GetReader();
                        var result       = nestedReader(recordContext, values);
                        nestedMapper.Member.SetValue(entity, result);
                    }
                    return(entity);
                };
            }
            else
            {
                cachedReader = (recordContext, values) =>
                {
                    var entity = factory();
                    nullChecker(values);
                    deserializer(recordContext, entity, values);
                    return(entity);
                };
            }
            return(cachedReader);
        }