Skip to content

Commit ceb8f1e

Browse files
author
Sergey Tregub
committed
Use option pattern
1 parent d5eccdf commit ceb8f1e

6 files changed

Lines changed: 43 additions & 26 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace ReferenceProject.Configuration
5+
{
6+
public static class ApplicationSettings
7+
{
8+
public static void AddSettings(this IServiceCollection services, IConfiguration Configuration)
9+
{
10+
services.AddOptions<Settings.Products>()
11+
.Bind(Configuration.GetSection(Settings.Products.SectionName));
12+
}
13+
}
14+
}

ProjectTemplates/ReferenceProject/Controllers/ProductsController.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Options;
56
using ReferenceProject.Repo;
67
using System;
78
using System.Collections.Generic;
@@ -14,15 +15,17 @@ namespace ReferenceProject.Controllers
1415
[Produces("application/json")]
1516
public class ProductsController: ControllerBase
1617
{
17-
Repo.IProductsRepo ProductsRepo { get; }
18+
IProductsRepo ProductsRepo { get; }
19+
IOptionsSnapshot<Settings.Products> Settings { get; }
1820
IMapper Mapper { get; }
1921
ILogger Logger { get; }
2022

21-
public ProductsController(IProductsRepo productsRepo, IMapper mapper, ILogger<ProductsController> logger)
23+
public ProductsController(IProductsRepo productsRepo, IOptionsSnapshot<Settings.Products> options, IMapper mapper, ILogger<ProductsController> logger)
2224
{
2325
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
2426
Mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
2527
ProductsRepo = productsRepo ?? throw new ArgumentNullException(nameof(productsRepo));
28+
Settings = options ?? throw new ArgumentNullException(nameof(options));
2629
}
2730

2831
/// <summary>
@@ -111,5 +114,11 @@ public IActionResult ThrowAnException()
111114
{
112115
throw new Exception("Example exception");
113116
}
117+
118+
[HttpGet("Settings")]
119+
public Settings.Products GetSettings()
120+
{
121+
return Settings.Value;
122+
}
114123
}
115124
}

ProjectTemplates/ReferenceProject/Settings.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace ReferenceProject.Settings
2+
{
3+
public class Products
4+
{
5+
public const string SectionName = "Products";
6+
7+
public string TempFolder { get; set; }
8+
public string BackendServiceUrl { get; set; }
9+
}
10+
}

ProjectTemplates/ReferenceProject/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Autofac;
22
using Autofac.Configuration;
3+
using Autofac.Core;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
@@ -15,6 +16,7 @@
1516
using Newtonsoft.Json;
1617
using Newtonsoft.Json.Converters;
1718
using Newtonsoft.Json.Serialization;
19+
using ReferenceProject.Configuration;
1820
using ReferenceProject.Filters;
1921
using ReferenceProject.Modules;
2022
using System.IO;
@@ -94,6 +96,8 @@ public void ConfigureServices(IServiceCollection services)
9496
services.AddRouting();
9597
services.AddControllers();
9698
services.AddHealthChecks();
99+
100+
services.AddSettings(Configuration);
97101
}
98102

99103
/// <summary>

ProjectTemplates/ReferenceProject/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
"FromLogContext"
3333
]
3434
},
35+
"Products": {
36+
"TempFolder": "%TEMP%",
37+
"BackendServiceUrl": "http://%gateway%/backend"
38+
},
3539
"AllowedHosts": "*",
36-
"CoolServiceEndpoint": "http://%ENDPOINT_HOST%/cool",
37-
"AnotherServiceEndpoint": "http://%ENDPOINT_HOST%/another",
3840
"Urls": "http://localhost:5000"
3941
}

0 commit comments

Comments
 (0)