diff --git a/models/Settings/Configuration.cs b/models/Settings/Configuration.cs new file mode 100644 index 0000000..57d09aa --- /dev/null +++ b/models/Settings/Configuration.cs @@ -0,0 +1,15 @@ +namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.Settings; + +public record Configuration +{ + public required AzureCosmosDB AzureCosmosDB { get; init; } +} + +public record AzureCosmosDB +{ + public required string Endpoint { get; init; } + + public required string DatabaseName { get; init; } + + public required string ContainerName { get; init; } +} \ No newline at end of file diff --git a/services/DemoService.cs b/services/DemoService.cs index 28a60bc..b0e39c4 100644 --- a/services/DemoService.cs +++ b/services/DemoService.cs @@ -1,21 +1,30 @@ +using System.Configuration; using Microsoft.Azure.Cosmos; +using Microsoft.Extensions.Options; using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models; using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces; +using Settings = Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.Settings; + namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services; -public sealed class DemoService(CosmosClient client) : IDemoService +public sealed class DemoService( + CosmosClient client, + IOptions configurationOptions +) : IDemoService { + private readonly Settings.Configuration configuration = configurationOptions.Value; + public string GetEndpoint() => $"{client.Endpoint}"; public async Task RunAsync(Func writeOutputAync) { - Database database = client.GetDatabase("cosmicworks"); + Database database = client.GetDatabase(configuration.AzureCosmosDB.DatabaseName); database = await database.ReadAsync(); await writeOutputAync($"Get database:\t{database.Id}"); - Container container = database.GetContainer("products"); + Container container = database.GetContainer(configuration.AzureCosmosDB.ContainerName); container = await container.ReadContainerAsync(); await writeOutputAync($"Get container:\t{container.Id}"); diff --git a/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj b/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj index 3a8972d..3f1a720 100644 --- a/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj +++ b/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj @@ -5,8 +5,9 @@ enable - - + + + diff --git a/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj b/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj index 952768b..814ed0a 100644 --- a/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj +++ b/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj @@ -6,9 +6,9 @@ f6167579-5a7c-405e-bdae-cf20a79d6b9d - - - + + + diff --git a/web/Program.cs b/web/Program.cs index 1f471dd..18ae16d 100644 --- a/web/Program.cs +++ b/web/Program.cs @@ -1,15 +1,23 @@ using Azure.Identity; using Microsoft.Azure.Cosmos; +using Microsoft.Extensions.Options; using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services; using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces; using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.Components; +using Settings = Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.Settings; + var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorComponents().AddInteractiveServerComponents(); -builder.Services.AddSingleton((_) => +builder.Services.AddOptions().Bind(builder.Configuration.GetSection(nameof(Settings.Configuration))); + +builder.Services.AddSingleton((serviceProvider) => { + IOptions configurationOptions = serviceProvider.GetRequiredService>(); + Settings.Configuration configuration = configurationOptions.Value; + CosmosClient client = new( connectionString: "" ); diff --git a/web/appsettings.json b/web/appsettings.json index c5f5d9e..945b1bb 100644 --- a/web/appsettings.json +++ b/web/appsettings.json @@ -6,5 +6,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" -} + "AllowedHosts": "*", + "Configuration": { + "AzureCosmosDB": { + "DatabaseName": "cosmicworks", + "ContainerName": "products" + } + } +} \ No newline at end of file