Exemplo n.º 1
0
        public void CannotSetNestedDeltaPropertyBadName()
        {
            dynamic deltaCustomer  = new Delta <CustomerEntity>();
            IDelta  ideltaCustomer = deltaCustomer as IDelta;

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

            // Nested Delta with bad name
            Assert.False(ideltaCustomer.TrySetPropertyValue("Bogus", ideltaAddress));

            // Nested Delta with good name, but not updatable
            deltaCustomer.UpdatableProperties.Clear();
            Assert.False(ideltaCustomer.TrySetPropertyValue("Address", ideltaAddress));
        }
Exemplo n.º 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());
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void CannotSetNestedDeltaPropertyNameTwice()
        {
            dynamic deltaCustomer  = new Delta <CustomerEntity>();
            IDelta  ideltaCustomer = deltaCustomer as IDelta;

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

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

            // modify again
            Assert.False(ideltaCustomer.TrySetPropertyValue("Address", ideltaAddress));
        }
Exemplo n.º 6
0
        internal static void SetProperty(object resource, string propertyName, object value)
        {
            IDelta delta = resource as IDelta;

            if (delta == null)
            {
                resource.GetType().GetProperty(propertyName).SetValue(resource, value, index: null);
            }
            else
            {
                delta.TrySetPropertyValue(propertyName, value);
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        internal static void SetDynamicProperty(object resource, string propertyName, object value,
                                                IEdmStructuredType structuredType, ODataDeserializerContext readContext)
        {
            IDelta delta = resource as IDelta;

            if (delta != null)
            {
                delta.TrySetPropertyValue(propertyName, value);
            }
            else
            {
                PropertyInfo propertyInfo = EdmLibHelpers.GetDynamicPropertyDictionary(structuredType,
                                                                                       readContext.Model);
                if (propertyInfo == null)
                {
                    return;
                }

                IDictionary <string, object> dynamicPropertyDictionary;
                object dynamicDictionaryObject = propertyInfo.GetValue(resource);
                if (dynamicDictionaryObject == null)
                {
                    if (!propertyInfo.CanWrite)
                    {
                        throw Error.InvalidOperation(SRResources.CannotSetDynamicPropertyDictionary, propertyName,
                                                     resource.GetType().FullName);
                    }

                    dynamicPropertyDictionary = new Dictionary <string, object>();
                    propertyInfo.SetValue(resource, dynamicPropertyDictionary);
                }
                else
                {
                    dynamicPropertyDictionary = (IDictionary <string, object>)dynamicDictionaryObject;
                }

                if (dynamicPropertyDictionary.ContainsKey(propertyName))
                {
                    throw Error.InvalidOperation(SRResources.DuplicateDynamicPropertyNameFound,
                                                 propertyName, structuredType.FullTypeName());
                }

                dynamicPropertyDictionary.Add(propertyName, value);
            }
        }
Exemplo n.º 10
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());
        }