Ejemplo n.º 1
0
        internal static IEnumerable <T> ReadExportRecords <T>(Stream stream, bool retrieveRelated) where T : ZObject
        {
            IEnumerable <CsvRow> data = ReadExportData(stream);

            Type         type     = typeof(T);
            string       name     = type.GetName();
            TypeAccessor accessor = ObjectHydrator.GetAccessor(type);
            List <T>     records  = new List <T>();

            Parallel.ForEach(data.GroupBy(x => x[name + ".Id"]), itemData =>
            {
                T item = null;
                foreach (CsvRow row in itemData)
                {
                    if (item == null)
                    {
                        item = ObjectHydrator.ParseItem <T>(type, row, accessor, retrieveRelated);
                    }
                    ObjectHydrator.CombineRelations(item, type, row, accessor);
                }
                records.Add(item);
            });

            return(records);
        }
Ejemplo n.º 2
0
        internal static T ReadSimpleResponse <T>(XmlNode node, XmlDocument document)
        {
            Type         type     = typeof(T);
            bool         ns       = type.BaseType == typeof(ZObject);
            T            obj      = Activator.CreateInstance <T>();
            TypeAccessor accessor = ObjectHydrator.GetAccessor(type);

            foreach (PropertyInfo property in type.GetCachedProperties())
            {
                Type   propertyType = property.PropertyType;
                string name         = property.GetName();
                if (name == "records")
                {
                    // Not sure I like this. I might want to create a new response reader
                    accessor[obj, property.Name] = ReadArrayResponse(Activator.CreateInstance(propertyType), document);
                }
                else
                {
                    if (ns)
                    {
                        name = "ns2:" + name;
                    }
                    string value = node.GetValue(name);

                    if (value == null)
                    {
                        IEnumerable <XElement> nodes = GetNamedNodes(node, name);
                        if (nodes == null || !nodes.Any())
                        {
                            continue;
                        }
                        XElement child = nodes.FirstOrDefault();
                        if (child == null)
                        {
                            continue;
                        }

                        value = child.Value;
                    }

                    ObjectHydrator.SetProperty(obj, value, property, accessor);
                }
            }

            return(obj);
        }