コード例 #1
0
        public override object Read(object obj, Type objType, IDatabase redisDatabase, string id, PropertyInfo basePropertyInfo, LimitObject limits = null)
        {
            if (id == null)
            {
                throw new Exception("Id can't be null");
            }
            var redisKey = new RedisKeyObject(objType, id);

            RedisObjectManager.RedisBackup?.RestoreHash(redisDatabase, redisKey);
            var ret = redisDatabase.HashGetAll(redisKey.RedisKey);

            // Attempt to set all given properties
            obj = RedisObjectManager.ConvertToObject(obj, ret);

            // Do we continue here if it is a base system class?
            if (!(obj is IRedisObject))
            {
                return(obj);
            }

            var props = obj.GetType().GetProperties();

            foreach (var prop in props)
            {
                // If value is virtual assume it is lazy
                if (!prop.GetMethod.IsVirtual)
                {
                    continue;
                }
                // Create proxies here
                if (prop.PropertyType.IsSealed)
                {
                    continue;
                }
                if (!prop.PropertyType.IsClass && !prop.PropertyType.IsInterface)
                {
                    continue;
                }
                try
                {
                    // If the target is an IRedisObject we need to get the ID differently
                    string objectKey = null;
                    //RedisValue propKey = new RedisValue();
                    if (prop.PropertyType.GetInterfaces().Any(x => x == typeof(IRedisObject)))
                    {
                        // Try to get the property value from ret
                        RedisValue propKey;
                        if (ret.ToDictionary().TryGetValue(prop.Name, out propKey))
                        {
                            objectKey = propKey.ParseKey();
                            if (prop.GetValue(obj, null) == null)
                            {
                                var pr = RedisObjectManager.GetRedisObjectWithType(redisDatabase, (string)propKey, objectKey);
                                obj.GetType().GetProperty(prop.Name).SetValue(obj, pr);
                            }
                        }
                        else
                        {
                            if (prop.PropertyType.IsInterface)
                            {
                                throw new Exception("Properties of type Interface need to be populated first");
                            }
                            else
                            {
                                object baseObject = prop.GetValue(obj, null) ?? Activator.CreateInstance(prop.PropertyType);
                                var    key        = new RedisKeyObject(baseObject.GetType(), string.Empty);
                                redisDatabase.GenerateId(key, baseObject, null);
                                objectKey = key.Id;

                                if (!(baseObject is IProxyTargetAccessor))
                                {
                                    var pr = RedisObjectManager.RetrieveObjectProxy(prop.PropertyType, objectKey, redisDatabase,
                                                                                    baseObject, obj);
                                    obj.GetType().GetProperty(prop.Name).SetValue(obj, pr);
                                }
                            }
                        }
                    }
                    else
                    {
                        // Here we try to handle NON redisObjects
                        object baseObject = prop.GetValue(obj, null) ?? Activator.CreateInstance(prop.PropertyType);

                        objectKey = id;

                        if (!(baseObject is IProxyTargetAccessor))
                        {
                            var pr = RedisObjectManager.RetrieveObjectProxy(prop.PropertyType, objectKey, redisDatabase,
                                                                            baseObject, obj);
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, pr);
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                }
            }

            return(obj);
        }
コード例 #2
0
        public void TestRedisRetrieveObject()
        {
            var test1 = _redis.RetrieveObjectProxy <TestConvertClassSubSuffix2>("12345", _database);

            test1.subTest.TestInitite.test = "test string - new";

            var test2 = _redis.RetrieveObjectProxy <TestConvertClassSubSuffix2>("12345", _database);

            Debug.WriteLine($"Id {test2.subTest.Id} - Test Value: {test2.subTest.TestInitite.test}");

            test2.subTest = new TestConvertClassSub {
                test = "test string"
            };
            test2.subTest.TestInitite.test = "test string";

            Assert.IsTrue(test2.Id == "12345");
            Assert.IsTrue(test2.subTest.TestInitite.test == "test string");

            Assert.IsTrue(test1 != null);
        }