public void ApplyODataDerivedComplexValueForBaseComplexTypeProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
            complexInstance.InnerComplexProperty = new DerivedComplexType {DerivedProp = 1};

            //In a true client, a TypeResolver will be used to resolve derived property type.
            context.ResolveTypeForMaterializationOverrideFunc = (Type type, string name) =>
            {
                if (name == "DerivedComplexType")
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
                    return new ClientTypeAnnotation(edmType, typeof(DerivedComplexType), "DerivedComplexType", context.Model);
                }
                else
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(ComplexTypeWithChildComplexType));
                    return new ClientTypeAnnotation(edmType, typeof(ComplexTypeWithChildComplexType), "ComplexTypeWithChildComplexType", context.Model);
                }
            };

            IEdmType t = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
            ODataProperty property = new ODataProperty() { Name = "InnerComplexProperty", Value = new ODataComplexValue { TypeName = "DerivedComplexType", Properties = new ODataProperty[] { new ODataProperty { Name = "DerivedProp", Value = 1 } } } };

            Action test = () => this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
            test.ShouldNotThrow();
        }
Exemplo n.º 2
0
        public void ApplyDerivedComplexForBaseComplexTypeProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();

            context.ResolveTypeForMaterializationOverrideFunc = (Type type, string name) =>
            {
                if (name == "DerivedComplexType")
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
                    return(new ClientTypeAnnotation(edmType, typeof(DerivedComplexType), "DerivedComplexType", context.Model));
                }
                else
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(ComplexTypeWithChildComplexType));
                    return(new ClientTypeAnnotation(edmType, typeof(ComplexTypeWithChildComplexType), "ComplexTypeWithChildComplexType", context.Model));
                }
            };
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();

            complexInstance.InnerComplexProperty = new DerivedComplexType {
                DerivedProp = 1
            };

            var innerResource = new ODataResource()
            {
                TypeName   = "DerivedComplexType",
                Properties = new ODataProperty[] { new ODataProperty {
                                                       Name = "DerivedProp", Value = 2
                                                   } }
            };

            this.ApplyInnerProperty(innerResource, complexInstance, context);

            Assert.Equal(2, (complexInstance.InnerComplexProperty as DerivedComplexType).DerivedProp);
        }
Exemplo n.º 3
0
        public void NullValueShouldBeAppliedToSubComplexValueProperty()
        {
            TestMaterializerContext         context         = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();

            complexInstance.InnerComplexProperty = new ChildComplexType();

            this.ApplyInnerProperty(null, complexInstance);
            Assert.Null(complexInstance.InnerComplexProperty);
        }
        public void NullValueShouldBeAppliedToSubComplexValueProperty()
        {
            TestMaterializerContext         context         = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();

            complexInstance.InnerComplexProperty = new ChildComplexType();
            ODataProperty property = new ODataProperty()
            {
                Name = "InnerComplexProperty", Value = null
            };

            this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
            Assert.IsNull(complexInstance.InnerComplexProperty);
        }
Exemplo n.º 5
0
 public void ValueShouldBeAppliedRegardlessIfPropertyStartsNullOrNot()
 {
     foreach (var startingPropertyState in new ChildComplexType[] { null, new ChildComplexType() })
     {
         TestMaterializerContext         context         = new TestMaterializerContext();
         ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
         complexInstance.InnerComplexProperty = startingPropertyState;
         var innerEntry = new ODataResource()
         {
             Properties = new ODataProperty[] { new ODataProperty()
                                                {
                                                    Name = "Prop", Value = 1
                                                } }
         };
         this.ApplyInnerProperty(innerEntry, complexInstance);
         complexInstance.InnerComplexProperty.Prop.Should().Be(1);
     }
 }
        public void ValueShouldBeAppliedRegardlessIfPropertyStartsNullOrNot()
        {
            foreach (var startingPropertyState in new ChildComplexType[] { null, new ChildComplexType() })
            {
                TestMaterializerContext         context         = new TestMaterializerContext();
                ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
                complexInstance.InnerComplexProperty = startingPropertyState;
                ODataProperty property = new ODataProperty()
                {
                    Name = "InnerComplexProperty", Value = new ODataComplexValue()
                    {
                        Properties = new ODataProperty[] { new ODataProperty()
                                                           {
                                                               Name = "Prop", Value = 1
                                                           } }
                    }
                };

                this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
                complexInstance.InnerComplexProperty.Prop.Should().Be(1);
            }
        }
        public void ApplyODataDerivedComplexValueForBaseComplexTypeProperty()
        {
            TestMaterializerContext         context         = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();

            complexInstance.InnerComplexProperty = new DerivedComplexType {
                DerivedProp = 1
            };

            //In a true client, a TypeResolver will be used to resolve derived property type.
            context.ResolveTypeForMaterializationOverrideFunc = (Type type, string name) =>
            {
                if (name == "DerivedComplexType")
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
                    return(new ClientTypeAnnotation(edmType, typeof(DerivedComplexType), "DerivedComplexType", context.Model));
                }
                else
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(ComplexTypeWithChildComplexType));
                    return(new ClientTypeAnnotation(edmType, typeof(ComplexTypeWithChildComplexType), "ComplexTypeWithChildComplexType", context.Model));
                }
            };

            IEdmType      t        = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
            ODataProperty property = new ODataProperty()
            {
                Name = "InnerComplexProperty", Value = new ODataComplexValue {
                    TypeName = "DerivedComplexType", Properties = new ODataProperty[] { new ODataProperty {
                                                                                            Name = "DerivedProp", Value = 1
                                                                                        } }
                }
            };

            Action test = () => this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);

            test.ShouldNotThrow();
        }
Exemplo n.º 8
0
        private void ApplyInnerProperty(ODataResource innerResource, ComplexTypeWithChildComplexType parentInstance, TestMaterializerContext context = null)
        {
            context = context ?? new TestMaterializerContext();
            var resource = new ODataResource()
            {
                TypeName = "ComplexTypeWithChildComplexType", Properties = new ODataProperty[0]
            };

            var clientEdmModel    = new ClientEdmModel(ODataProtocolVersion.V4);
            var materializerEntry = MaterializerEntry.CreateEntry(resource, ODataFormat.Json, false, clientEdmModel);

            materializerEntry.ResolvedObject = parentInstance;
            ODataNestedResourceInfo innerComplexP = new ODataNestedResourceInfo()
            {
                Name = "InnerComplexProperty", IsCollection = false
            };

            MaterializerEntry innerMaterializerEntry;

            if (innerResource != null)
            {
                innerMaterializerEntry = MaterializerEntry.CreateEntry(innerResource, ODataFormat.Json, true, clientEdmModel);
            }
            else
            {
                innerMaterializerEntry = MaterializerEntry.CreateEmpty();
            }

            MaterializerNavigationLink.CreateLink(innerComplexP, innerMaterializerEntry);
            materializerEntry.AddNestedResourceInfo(innerComplexP);

            var policy = this.CreateEntryMaterializationPolicy(context);

            policy.EntityTrackingAdapter.TargetInstance = parentInstance;
            policy.Materialize(materializerEntry, typeof(ComplexTypeWithChildComplexType), true);
        }
        public void NullValueShouldBeAppliedToSubComplexValueProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
            complexInstance.InnerComplexProperty = new ChildComplexType();
            ODataProperty property = new ODataProperty() { Name = "InnerComplexProperty", Value = null };

            this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
            Assert.IsNull(complexInstance.InnerComplexProperty);
        }
        public void ValueShouldBeAppliedRegardlessIfPropertyStartsNullOrNot()
        {
            foreach (var startingPropertyState in new ChildComplexType[] {null, new ChildComplexType()})
            {
                TestMaterializerContext context = new TestMaterializerContext();
                ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
                complexInstance.InnerComplexProperty = startingPropertyState;
                ODataProperty property = new ODataProperty() {Name = "InnerComplexProperty", Value = new ODataComplexValue() {Properties = new ODataProperty[] {new ODataProperty() {Name = "Prop", Value = 1}}}};

                this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
                complexInstance.InnerComplexProperty.Prop.Should().Be(1);
            }
        }