2

Background: Asp.Net Core website using Dependency Injection to relay needed services to various parts of the website

I have a service that is being added to the IServiceCollection in the ConfigureServices method in my Startup class as a singleton in the following manner:

//Settings
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<ISettingsIO, SettingsIO>();
services.AddSingleton<ISettings>(f =>
{
    return f.GetService<ISettingsService>().GetSettings();
});

This works great and all the pages/controllers I need to have access to Example can do so without issue.

However, I now have the capability to change the data that is being pulled in the method GetSettings(). This means that I need to update the service that is added to the ServiceCollection.

How can I do this without changing the service from a singleton to transient?

Thanks for any help provided!

7
  • How is GetSettings implemented? Commented Apr 28, 2016 at 20:59
  • 1
    I don't understand why ISetting is also as singleton since ISettingsService is allready. Why not use it as transient or scoped and let the SettingsService who is singleton resolve it ? Commented Apr 28, 2016 at 21:04
  • @YacoubMassad - GetSiteSettings returns an ISiteSettings Object that it creates from a query to the DB. Commented Apr 28, 2016 at 21:11
  • @Fabien - Is there something else I would need to do other than simply change the ISettings to AddTransient? I tried that and saw no noticeable changes. Commented Apr 28, 2016 at 21:11
  • 1
    One way to do it (which might not be the cleanest way to do it) is to make GetSettings return a smart implementation of ISettings. Such implementation starts with an initial set of settings (the ones from the database) but that can then return a new set of settings. This implementation would provide means for you to set the new settings. Commented Apr 28, 2016 at 21:41

1 Answer 1

1

As I said in the comment, this is not very clean. I think that a better solution would require more information about your system.

Create a mutable settings wrapper class:

public class MyMutableSettings : ISettings
{
    public ISettings Settings {get;set;}

    //Implement the ISettings interface by delegating to Settings, e.g.:
    public int GetNumberOfCats()
    {
        return Settings.GetNumberOfCats();
    }
}

And then you can use it like this:

MyMutableSettings mySettings = new MyMutableSettings();

services.AddSingleton<ISettings>(f =>
{
    mySettings.Settings = f.GetService<ISettingsService>().GetSettings();

    return mySettings;
});

Then, when you want to change the settings:

mySettings.Settings = GetMyNewSettings();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.