Skip to content

Instantly share code, notes, and snippets.

@premrajah
Last active March 12, 2020 01:50
Show Gist options
  • Save premrajah/8050b0c779b17fef131a3efbed9390a0 to your computer and use it in GitHub Desktop.
Save premrajah/8050b0c779b17fef131a3efbed9390a0 to your computer and use it in GitHub Desktop.
#The strongly-typed approach(es)
The approach that we chose to retrieve the appsettings.json configuration values
makes use of the generic IConfiguration object, which can be queried using the
preceding string-based syntax. This approach is rather practical; however, if we want
to retrieve this data in a more robust way, for example, in a strongly typed fashion, we
can—and should—implement something better. Although we won't cover that in
more depth in this book, we suggest you read the following great articles, showing
three different approaches to achieving this result:
##The first one, written by Rick Strahl, explains how to do that using the IOptions<T>
provider interface:
https:/ / weblog. west- wind. com/ posts/ 2016/ may/ 23/ strongly- typedconfiguration-
settings- in- aspnet- core
##The second, by Filip W, explains how to do that with a simple POCO class, thus
avoiding the IOptions<T> interface and the extra dependencies required by the
preceding approach:
https:/ / www. strathweb. com/ 2016/ 09/ strongly- typed- configuration- in- asp- netcore-
without- ioptionst/
##The third, by Khalid Abuhakmeh, shows an alternative way to use a standard POCO
class and directly register it as a Singleton with the ServicesCollection, while
also (optionally) shielding it from unwanted modifications due to development
mistakes:
https:/ / rimdev. io/ strongly- typed- configuration- settings- in- asp- net- corepart-
ii/
All of these approaches were originally meant to work with .NET Core 1.x; however,
they can still be used with .NET Core 3.x (at the time of writing). That said, if we were
to choose, we would probably go with the final option, as we find it to be the cleanest
and cleverest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment