Class CosmosConfigBuilder
Builder pattern for creating CosmosConfig instances with a fluent API. This provides a cleaner way to configure Cosmos DB settings without dealing with multiple constructor parameters.
Inherited Members
Namespace: Benday.CosmosDb.Utilities
Assembly: Benday.CosmosDb.dll
Syntax
public class CosmosConfigBuilder
Constructors
| Edit this page View SourceCosmosConfigBuilder()
Initializes a new instance of the CosmosConfigBuilder.
Declaration
public CosmosConfigBuilder()
Methods
| Edit this page View SourceBuild()
Builds the CosmosConfig instance with the configured settings.
Declaration
public CosmosConfig Build()
Returns
| Type | Description |
|---|---|
| CosmosConfig | The configured CosmosConfig instance |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when required settings are missing |
ForEmulator()
Configures the builder with optimal settings for the Azure Cosmos DB Linux emulator. This sets Gateway mode (required), disables bulk execution (not supported), enables structure creation (convenient for dev), and uses the standard emulator endpoint and key.
Declaration
public CosmosConfigBuilder ForEmulator()
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
FromConfig(CosmosConfig)
Creates a CosmosConfigBuilder from an existing CosmosConfig instance. Useful for modifying existing configurations.
Declaration
public static CosmosConfigBuilder FromConfig(CosmosConfig config)
Parameters
| Type | Name | Description |
|---|---|---|
| CosmosConfig | config | The existing configuration |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | A new builder with the existing configuration values |
UseDefaultAzureCredential()
Configures the builder to use DefaultAzureCredential for authentication instead of an account key.
Declaration
public CosmosConfigBuilder UseDefaultAzureCredential()
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
UseGatewayMode()
Enables Gateway connection mode instead of Direct mode. Gateway mode can be useful for development or firewall-restricted environments.
Declaration
public CosmosConfigBuilder UseGatewayMode()
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
UseHierarchicalPartitionKeys()
Enables hierarchical partition keys.
Declaration
public CosmosConfigBuilder UseHierarchicalPartitionKeys()
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithAccountKey(string)
Sets the Cosmos DB account key for authentication.
Declaration
public CosmosConfigBuilder WithAccountKey(string accountKey)
Parameters
| Type | Name | Description |
|---|---|---|
| string | accountKey | The account key |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithBulkExecution(bool)
Configures bulk execution settings.
Declaration
public CosmosConfigBuilder WithBulkExecution(bool allowBulkExecution = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | allowBulkExecution | Whether to allow bulk execution (default: true) |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithCamelCase(bool)
Configures camelCase JSON serialization. Enabled by default. When enabled, C# PascalCase properties will be serialized as camelCase in Cosmos DB documents.
Declaration
public CosmosConfigBuilder WithCamelCase(bool useCamelCase = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | useCamelCase | Whether to use camelCase (default: true) |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithContainer(string)
Sets the container configuration.
Declaration
public CosmosConfigBuilder WithContainer(string containerName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | containerName | The name of the container |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithCreateStructures()
Enables automatic creation of database and container structures if they don't exist. Note: This should typically be disabled in production environments.
Declaration
public CosmosConfigBuilder WithCreateStructures()
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithDatabase(string, int?)
Sets the database configuration.
Declaration
public CosmosConfigBuilder WithDatabase(string databaseName, int? throughput = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | databaseName | The name of the database |
| int? | throughput | Optional throughput for the database (default: 400 RU/s) |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithEndpoint(string)
Sets the Cosmos DB endpoint URL.
Declaration
public CosmosConfigBuilder WithEndpoint(string endpoint)
Parameters
| Type | Name | Description |
|---|---|---|
| string | endpoint | The endpoint URL |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |
WithPartitionKey(string, bool)
Sets the partition key configuration.
Declaration
public CosmosConfigBuilder WithPartitionKey(string partitionKey, bool useHierarchical = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | partitionKey | The partition key path(s), comma-separated for hierarchical keys |
| bool | useHierarchical | Whether to use hierarchical partition keys |
Returns
| Type | Description |
|---|---|
| CosmosConfigBuilder | The builder instance for chaining |