Exemplo n.º 1
0
        /// <summary>
        /// Attempts to get the value of the Property called <paramref name="name" /> from the underlying Entity.
        /// <remarks>
        /// Only properties that exist on <see cref="EntityType" /> can be retrieved.
        /// Both modified and unmodified properties can be retrieved.
        /// </remarks>
        /// </summary>
        /// <param name="name">The name of the Property</param>
        /// <param name="value">The value of the Property</param>
        /// <returns>True if the Property was found</returns>
        public bool TryGetPropertyValue(string name, out object value)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (_propertiesThatExist.ContainsKey(name))
            {
                PropertyAccessor <TEntityType> cacheHit = _propertiesThatExist[name];
                value = cacheHit.GetValue(_entity);
                return(true);
            }
            else
            {
                value = null;
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to set the Property called <paramref name="name" /> to the <paramref name="value" /> specified.
        /// <remarks>
        /// Only properties that exist on <see cref="EntityType" /> can be set.
        /// If there is a type mismatch the request will fail.
        /// </remarks>
        /// </summary>
        /// <param name="name">The name of the Property</param>
        /// <param name="value">The new value of the Property</param>
        /// <returns>True if successful</returns>
        public bool TrySetPropertyValue(string name, object value)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (!_propertiesThatExist.ContainsKey(name))
            {
                return(false);
            }

            PropertyAccessor <TEntityType> cacheHit = _propertiesThatExist[name];

            if (value == null && !IsNullable(cacheHit.Property.PropertyType))
            {
                return(false);
            }

            if (value != null)
            {
                if (value is JToken)
                {
                    try {
                        value = JsonConvert.DeserializeObject(value.ToString(), cacheHit.Property.PropertyType);
                    } catch (Exception ex) {
                        Log.Error().Exception(ex).Message("Error deserializing value: {0}", value.ToString()).Report().Write();
                        return(false);
                    }
                }
                else
                {
                    bool isGuid  = cacheHit.Property.PropertyType == typeof(Guid) && value is string;
                    bool isEnum  = cacheHit.Property.PropertyType.IsEnum && value is Int64 && (long)value <= int.MaxValue;
                    bool isInt32 = cacheHit.Property.PropertyType == typeof(int) && value is Int64 && (long)value <= int.MaxValue;

                    if (!cacheHit.Property.PropertyType.IsPrimitive && !isGuid && !isEnum && !cacheHit.Property.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        return(false);
                    }

                    if (isGuid)
                    {
                        value = new Guid((string)value);
                    }
                    if (isInt32)
                    {
                        value = (int)(long)value;
                    }
                    if (isEnum)
                    {
                        value = Enum.Parse(cacheHit.Property.PropertyType, value.ToString());
                    }
                }
            }

            //.Setter.Invoke(_entity, new object[] { value });
            cacheHit.SetValue(_entity, value);
            _changedProperties.Add(name);
            return(true);
        }