Skip to content

Commit 3e8016d

Browse files
calin-lupas_dsamcapscalin-lupas_dsamcaps
authored andcommitted
Refactor CosmosDB options and container management
Removed the `GitHubWebhooksContainer` property from `CosmosDbOptions` for simplification. Introduced `CosmosContainerHelper` to dynamically generate container names based on entity types. Updated `CosmosDbStorageContext` to use this helper, eliminating the need for a manual container parameter. Adjusted `appsettings.json` to reflect these changes by removing the corresponding configuration option.
1 parent 0bbac7c commit 3e8016d

4 files changed

Lines changed: 37 additions & 19 deletions

File tree

src/DevExcelerateApi/Core/Options/CosmosDbOptions.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@ public class CosmosDbOptions
1111
/// Gets or sets the CosmosDB database name.
1212
/// </summary>
1313
[Required]
14-
public string Database { get; set; } = string.Empty;
15-
16-
/// <summary>
14+
public string Database { get; set; } = string.Empty; /// <summary>
1715
/// Gets or sets the Cosmos connection string.
1816
/// </summary>
1917
[Required]
2018
public string ConnectionString { get; set; } = string.Empty;
21-
22-
/// <summary>
23-
/// Gets or sets the CosmosDB container for GitHub Webhooks.
24-
/// </summary>
25-
[Required]
26-
public string GitHubWebhooksContainer { get; set; } = string.Empty;
2719
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace DevExcelerateApi.Data
2+
{
3+
/// <summary>
4+
/// Helper class for generating CosmosDB container names from entity types.
5+
/// Similar to SqlTableHelper, this uses the entity type name as the container name.
6+
/// </summary>
7+
public static class CosmosContainerHelper
8+
{
9+
/// <summary>
10+
/// Gets the container name for the specified entity type.
11+
/// </summary>
12+
/// <typeparam name="T">The entity type.</typeparam>
13+
/// <returns>The container name based on the entity type name.</returns>
14+
public static string GetContainerName<T>() where T : IStorageEntity
15+
{
16+
return typeof(T).Name;
17+
}
18+
19+
/// <summary>
20+
/// Gets the container name for the specified entity type.
21+
/// </summary>
22+
/// <param name="entityType">The entity type.</param>
23+
/// <returns>The container name based on the entity type name.</returns>
24+
public static string GetContainerName(Type entityType)
25+
{
26+
return entityType.Name;
27+
}
28+
}
29+
}

src/DevExcelerateApi/Data/CosmosDbStorageContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ public class CosmosDbStorageContext<T> : IStorageContext<T>, IDisposable where T
1919
/// <summary>
2020
/// CosmosDB container.
2121
/// </summary>
22-
private readonly Container _container;
23-
22+
private readonly Container _container;
23+
2424
/// <summary>
2525
/// Initializes a new instance of the CosmosDbContext class.
2626
/// </summary>
2727
/// <param name="connectionString">The CosmosDB connection string.</param>
2828
/// <param name="database">The CosmosDB database name.</param>
29-
/// <param name="container">The CosmosDB container name.</param>
30-
public CosmosDbStorageContext(string connectionString, string database, string container)
29+
public CosmosDbStorageContext(string connectionString, string database)
3130
{
3231
// Configure JsonSerializerOptions
3332
var options = new CosmosClientOptions
@@ -38,7 +37,8 @@ public CosmosDbStorageContext(string connectionString, string database, string c
3837
},
3938
};
4039
_client = new CosmosClient(connectionString, options);
41-
_container = _client.GetContainer(database, container);
40+
var containerName = CosmosContainerHelper.GetContainerName<T>();
41+
_container = _client.GetContainer(database, containerName);
4242
}
4343

4444
/// <inheritdoc/>

src/DevExcelerateApi/appsettings.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@
3131
"Type": "",
3232
"FileSystem": {
3333
"FilePath": ""
34-
},
35-
"CosmosDb": {
34+
}, "CosmosDb": {
3635
"Database": "",
3736
// dotnet user-secrets set "DataStore:CosmosDb:ConnectionString" "MY_COSMOS_CONNECTION_STRING"
38-
"ConnectionString": "",
39-
// Each container requires a specific partition key. Ensure these are set correctly in your CosmosDB instance.
40-
"GitHubWebhooksContainer": ""
37+
"ConnectionString": ""
4138
},
4239
"SqlServer": {
4340
"ConnectionString": "" // dotnet user-secrets set "DataStore:SqlServer:ConnectionString" "MY_SQLSERVER_CONNECTION_STRING"

0 commit comments

Comments
 (0)