コード例 #1
0
        /// <inheritdoc />
        public bool TryGetPropertyValue(string propertyName, out object value)
        {
            // look into the container first to see if it has that property. container would have it
            // if the property was expanded.
            if (Container != null)
            {
                _containerDict = _containerDict ?? Container.ToDictionary(DefaultPropertyMapper, includeAutoSelected: true);
                if (_containerDict.TryGetValue(propertyName, out value))
                {
                    return(true);
                }
            }

            // fall back to the instance.
            if (UseInstanceForProperties && UntypedInstance != null)
            {
                IEdmTypeReference edmTypeReference = GetEdmType();
                if (edmTypeReference is IEdmComplexTypeReference)
                {
                    _typedEdmStructuredObject = _typedEdmStructuredObject ??
                                                new TypedEdmComplexObject(UntypedInstance, edmTypeReference as IEdmComplexTypeReference, Model);
                }
                else
                {
                    _typedEdmStructuredObject = _typedEdmStructuredObject ??
                                                new TypedEdmEntityObject(UntypedInstance, edmTypeReference as IEdmEntityTypeReference, Model);
                }

                return(_typedEdmStructuredObject.TryGetPropertyValue(propertyName, out value));
            }

            value = null;
            return(false);
        }
コード例 #2
0
        public void GetPropertyGetter_CreatesGetterPerType()
        {
            // Arrange
            IEdmModel model;
            IEdmEntityTypeReference reference;

            GetModel(out model, out reference);
            string propertyName = "Alias";

            // Act
            Func <object, object> testEntityGetter = TypedEdmStructuredObject.GetOrCreatePropertyGetter(
                typeof(TestEntity),
                propertyName,
                reference,
                model);

            Func <object, object> proxyEntityGetter = TypedEdmStructuredObject.GetOrCreatePropertyGetter(
                typeof(TestEntityGeneratedProxy),
                propertyName,
                reference,
                model);

            // Assert
            Assert.NotSame(testEntityGetter, proxyEntityGetter);
        }
コード例 #3
0
        public void GetPropertyGetter_Caches_PropertyGetter()
        {
            // Arrange
            Mock <IEdmStructuredTypeReference> mockEdmTypeReference1 = new Mock <IEdmStructuredTypeReference>();
            Mock <IEdmStructuredTypeReference> mockEdmTypeReference2 = new Mock <IEdmStructuredTypeReference>();
            Mock <IEdmStructuredType>          mockEdmType           = new Mock <IEdmStructuredType>();
            Mock <IEdmProperty> mockProperty = new Mock <IEdmProperty>();

            mockEdmType.Setup(t => t.FindProperty("Property")).Returns(mockProperty.Object);
            mockEdmTypeReference1.Setup(t => t.Definition).Returns(mockEdmType.Object);
            mockEdmTypeReference2.Setup(t => t.Definition).Returns(mockEdmType.Object);
            IEdmModel             model   = new Mock <EdmModel>().Object;
            Func <object, object> getter1 = TypedEdmStructuredObject.GetOrCreatePropertyGetter(
                typeof(TestEntity),
                "Property",
                mockEdmTypeReference1.Object,
                model);
            Func <object, object> getter2 = TypedEdmStructuredObject.GetOrCreatePropertyGetter(
                typeof(TestEntity),
                "Property",
                mockEdmTypeReference2.Object,
                model);

            // Act & Assert
            Assert.Same(getter1, getter2);
        }
コード例 #4
0
        private object BuildResourceInstance()
        {
            if (EdmObject == null)
            {
                return(null);
            }

            TypedEdmStructuredObject edmStructruredObject = EdmObject as TypedEdmStructuredObject;

            if (edmStructruredObject != null)
            {
                return(edmStructruredObject.Instance);
            }

            SelectExpandWrapper selectExpandWrapper = EdmObject as SelectExpandWrapper;

            if (selectExpandWrapper != null && selectExpandWrapper.UntypedInstance != null)
            {
                return(selectExpandWrapper.UntypedInstance);
            }

            Type clrType = EdmLibHelpers.GetClrType(StructuredType, EdmModel);

            if (clrType == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.MappingDoesNotContainResourceType, StructuredType.FullTypeName()));
            }

            object resource = Activator.CreateInstance(clrType);

            foreach (IEdmStructuralProperty property in StructuredType.StructuralProperties())
            {
                object value;
                if (EdmObject.TryGetPropertyValue(property.Name, out value) && value != null)
                {
                    if (value is SelectExpandWrapper)
                    {
                        // Skip the select expand property
                        continue;
                    }

                    string propertyName = EdmLibHelpers.GetClrPropertyName(property, EdmModel);

                    if (TypeHelper.IsCollection(value.GetType()))
                    {
                        DeserializationHelpers.SetCollectionProperty(resource, property, value, propertyName);
                    }
                    else
                    {
                        DeserializationHelpers.SetProperty(resource, propertyName, value);
                    }
                }
            }

            return(resource);
        }
コード例 #5
0
        public void GetPropertyGetter_WorksWithAliasedPropertiesAndDynamicProxies()
        {
            // Arrange
            IEdmModel model;
            IEdmEntityTypeReference reference;

            GetModel(out model, out reference);
            string propertyName = "Alias";

            // Act
            Func <object, object> testEntityGetter = TypedEdmStructuredObject.GetOrCreatePropertyGetter(
                typeof(TestEntityGeneratedProxy),
                propertyName,
                reference,
                model);

            // Assert
            Assert.NotNull(testEntityGetter);
        }
コード例 #6
0
        private object BuildResourceInstance()
        {
            if (EdmObject == null)
            {
                return(null);
            }

            TypedEdmStructuredObject edmStructruredObject = EdmObject as TypedEdmStructuredObject;

            if (edmStructruredObject != null)
            {
                return(edmStructruredObject.Instance);
            }

            Type clrType = EdmLibHelpers.GetClrType(StructuredType, EdmModel);

            if (clrType == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.MappingDoesNotContainEntityType, StructuredType.FullTypeName()));
            }

            object resource = Activator.CreateInstance(clrType);

            foreach (IEdmStructuralProperty property in StructuredType.StructuralProperties())
            {
                object value;
                if (EdmObject.TryGetPropertyValue(property.Name, out value) && value != null)
                {
                    if (value.GetType().IsCollection())
                    {
                        DeserializationHelpers.SetCollectionProperty(resource, property, value, property.Name);
                    }
                    else
                    {
                        DeserializationHelpers.SetProperty(resource, property.Name, value);
                    }
                }
            }

            return(resource);
        }