Our application on PRODUCTION uses repository class, to create database, collection if not exists already and then to save & query the documents. This class working as expected on PRODUCTION, no issues. Please note in the current PROD database we have around 15 different collections and TTL not enabled on any of these models, as there is no business requirement.
Now we want to create couple of brand new collections in same database but with TTL enabled by mentioning -1 as TTL & to save documents with respective TTL values. To achieve this below class can be modified but it will break OCP/SOLID principle & it is risky to do so on LIVE application
So want extend the below class using inheritance or composition so that the existing functionalities won't break & as well we can able to support new requirement of enabling TTL on new collections & its respective document. Any thoughts or suggestions to do it on the best possible way? please
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);
}
}
question from:
https://stackoverflow.com/questions/65940874/best-way-to-create-cosmos-collections-first-without-later-with-ttl-using-cosmo 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…