Ejemplo n.º 1
0
        public void CreateResource_CreateDeltaWith_OpenComplexType()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.ComplexType <SimpleOpenAddress>();
            IEdmModel model = builder.GetEdmModel();
            IEdmComplexTypeReference addressTypeReference = model.GetEdmTypeReference(typeof(SimpleOpenAddress)).AsComplex();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(Delta <SimpleOpenAddress>)
            };
            var structuralProperties = addressTypeReference.StructuralProperties().Select(p => p.Name);

            // Act
            Delta <SimpleOpenAddress> resource =
                ODataComplexTypeDeserializer.CreateResource(addressTypeReference, readContext) as
                Delta <SimpleOpenAddress>;

            // Assert
            Assert.NotNull(resource);
            Assert.Equal(structuralProperties, resource.GetUnchangedPropertyNames());
        }
Ejemplo n.º 2
0
        public void ComplexType_reference_extensions()
        {
            IEdmModel       edmModel           = this.GetEdmModel();
            IEdmComplexType derivedComplexType = edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.BaseType != null);
            IEdmComplexType baseComplexType    = derivedComplexType.BaseComplexType();

            Assert.IsNotNull(baseComplexType, "Base complex type should not be null!");

            IEdmComplexTypeReference derivedComplexTypeRef = (IEdmComplexTypeReference)derivedComplexType.ToTypeReference();

            Assert.AreEqual(baseComplexType, derivedComplexTypeRef.BaseComplexType(), "ComplexTypeReference.BaseComplexType()");
            Assert.AreEqual(baseComplexType, derivedComplexTypeRef.BaseType(), "ComplexTypeReference.BaseType()");

            Assert.AreEqual(derivedComplexType.IsAbstract, derivedComplexTypeRef.IsAbstract(), "StructuralTypeReference.IsAbstract()");
            Assert.AreEqual(derivedComplexType.IsOpen, derivedComplexTypeRef.IsOpen(), "StructuralTypeReference.IsOpen()");

            Assert.AreEqual(derivedComplexType.DeclaredStructuralProperties().Count(), derivedComplexTypeRef.DeclaredStructuralProperties().Count(), "StructuralTypeReference.DeclaredStructuralProperties()");
            Assert.AreEqual(derivedComplexType.StructuralProperties().Count(), derivedComplexTypeRef.StructuralProperties().Count(), "StructuralTypeReference.StructuralProperties()");
        }
Ejemplo n.º 3
0
        public void CreateResource_CreatesDeltaWith_ExpectedUpdatableProperties()
        {
            // Arrange
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(Delta <ODataEntityDeserializerTests.Address>)
            };
            var structuralProperties = _addressEdmType.StructuralProperties().Select(p => p.Name);

            // Act
            Delta <ODataEntityDeserializerTests.Address> resource =
                ODataComplexTypeDeserializer.CreateResource(_addressEdmType, readContext) as
                Delta <ODataEntityDeserializerTests.Address>;

            // Assert
            Assert.NotNull(resource);
            Assert.Equal(structuralProperties, resource.GetUnchangedPropertyNames());
        }
        internal static object CreateResource(IEdmComplexTypeReference complexType, ODataDeserializerContext readContext)
        {
            if (complexType == null)
            {
                throw Error.ArgumentNull("complexType");
            }
            if (readContext == null)
            {
                throw Error.ArgumentNull("readContext");
            }

            IEdmModel model = readContext.Model;
            if (model == null)
            {
                throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext);
            }

            if (readContext.IsUntyped)
            {
                return new EdmComplexObject(complexType);
            }
            else
            {
                Type clrType = EdmLibHelpers.GetClrType(complexType, readContext.Model);
                if (clrType == null)
                {
                    throw Error.InvalidOperation(SRResources.MappingDoesNotContainEntityType, complexType.FullName());
                }

                if (readContext.IsDeltaOfT)
                {
                    Type elementType = readContext.ResourceType.GetGenericArguments()[0];
                    if (elementType != clrType)
                    {
                        // Just create the object for inline complex type
                        return Activator.CreateInstance(clrType);
                    }

                    IEnumerable<string> structuralProperties = complexType.StructuralProperties()
                        .Select(edmProperty => EdmLibHelpers.GetClrPropertyName(edmProperty, model));

                    if (complexType.IsOpen())
                    {
                        PropertyInfo dynamicDictionaryPropertyInfo = EdmLibHelpers.GetDynamicPropertyDictionary(
                            complexType.StructuredDefinition(), model);

                        return Activator.CreateInstance(readContext.ResourceType, clrType, structuralProperties,
                            dynamicDictionaryPropertyInfo);
                    }
                    else
                    {
                        return Activator.CreateInstance(readContext.ResourceType, clrType, structuralProperties);
                    }
                }
                else
                {
                    return Activator.CreateInstance(clrType);
                }
            }
        }
Ejemplo n.º 5
0
        internal static object CreateResource(IEdmComplexTypeReference complexType, ODataDeserializerContext readContext)
        {
            if (complexType == null)
            {
                throw Error.ArgumentNull("complexType");
            }
            if (readContext == null)
            {
                throw Error.ArgumentNull("readContext");
            }

            IEdmModel model = readContext.Model;

            if (model == null)
            {
                throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext);
            }

            if (readContext.IsUntyped)
            {
                return(new EdmComplexObject(complexType));
            }
            else
            {
                Type clrType = EdmLibHelpers.GetClrType(complexType, readContext.Model);
                if (clrType == null)
                {
                    throw Error.InvalidOperation(SRResources.MappingDoesNotContainEntityType, complexType.FullName());
                }

                if (readContext.IsDeltaOfT)
                {
                    Type elementType = readContext.ResourceType.GetGenericArguments()[0];
                    if (elementType != clrType)
                    {
                        // Just create the object for inline complex type
                        return(Activator.CreateInstance(clrType));
                    }

                    IEnumerable <string> structuralProperties = complexType.StructuralProperties()
                                                                .Select(edmProperty => EdmLibHelpers.GetClrPropertyName(edmProperty, model));

                    if (complexType.IsOpen())
                    {
                        PropertyInfo dynamicDictionaryPropertyInfo = EdmLibHelpers.GetDynamicPropertyDictionary(
                            complexType.StructuredDefinition(), model);

                        return(Activator.CreateInstance(readContext.ResourceType, clrType, structuralProperties,
                                                        dynamicDictionaryPropertyInfo));
                    }
                    else
                    {
                        return(Activator.CreateInstance(readContext.ResourceType, clrType, structuralProperties));
                    }
                }
                else
                {
                    return(Activator.CreateInstance(clrType));
                }
            }
        }