使用 cosmosdb sql sdk 先不使用 TTL 再使用 TTL 创建 cosmos 集合的最佳方法

问题描述

我们在 PRODUCTION 上的应用程序使用存储库类来创建数据库,如果不存在集合,然后保存和查询文档。这个课程在 PRODUCTION 上按预期工作,没有问题。请注意,在当前的 PROD 数据库中,我们有大约 15 个不同的集合,并且这些模型中的任何一个都未启用 TTL,因为没有业务要求。

现在我们想在同一个数据库中创建几个全新的集合,但通过将 -1 指定为 TTL 来启用 TTL 并保存具有各自 TTL 值的文档。为了实现这一点,可以修改下面的类,但它会破坏 OCP/SOLID 原则,并且在 LIVE 应用程序中这样做是有风险的

因此希望使用继承或组合扩展以下类,以便现有功能不会中断,并且我们能够支持在新集合及其相应文档上启用 TTL 的新要求。任何想法或建议以最好的方式做到这一点?请

          public class Repository<T> : IRepository<T>  where T : new()
                {
                    private readonly string _collectionName;
                    private readonly string _dbname;
                    private readonly RepositoryConfig _config;
                    private readonly AsyncLazy<IDocumentClient> _client;
                    private readonly AsyncLazy<DocumentCollection> _collection;
                    private readonly AsyncLazy<Database> _database;
                    public Repository()
                    {
                        _collectionName = typeof(T).Name.ToLowerInvariant();
                        _dbname = "DatabaseName";

                        _client = new AsyncLazy<IDocumentClient>(() =>
                        {
                            return new DocumentClient(new Uri("Uri"),"id");

                        });

                        _database = new AsyncLazy<Database>(async () =>
                        {
                            var database = await _client.CreateDatabaseQuery()
                                .Where(db => db.Id == _dbname).AsEnumerable().FirstOrDefault();

                            if (database == null)
                            {
                                var newDatabase = new Database { Id = _dbname };
                                database = await _client.CreateDatabaseAsync(newDatabase);
                            }

                            return database;
                        });

                        _collection = new AsyncLazy<DocumentCollection>(async () =>
                        {
                            var docclient = await _client;
                            var docDB = await _database;
                            var documentCollection = docclient.CreateDocumentCollectionQuery(docDB.SelfLink)
                                .Where(a => a.Id == _collectionName).AsEnumerable().FirstOrDefault();
                            if (documentCollection == null)
                            {
                                DocumentCollection newCollection = new DocumentCollection();
                                newCollection.Id = _collectionName;
                                newCollection.PartitionKey.Paths.Add(Constants.DataStore.CollectionPartitionKey);

                                documentCollection = await docclient.CreateDocumentCollectionAsync(docDB.SelfLink,newCollection);
                            }

                            return documentCollection;
                        });
                    }

                    public async Task<T> GetDocByIdAsync(string id)
                    {
                        var db = await _database;
                        var coll = await _collection;
                        var client = await _client;
                        var documentUri = UriFactory.CreateDocumentUri(db.Id,coll.Id,id);
                        var response = await client.ReadDocumentAsync(documentUri,new RequestOptions { PartitionKey = new PartitionKey(id) });

                        return (T)(dynamic)response.Resource;
                    }

                    public async Task CreateOrUpdateAsync(T model)
                    {
                        var db = await _database;
                        var coll = await _collection;
                        var client = await _client; ;
                        await _client.UpsertDocumentAsync(_collection.SelfLink,model);
                    }
                }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)