Example #1
0
        public void CannotGetChangedNestedDeltaPropertyNames()
        {
            dynamic deltaCustomer  = new Delta <CustomerEntity>();
            IDelta  ideltaCustomer = deltaCustomer as IDelta;

            AddressEntity address = new AddressEntity();

            // modify
            address.City          = "Sammamish";
            address.StreetAddress = "23213 NE 15th Ct";

            // modify the nested property
            ideltaCustomer.TrySetPropertyValue("Address", address);
            Assert.Single(ideltaCustomer.GetChangedPropertyNames());
            Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single());
            Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count());

            // read the not nested property back using legacy API
            Assert.True(ideltaCustomer.TryGetPropertyValue("Address", out dynamic deltaAddressEntity));
            Assert.IsAssignableFrom <AddressEntity>(deltaAddressEntity);
            AddressEntity addressEntity = deltaAddressEntity as AddressEntity;

            Assert.Equal("23213 NE 15th Ct", addressEntity.StreetAddress);
            Assert.Equal("Sammamish", addressEntity.City);

            // read the not nested property back using nested API
            Assert.False(deltaCustomer.TryGetNestedPropertyValue("Address", out dynamic deltaNestedAddress));
        }
Example #2
0
        public void CanGetChangedPropertyNames()
        {
            dynamic delta  = new Delta <AddressEntity>();
            IDelta  idelta = delta as IDelta;

            // modify in the way we expect the formatter too.
            idelta.TrySetPropertyValue("City", "Sammamish");
            Assert.Single(idelta.GetChangedPropertyNames());
            Assert.Equal("City", idelta.GetChangedPropertyNames().Single());
            Assert.Equal(4, idelta.GetUnchangedPropertyNames().Count());

            // read the property back
            object city;

            Assert.True(idelta.TryGetPropertyValue("City", out city));
            Assert.Equal("Sammamish", city);

            // modify the way people will through custom code
            delta.StreetAddress = "23213 NE 15th Ct";
            string[] mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Equal(2, mods.Count());
            Assert.Contains("StreetAddress", mods);
            Assert.Contains("City", mods);
            Assert.Equal("23213 NE 15th Ct", delta.StreetAddress);
            Assert.Equal(3, idelta.GetUnchangedPropertyNames().Count());
        }
Example #3
0
        public void CanGetChangedNestedPropertyNames()
        {
            dynamic deltaCustomer  = new Delta <CustomerEntity>();
            IDelta  ideltaCustomer = deltaCustomer as IDelta;

            AddressEntity Address = new AddressEntity();

            Address.ID            = 42;
            Address.StreetAddress = "23213 NE 15th Ct";
            Address.City          = "Sammamish";
            Address.State         = "WA";
            Address.ZipCode       = 98074;

            // modify in the way we expect the formatter too.
            ideltaCustomer.TrySetPropertyValue("Address", Address);
            Assert.Single(ideltaCustomer.GetChangedPropertyNames());
            Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single());
            Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count());

            // read the property back
            Assert.True(ideltaCustomer.TryGetPropertyValue("Address", out object address));
            Assert.Equal(Address, address);

            // read the instance
            CustomerEntity instance = deltaCustomer.GetInstance();

            Assert.Equal(Address, instance.Address);
        }
Example #4
0
        private static object GetProperty(object resource, string propertyName)
        {
            IDelta delta = resource as IDelta;

            if (delta == null)
            {
                return(resource.GetType().GetProperty(propertyName).GetValue(resource, (object[])null));
            }
            object obj;

            delta.TryGetPropertyValue(propertyName, out obj);
            return(obj);
        }
Example #5
0
        public void CanGetChangedNestedDeltaPropertyNames()
        {
            dynamic deltaCustomer  = new Delta <CustomerEntity>();
            IDelta  ideltaCustomer = deltaCustomer as IDelta;

            dynamic deltaAddress  = new Delta <AddressEntity>();
            IDelta  ideltaAddress = deltaAddress as IDelta;

            // modify
            ideltaAddress.TrySetPropertyValue("City", "Sammamish");
            ideltaAddress.TrySetPropertyValue("StreetAddress", "23213 NE 15th Ct");
            Assert.Equal(3, ideltaAddress.GetUnchangedPropertyNames().Count());
            string[] mods = ideltaAddress.GetChangedPropertyNames().ToArray();
            Assert.Equal(2, mods.Length);
            Assert.Contains("StreetAddress", mods);
            Assert.Contains("City", mods);
            Assert.Equal("23213 NE 15th Ct", deltaAddress.StreetAddress);
            Assert.Equal("Sammamish", deltaAddress.City);

            // read the property back
            Assert.True(ideltaAddress.TryGetPropertyValue("City", out var city));
            Assert.Equal("Sammamish", city);
            Assert.True(ideltaAddress.TryGetPropertyValue("StreetAddress", out var streetAddress));
            Assert.Equal("23213 NE 15th Ct", streetAddress);

            // modify the nested property
            ideltaCustomer.TrySetPropertyValue("Address", ideltaAddress);
            Assert.Single(ideltaCustomer.GetChangedPropertyNames());
            Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single());
            Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count());

            // read the nested property back
            Assert.True(ideltaCustomer.TryGetPropertyValue("Address", out dynamic nestedAddress));
            Assert.IsAssignableFrom <Delta <AddressEntity> >(nestedAddress);
            Assert.Equal("Sammamish", nestedAddress.City);
            Assert.Equal("23213 NE 15th Ct", nestedAddress.StreetAddress);
        }
Example #6
0
        private static object GetProperty(object resource, string propertyName)
        {
            IDelta delta = resource as IDelta;

            if (delta != null)
            {
                object value;
                delta.TryGetPropertyValue(propertyName, out value);
                return(value);
            }
            else
            {
                PropertyInfo property = resource.GetType().GetProperty(propertyName);
                Contract.Assert(property != null, "ODataLib should have already verified that the property exists on the type.");
                return(property.GetValue(resource, index: null));
            }
        }
Example #7
0
        public static IODataInstanceAnnotationContainer GetAnnotationContainer(PropertyInfo propertyInfo, object resource)
        {
            IDelta delta = resource as IDelta;
            object value;

            if (delta != null)
            {
                delta.TryGetPropertyValue(propertyInfo.Name, out value);
            }
            else
            {
                value = propertyInfo.GetValue(resource);
            }

            IODataInstanceAnnotationContainer instanceAnnotationContainer = value as IODataInstanceAnnotationContainer;

            if (instanceAnnotationContainer == null)
            {
                try
                {
                    if (propertyInfo.PropertyType == typeof(ODataInstanceAnnotationContainer) || propertyInfo.PropertyType == typeof(IODataInstanceAnnotationContainer))
                    {
                        instanceAnnotationContainer = new ODataInstanceAnnotationContainer();
                    }
                    else
                    {
                        instanceAnnotationContainer = Activator.CreateInstance(propertyInfo.PropertyType) as IODataInstanceAnnotationContainer;
                    }

                    if (delta != null)
                    {
                        delta.TrySetPropertyValue(propertyInfo.Name, instanceAnnotationContainer);
                    }
                    else
                    {
                        propertyInfo.SetValue(resource, instanceAnnotationContainer);
                    }
                }
                catch (Exception ex)
                {
                    throw new ODataException(Error.Format(SRResources.CannotCreateInstanceForProperty, propertyInfo.Name), ex);
                }
            }

            return(instanceAnnotationContainer);
        }
Example #8
0
        private void SetDeltaPropertyValue(ODataSerializerContext writeContext, List <ODataProperty> properties, IDelta delta, string propertyName)
        {
            object propertyValue;

            if (delta.TryGetPropertyValue(propertyName, out propertyValue))
            {
                Type propertyType;
                IEdmStructuredTypeReference expectedPropType = null;

                if (propertyValue == null)
                {
                    // We need expected property type only if property value is null, else it will get from the value
                    if (delta.TryGetPropertyType(propertyName, out propertyType))
                    {
                        expectedPropType = writeContext.GetEdmType(propertyValue, propertyType) as IEdmStructuredTypeReference;
                    }
                }

                SetPropertyValue(writeContext, properties, expectedPropType, propertyName, propertyValue);
            }
        }
Example #9
0
        public void CanGetChangedPropertyNamesButOnlyUpdatable()
        {
            dynamic delta  = new Delta <AddressEntity>();
            IDelta  idelta = delta as IDelta;

            // modify in the way we expect the formatter too.
            idelta.TrySetPropertyValue("City", "Sammamish");
            Assert.Single(idelta.GetChangedPropertyNames());
            Assert.Equal("City", idelta.GetChangedPropertyNames().Single());
            Assert.Equal(4, idelta.GetUnchangedPropertyNames().Count());

            // read the property back
            object city;

            Assert.True(idelta.TryGetPropertyValue("City", out city));
            Assert.Equal("Sammamish", city);

            // limit updatable properties
            delta.UpdatableProperties.Clear();
            delta.UpdatableProperties.Add("City");
            delta.UpdatableProperties.Add("StreetAddress");

            // modify the way people will through custom code
            delta.StreetAddress = "23213 NE 15th Ct";
            string[] mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Equal(2, mods.Count());
            Assert.Contains("StreetAddress", mods);
            Assert.Contains("City", mods);
            Assert.Equal("23213 NE 15th Ct", delta.StreetAddress);
            Assert.Empty(idelta.GetUnchangedPropertyNames());

            // try to modify an un-updatable property
            idelta.TrySetPropertyValue("State", "IA");
            mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Equal(2, mods.Count());
            Assert.Contains("StreetAddress", mods);
            Assert.Contains("City", mods);
            Assert.Null(delta.State);
            Assert.Empty(idelta.GetUnchangedPropertyNames());

            // limit a property that has been updated
            delta.UpdatableProperties.Remove("StreetAddress");
            mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Single(mods);
            Assert.Contains("City", mods);
            Assert.Null(delta.State);
            Assert.Empty(idelta.GetUnchangedPropertyNames());

            // enable a property that has not been updated
            delta.UpdatableProperties.Add("State");
            mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Single(mods);
            Assert.Contains("City", mods);
            Assert.Null(delta.State);
            Assert.Single(idelta.GetUnchangedPropertyNames());
            Assert.Equal("State", idelta.GetUnchangedPropertyNames().Single());

            // enable a property that doesn't exist
            delta.UpdatableProperties.Add("Bogus");
            mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Single(mods);
            Assert.Contains("City", mods);
            Assert.Null(delta.State);
            Assert.Single(idelta.GetUnchangedPropertyNames());
            Assert.Equal("State", idelta.GetUnchangedPropertyNames().Single());

            // set a property that doesn't exist
            Assert.False(delta.TrySetPropertyValue("Bogus", "Bad"));
            mods = idelta.GetChangedPropertyNames().ToArray();
            Assert.Single(mods);
            Assert.Contains("City", mods);
            Assert.Null(delta.State);
            Assert.Single(idelta.GetUnchangedPropertyNames());
            Assert.Equal("State", idelta.GetUnchangedPropertyNames().Single());
        }