Esempio n. 1
0
            public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto)
            {
                if (context.EntityEntry == null)
                {
                    dto = null;
                    return(false);
                }

                dto = new DynamicObject(context.Object.GetType());
                context.AddToCache(dto);

                dto.Add(EntityType, context.EntityEntry.EntityType.DisplayName());

                if (context.EntityEntry.EntityState != EntityState.Detached)
                {
                    dto.Add(
                        EntityLoadedNavigations,
                        context.MapToDynamicObjectGraph(
                            context.EntityEntry.EntityType.GetNavigations()
                            .Where(n => context.EntityEntry.IsLoaded(n))
                            .Select(n => n.Name)
                            .ToList()));
                }

                foreach (MemberEntry prop in context.EntityEntry.ToEntityEntry().Members)
                {
                    DynamicObject value = context.MapToDynamicObjectGraph(
                        Utils.ConvertToProvider(prop.CurrentValue, prop.Metadata as IProperty));
                    dto.Add(prop.Metadata.Name, value);
                }

                return(true);
            }
Esempio n. 2
0
        public InfoCarrierQueryResultMapperTest(NorthwindQueryInfoCarrierFixture <NoopModelCustomizer> fixture)
        {
            this.context = fixture.CreateContext();

            this.queryResultMapper = new InfoCarrierQueryResultMapper(
                this.context.GetService <IQueryContextFactory>().Create(),
                new Aqua.TypeSystem.TypeResolver(),
                new Aqua.TypeSystem.TypeInfoProvider());

            IEnumerable <DynamicObject> Yield(params DynamicObject[] dynamicObjects) => dynamicObjects;

            // Mapped array
            {
                var obj         = new[] { 1, 2, 3 };
                var array       = obj.Select(x => new DynamicObject(x)).ToArray();
                var mappedArray = new DynamicObject();
                mappedArray.Add("ArrayType", new Aqua.TypeSystem.TypeInfo(obj.GetType(), includePropertyInfos: false));
                mappedArray.Add("Elements", array);
                this.arrayDto = Yield(mappedArray);
            }

            // Mapped list
            {
                var enumerable = new List <int> {
                    1, 2, 3
                };
                var mappedEnumerable = new DynamicObject(typeof(IEnumerable <int>));
                mappedEnumerable.Add(
                    "Elements",
                    new DynamicObject(enumerable.Select(x => new DynamicObject(x)).ToList()));
                this.listDto = Yield(mappedEnumerable);
            }

            // Mapped grouping
            {
                var key      = "hello";
                var elements = new List <int> {
                    1, 2, 3
                };
                var mappedGrouping = new DynamicObject(typeof(IGrouping <string, int>));
                mappedGrouping.Add("Key", new DynamicObject(key));
                mappedGrouping.Add(
                    "Elements",
                    new DynamicObject(elements.Select(x => new DynamicObject(x)).ToList()));
                this.groupingDto = Yield(mappedGrouping);
            }

            // Mapped product entity
            {
                var mappedProduct = new DynamicObject(typeof(Product));
                mappedProduct.Add("__EntityType", "Product");
                mappedProduct.Add("__EntityLoadedNavigations", new DynamicObject(new List <string>()));
                mappedProduct.Add("ProductID", new DynamicObject(1));
                mappedProduct.Add("ProductName", new DynamicObject("Potato"));
                mappedProduct.Add("Discontinued", new DynamicObject(false));
                mappedProduct.Add("UnitsInStock", new DynamicObject(default(ushort)));
                this.productDto = Yield(mappedProduct);
            }
        }
Esempio n. 3
0
            private DynamicObject MapGrouping <TKey, TElement>(IGrouping <TKey, TElement> grouping, Func <Type, bool> setTypeInformation)
            {
                var mappedGrouping = new DynamicObject(grouping.GetType());

                mappedGrouping.Add("Key", MapToDynamicObjectGraph(grouping.Key, setTypeInformation));
                mappedGrouping.Add("Elements", MapCollection(grouping, setTypeInformation).ToArray());
                return(mappedGrouping);
            }
Esempio n. 4
0
            public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto)
            {
                var groupingType = Utils.GetGenericTypeImplementations(context.Object.GetType(), typeof(IGrouping <,>)).SingleOrDefault();

                if (groupingType == null)
                {
                    dto = null;
                    return(false);
                }

                dto = new DynamicObject(groupingType);
                context.AddToCache(dto);
                dto.Add(Key, context.MapToDynamicObjectGraph(groupingType.GetProperty(Key)?.GetValue(context.Object)));
                dto.Add(
                    Elements,
                    new DynamicObject(((IEnumerable)context.Object).Cast <object>().Select(context.MapToDynamicObjectGraph).ToList()));
                return(true);
            }
Esempio n. 5
0
            public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto)
            {
                Type objType = context.Object.GetType();

                if (!objType.IsArray)
                {
                    dto = null;
                    return(false);
                }

                dto = new DynamicObject();
                context.AddToCache(dto);
                dto.Add(ArrayType, new TypeInfo(objType, includePropertyInfos: false));
                dto.Add(
                    Elements,
                    ((IEnumerable)context.Object).Cast <object>().Select(context.MapToDynamicObjectGraph).ToArray());
                return(true);
            }
Esempio n. 6
0
        private DynamicObject CreateDynamicObject(int propertiesPerLevel, int totalLevels, int leafSizeInBytes)
        {
            DynamicObject result = new DynamicObject();

            for (int i = 0; i < propertiesPerLevel; i++)
            {
                string name = $"Property{i}";

                if (totalLevels > 0)
                {
                    result.Add(name, CreateDynamicObject(propertiesPerLevel, totalLevels - 1, leafSizeInBytes));
                }
                else
                {
                    byte[] buffer = new byte[leafSizeInBytes];
                    new Random().NextBytes(buffer);
                    result.Add(name, buffer);
                }
            }

            return(result);
        }
        public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto)
        {
            if (!(context.Object is IGeometry geometry))
            {
                dto = null;
                return(false);
            }

            dto = new DynamicObject(typeof(IGeometry));
            context.AddToCache(dto);

            dto.Add(Data, new GeoJsonWriter().Write(geometry));
            return(true);
        }
        public virtual object VisitAnonymousObject(AnonymousObjectExpression node)
        {
            var members = node.Members;
            var obj     = new DynamicObject(members.Count);

            for (int index = 0; index < members.Count; index++)
            {
                AnonymousObjectMember item = members[index];
                var value = item.Expression.Accept(this);
                obj.Add(item.Name, value);
            }
            node.Type = typeof(DynamicObject);
            return(obj);
        }
            public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto)
            {
                Type objType = context.Object.GetType();

                if (objType == typeof(string))
                {
                    dto = null;
                    return(false);
                }

                Type elementType = Utils.TryGetSequenceType(objType);

                if (elementType == null || elementType == typeof(DynamicObject))
                {
                    dto = null;
                    return(false);
                }

                dto = new DynamicObject(typeof(IEnumerable <>).MakeGenericType(elementType));
                context.AddToCache(dto);
                dto.Add(
                    Elements,
                    new DynamicObject(((IEnumerable)context.Object).Cast <object>().Select(context.MapToDynamicObjectGraph).ToList()));

                if (objType != typeof(List <>).MakeGenericType(elementType))
                {
                    bool hasDefaultCtor = objType.GetTypeInfo().DeclaredConstructors
                                          .Any(c => !c.IsStatic && c.IsPublic && c.GetParameters().Length == 0);
                    if (hasDefaultCtor)
                    {
                        dto.Add(CollectionType, new TypeInfo(objType, includePropertyInfos: false));
                    }
                }

                return(true);
            }
Esempio n. 10
0
        /// <summary>
        /// Retrieves object members type by using <see cref="FormatterServices" /> and populates dynamic object
        /// </summary>
        private void MapObjectMembers(object from, DynamicObject to, Func<Type, bool> setTypeInformation)
        {
            var type = _resolveType(to.Type);

            var customPropertySet = GetPropertiesForMapping(type);
            var customPropertyNames = ReferenceEquals(null, customPropertySet) ? null : customPropertySet.ToDictionary(x => x.Name);
            var members = FormatterServices.GetSerializableMembers(type);
            var values = FormatterServices.GetObjectData(from, members);
            for (int i = 0; i < members.Length; i++)
            {
                var memberName = GetCleanMemberName(members[i]);
                if (!ReferenceEquals(null, customPropertyNames) && !customPropertyNames.ContainsKey(memberName))
                {
                    continue;
                }

                var value = MapToDynamicObjectIfRequired(values[i], setTypeInformation);
                to.Add(memberName, value);
            }
        }