What is the recommended method of loading data once to user everywhere around the app?
What is the recommended method of loading data once to user everywhere around the app?
As almost every project has some predefined setting values which are to be used throughout the application, what is the recommended way of loading them once and then using everywhere?
For example, I did a project where I had some settings stored in the database which were accessed throughout the application. Rather than querying them each and every time, I was loading everything into a static class and then would use that throughout the application. What other approach do you use?
ISettingsProvider
ISettingsProvider
You can also create a custom config provider that reads from a database. See: docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/…
– Chris Pratt
Jul 3 at 13:09
1 Answer
1
If you have a class that retrieves your settings from the database:
public class SqlSettingsProvider : ISettingsProvider
{
private readonly string _connectionString;
public SqlSettingsProvider(string connectionString)
{
_connectionString = connectionString;
}
public Settings GetSettings()
{
// load the settings from SQL
}
}
You can register that class with your ServiceCollection
:
ServiceCollection
var connectionString = "xyz"; // load this from config;
services.AddSingleton<SqlSettingsProvider>(provider =>
new SqlSettingsProvider(connectionString));
(You could have an interface like ISettingsProvider
, but in this example it's not needed, because you're going to inject Settings
, not the settings provider.)
ISettingsProvider
Settings
Then, configure your service collection to resolve Settings
by resolving SqlSettingsProvider
and using it to retrieve an instance of Settings
.
Settings
SqlSettingsProvider
Settings
services.AddSingleton<Settings>(provider =>
provider.GetService<SqlSettingsProvider>().GetSettings());
Now wherever you need Settings
you just inject them:
Settings
public class SomeControllerOrOtherClass
{
private readonly Settings _settings;
public SomeControllerOrOtherClass(Settings settings)
{
_settings = settings;
}
}
Now your classes can just depend on Settings
and not care where it comes from. The service collection will only create it once and will then return the same instance each time, meaning it only gets looked up once from the database.
Settings
There could be some long-term benefit to injecting an interface like ISettingsProvider
instead. That gives you a little bit more flexibility. For example, instead of storing the Settings
for the lifetime of the application it could cache them for specified duration, or it could behave differently in some other way.
ISettingsProvider
Settings
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You could inject a factory like
ISettingsProvider
into classes that need those settings. Then you could use an implementation that retrieves the settings and caches them in memory to return the same instance instead of reloading them. You could even just inject the settings class where needed and configure the container to resolveISettingsProvider
, and then make the settings class it returns a singleton.– Scott Hannen
Jul 2 at 19:22