Last active
May 11, 2016 18:36
-
-
Save rondefreitas/ba66505089a2784b7483167918bb5e56 to your computer and use it in GitHub Desktop.
Example on how to use a POCO Settings class in ASP.NET Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IGoogleApiRequest | |
{ | |
string ShowMyKey(); | |
} | |
public class GoogleApiRequest : IGoogleApiRequest | |
{ | |
private readonly IGoogleApiSettings _settings; | |
public GoogleApiRequest(IGoogleApiSettings settings) { | |
if(settings == null) throw new ArgumentNullException(nameof(settings)); | |
_settings = settings; | |
} | |
public string ShowMyKey() | |
{ | |
return $"Here's your key: {_settings.ApiKey}"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IGoogleApiSettings | |
{ | |
string ApiKey { get; set; } | |
bool EnablePlacesApi { get; set; } | |
} | |
public class GoogleApiSettings | |
{ | |
public string ApiKey { get; set; } | |
public bool EnablePlacesApi { get; set; } = true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM this is an example of how you add your secret | |
REM make sure you've reviewed the docs at http://docs.asp.net/en/latest/security/app-secrets.html | |
REM or at http://go.microsoft.com/fwlink/?LinkID=532709 | |
user-secret set GoogleApiSettings:ApiKey YOUR-GOOGLE-API-KEY-HERE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AppSettings": { | |
"SomeSetting": "???" | |
}, | |
"GoogleApiSettings": { | |
"ApiKey": "SINCE WE ARE USING USER SECRETS THIS SHOULD NEVER BE THE VALUE", | |
"EnablePlacesApi": false | |
}, | |
"Data": { | |
"DefaultConnection": { | |
"ConnectionString": "Server=localhost\\sqlexpress;Database=WebApp;Trusted_Connection=True;MultipleActiveResultSets=true" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller { | |
private IGoogleApiRequest _googleApiRequest; | |
public HomeController(IGoogleApiRequest googleApiRequest) | |
{ | |
_googleApiRequest = googleApiRequest; | |
} | |
// if you do constructor injection | |
public IActionResult ApiKeyFromConstructor() | |
{ | |
return Content(_googleApiRequest.ShowMyKey()); | |
} | |
// if you do method body injection | |
public IActionResult ApiKeyFromMethodBody([FromServices]IGoogleApiRequest fromServicesGoogleApiRequest) | |
{ | |
return Content(fromServicesGoogleApiRequest.ShowMyKey()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make sure to add this dependency: | |
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ClassLibrary; | |
// warning, incomplete class - provided for reference only | |
public partial class Startup | |
{ | |
public Startup() | |
{ | |
// perform normal initialization from appsettings, etc | |
// make sure to use this for user secrets | |
if (hostingEnvironment.IsDevelopment()) | |
{ | |
builder.AddUserSecrets(); | |
} | |
builder.AddEnvironmentVariables(); | |
Configuration = builder.Build(); | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// do other things | |
var googleSettingsSection = Configuration.GetSection("GoogleApiSettings"); | |
// one way to do it - personally I prefer this over the IOptions API | |
services.AddTransient<IGoogleApiSettings>(provider => googleSettingsSection.Get<GoogleApiSettings>()); | |
// or if you really want to make use of IOptions... | |
services.AddOptions().Configure<GoogleApiSettings>(googleSettingsSection); | |
// you can then override the construction of your injected service | |
services.AddTransient<IGoogleApiRequest>( | |
provider => | |
{ | |
var options = provider.GetService<IOptions<GoogleApiSettings>>()?.Value; | |
return new GoogleApiRequest(options); | |
}); | |
// do more things | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment