コード例 #1
0
        public void Automatically_migrate_to_new_Schema()
        {
            var repository = new BlogRepository(this._redisClient);

            // Populate the DataStore with the old schema from the 'BlogPostBestPractice'
            BlogPostBestPractice.InsertTestData(repository);

            // Create a typed-client based on the new schema
            var redisBlogPosts = this._redisClient.As <New.BlogPost>();
            // Automatically retrieve blog posts
            var allBlogPosts = redisBlogPosts.GetAll();

            // Print out the data in the list of 'New.BlogPost' populated from old 'BlogPost' type
            // Note: renamed fields are lost
            Console.WriteLine(allBlogPosts.ToJson());
        }
コード例 #2
0
        public void Manually_migrate_to_new_Schema_using_a_custom_tranlation()
        {
            var repository = new BlogRepository(this._redisClient);

            // Populate the DataStore with the old schema from the 'BlogPostBestPractice'
            BlogPostBestPractice.InsertTestData(repository);

            // Create a typed-client based on the new schema
            var redisBlogPosts    = this._redisClient.As <BlogPost>();
            var redisNewBlogPosts = this._redisClient.As <New.BlogPost>();
            // Automatically retrieve blog posts
            var oldBlogPosts = redisBlogPosts.GetAll();

            // Write a custom translation layer to migrate to the new schema
            var migratedBlogPosts = oldBlogPosts.Select(old => new New.BlogPost {
                Id       = old.Id,
                BlogId   = old.BlogId,
                Title    = old.Title,
                Content  = old.Content,
                Labels   = old.Categories,       // populate with data from renamed field
                PostType = BlogPostType.Article, // select non-default enum value
                Tags     = new HashSet <string>(old.Tags),
                Comments = old.Comments.ConvertAll(x =>
                                                   new Dictionary <string, string> {
                    { "Content", x.Content },
                    { "CreatedDate", x.CreatedDate.ToString() }
                }),
                NoOfComments = old.Comments.Count // populate using logic from old data
            }).ToList();

            // Persist the new migrated blog posts
            redisNewBlogPosts.StoreAll(migratedBlogPosts);

            // Read out the newly stored blog posts
            var refreshedNewBlogPosts = redisNewBlogPosts.GetAll();

            // Note: data renamed fields are successfully migrated to the new schema
            Console.WriteLine(refreshedNewBlogPosts.ToJson());
        }