示例#1
0
        private void AssertConversionEqual <T>(RedisTypeConverter converter, T value,
                                               RedisValue?expectedRedisValue = null)
        {
            // check from(to(V)) == V (explicit type version)
            var rv1 = converter.GetToRedisValueFunc <T>()(value);
            var nv1 = converter.GetFromRedisValueFunc <T>()(rv1);

            Assert.Equal(value, nv1);

            // check from(to(V)) == V (object type version)
            var rv2 = converter.GetToRedisValueFunc(typeof(T))(value);
            var nv2 = converter.GetFromRedisValueFunc(typeof(T))(rv2);

            Assert.Equal(value, nv2);

            // check to1(V) == to2(V)
            Assert.Equal(rv1, rv2);

            if (expectedRedisValue != null)
            {
                Assert.Equal(expectedRedisValue.Value, rv1);
            }
        }
        public TrackableDictionaryRedisMapper(RedisTypeConverter typeConverter = null)
        {
            if (typeConverter == null)
            {
                typeConverter = RedisTypeConverter.Instance;
            }

            _keyToRedisValue = typeConverter.GetToRedisValueFunc <TKey>();
            _redisValueToKey = typeConverter.GetFromRedisValueFunc <TKey>();

            if (_keyToRedisValue == null || _redisValueToKey == null)
            {
                throw new ArgumentException("Cannot find type converter. Type=" + typeof(TKey).Name);
            }

            _valueToRedisValue = typeConverter.GetToRedisValueFunc <TValue>();
            _redisValueToValue = typeConverter.GetFromRedisValueFunc <TValue>();

            if (_valueToRedisValue == null || _redisValueToValue == null)
            {
                throw new ArgumentException("Cannot find type converter. Type=" + typeof(TValue).Name);
            }
        }
        public TrackableListRedisMapper(RedisTypeConverter typeConverter = null)
        {
            if (typeConverter == null)
            {
                typeConverter = RedisTypeConverter.Instance;
            }

            _valueToRedisValue = typeConverter.GetToRedisValueFunc <T>();
            _redisValueToValue = typeConverter.GetFromRedisValueFunc <T>();

            if (_valueToRedisValue == null || _redisValueToValue == null)
            {
                throw new ArgumentException("Cannot find type converter. Type=" + typeof(T).Name);
            }
        }
        private static PropertyItem[] ConstructPropertyItems(RedisTypeConverter typeConverter)
        {
            var trackerType = TrackerResolver.GetDefaultTracker(typeof(T));

            var items = new List <PropertyItem>();

            foreach (var property in typeof(T).GetProperties())
            {
                var fieldName = property.Name;

                var attr = property.GetCustomAttribute <TrackablePropertyAttribute>();
                if (attr != null)
                {
                    if (attr["redis.ignore"] != null)
                    {
                        continue;
                    }
                    fieldName = attr["redis.field:"] ?? fieldName;
                }

                var item = new PropertyItem
                {
                    Name                  = property.Name,
                    FieldName             = fieldName,
                    PropertyInfo          = property,
                    TrackerPropertyInfo   = trackerType.GetProperty(property.Name + "Tracker"),
                    ConvertToRedisValue   = typeConverter.GetToRedisValueFunc(property.PropertyType),
                    ConvertFromRedisValue = typeConverter.GetFromRedisValueFunc(property.PropertyType),
                };

                if (item.TrackerPropertyInfo == null)
                {
                    throw new ArgumentException($"Cannot find tracker type of '{property.Name}'");
                }

                if (item.ConvertToRedisValue == null || item.ConvertFromRedisValue == null)
                {
                    throw new ArgumentException("Cannot find type converter. Property=" + property.Name);
                }

                items.Add(item);
            }
            return(items.ToArray());
        }