Esempio n. 1
0
        private object GetColumnValue <T>(IDataRecord data, string columnName, string propertyName, Type propertyType)
        {
            var    key = TransformKey.Create(typeof(T), propertyName);
            object value;

            if (_args.Transformations.ContainsKey(key))
            {
                //Perform the property transformation if there is one
                value = _args.Transformations[key](data[columnName].ToString());
            }
            else if (_args.TypeTransformations.ContainsKey(propertyType))
            {
                //Perform the type transformation if there is one
                value = _args.TypeTransformations[propertyType](data[columnName].ToString());
            }
            else
            {
                value = data[columnName];
            }

            return(value);
        }
Esempio n. 2
0
        private IEnumerable <object> GetTypeResults <T>(IDataReader data, IEnumerable <string> columns, QueryModel queryModel)
        {
            var results  = new List <object>();
            var fromType = queryModel.MainFromClause.ItemType;
            var props    = fromType.GetProperties();

            if (_args.StrictMapping.Value != StrictMappingType.None)
            {
                this.ConfirmStrictMapping(columns, props, _args.StrictMapping.Value);
            }

            while (data.Read())
            {
                var result = Activator.CreateInstance(fromType);
                foreach (var prop in props)
                {
                    var columnName = (_args.ColumnMappings.ContainsKey(prop.Name)) ?
                                     _args.ColumnMappings[prop.Name] :
                                     prop.Name;
                    if (columns.Contains(columnName))
                    {
                        result.SetProperty(prop.Name, GetColumnValue <T>(data, columnName, prop.Name, prop.PropertyType).Cast(prop.PropertyType));
                    }
                    else if (_args.ForeignKeyTransformations.ContainsKey(TransformKey.Create(typeof(T), prop.Name)))
                    {
                        // foreign key mapping found, extract function and perform mapping
                        var    wkshtAndFunc = _args.ForeignKeyTransformations[TransformKey.Create(typeof(T), prop.Name)];
                        string worksheet    = wkshtAndFunc.Item1;
                        var    func         = wkshtAndFunc.Item2;
                        // get type for second table mapping
                        var propQueryType = prop.PropertyType.GetGenericArguments()[0];
                        // create list and add selected items
                        result.SetListProperty(prop.Name, func(result, _args.SheetDataIncludes[TransformKey.Create(propQueryType, worksheet)]), propQueryType);
                    }
                }
                results.Add(result);
            }
            return(results.AsEnumerable());
        }