Ejemplo n.º 1
0
        /// <summary>
        /// Maps object into HashEntry values array
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private HashEntry[] Map(T obj)
        {
            var values = new List <HashEntry>();

            foreach (var propertyName in ObjectPropertyNames)
            {
                var objectProperty = obj.GetType().GetProperties().First(pi => pi.Name == propertyName);
                if (objectProperty == null)
                {
                    throw new Exception($"Couldn't get object property named {propertyName}");
                }

                string value = String.Empty; // default value

                if (objectProperty.GetCustomAttributes <SerializableRedisPropertyAttribute>(false).Any())
                {
                    var propertyValue = objectProperty.GetValue(obj, null);
                    // avoid persisting "" value as a json object, issues deserializing
                    if (propertyValue != null)
                    {
                        value = Serializer.Serialize(objectProperty.GetValue(obj, null) ?? String.Empty);
                    }
                }
                else
                {
                    value = (objectProperty.GetValue(obj, null) ?? String.Empty).ToString();
                }

                values.Add(new HashEntry(propertyName, value));
            }
            return(values.ToArray());
        }
Ejemplo n.º 2
0
        public object this[Expression <Func <T, object> > key]
        {
            get
            {
                if (ContainsKey(key.GetPropertyName()))
                {
                    return(base[key.GetPropertyName()]);
                }

                return(null);
            }

            set
            {
                var isSerializable = key.IsPropertySerializable();
                if (isSerializable)
                {
                    base[key.GetPropertyName()] = value == null ? String.Empty : Serializer.Serialize(value);
                }
                else
                {
                    base[key.GetPropertyName()] = value;
                }
            }
        }