A simpler way of querying a CosmosDb Namespace
- Your models / cosmos entities should inherit from the interface
public class MyCosmosEntity : ICosmicEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
// The properties below are implementations from ICosmicEntity
public string id { get; set; }
public DateTime AddedOn { get; set; }
public DateTime ModifiedOn { get; set; }
[JsonIgnore]
public string PartitionKey => FirstName;
}
- Create a repository like so:
public class MyRepository : Galaxy<MyModel>
{
public MyRepository(CosmosClient client, string database, string container, string partitionKey) : base(client, database, container, partitionKey)
{
}
}
// If you want to see debug information such as the full Query text executed, use the format below:
public class MyRepository : Galaxy<MyModel>
{
public MyRepository(CosmosClient client, string database, string container, string partitionKey) : base(client, database, container, partitionKey, true)
{
}
}
- In your Startup.cs / Main method / Program.cs, configure the CosmosClient like so:
_ = services.AddScoped(_ => new CosmosClient(
System.Environment.GetEnvironmentVariable("CosmosDbUri"),
System.Environment.GetEnvironmentVariable("CosmosDbPrimaryKey"),
clientOptions: new()
{
Serializer = new UniverseSerializer() // This is from Universe.Options
AllowBulkExecution = true // This will tell the underlying code to allow async bulk operations
}
));
- In your Startup.cs / Main method / Program.cs, configure your CosmosDb repository like so:
_ = services.AddScoped<IGalaxy<MyModel>, MyRepository>(service => new(
client: service.GetRequiredService<CosmosClient>(),
database: "database-name",
container: "container-name",
partitionKey: "/partitionKey"
));
- Inject your
IGalaxy<MyModel>
dependency into your classes and enjoy a simpler way to query CosmosDb
MyModel myModel = await IGalaxy<MyModel>.Get(new Catalyst("LastName", "last name value"));