Exemple #1
0
 protected IList <T> QueryObjects <T>(string query, JoinOn joinOn, params object[] arguments)
 {
     using (var conn = CreateOpenConnection())
         using (var selectStatement = CreateCommand(conn, query, arguments))
             using (var dataReader = selectStatement.ExecuteReader())
             {
                 var objects = new List <T>();
                 while (dataReader.Read())
                 {
                     var obj = new ResultSetObjectMapper <T>(dataReader, joinOn).MapResultToType();
                     objects.Add(obj);
                 }
                 return(objects);
             }
 }
        private void MapAssociations(object obj, ISet <string> associationsToMap)
        {
            var mappedCollections = new Dictionary <string, ICollection <object> >();

            while (_dataReader.NextResult())
            {
                foreach (var fieldName in associationsToMap)
                {
                    var associationField     = typeof(T).GetField(fieldName);
                    var associationFieldType = default(Type);
                    var collection           = default(ICollection <object>);

                    if (typeof(IEnumerable).IsAssignableFrom(associationField.FieldType))
                    {
                        if (!mappedCollections.TryGetValue(fieldName, out collection))
                        {
                            collection = CreateCollectionFrom(associationField.FieldType);
                            mappedCollections.Add(fieldName, collection);
                            associationField.SetValue(obj, collection);
                        }

                        var genericType = associationField.FieldType.GetGenericTypeDefinition();
                        associationFieldType = genericType.GetGenericArguments()[0];
                    }
                    else
                    {
                        associationFieldType = associationField.FieldType;
                    }

                    var columnName = FieldNameToColumnName(fieldName);

                    var mapper = new ResultSetObjectMapper <object>(_dataReader, _joinOn, ToObjectPrefix(columnName));

                    var associationObject = mapper.MapResultToType();

                    if (collection != null)
                    {
                        collection.Add(associationObject);
                    }
                    else
                    {
                        associationField.SetValue(obj, associationObject);
                    }
                }
            }
        }