1

On a ASP.NET MVC application with multiple assemblies I need to access a few settings.

Basically. the settings are constants or values from Web.Config AppSettings.

My idea is to inject a Settings class, as a singleton, in places where I need it:

public interface ISettings {
  LoggerSettings Logger { get; }
} // ISettings

public class LoggerSettings {
  public String Levels { get { return ConfigurationManager.AppSettings["Logger.Levels"]; } }
  public const String Report = "team@xyz.com";
} // LoggerSettings

public class Settings : ISettings {
  public LoggerSettings Logger { get; private set; }

  public Settings() {
    Logger = new LoggerSettings();
  }
} // Settings
  1. What do you think about this approach and injecting the class as a singleton?

  2. Do I need, in this case, to set any class/property as static? I think I need to have LoggerSettings and its properties as static, not? Otherwise I will need to create a new instance when constructing the Settings?

Could someone, please, advise me on this?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

3

If you are actually injecting your class (via a DI framework), and by "singleton" you mean you are using a singleton "scope" in your DI framework, then this approach will work just fine. If this in fact what you are doing, then none of your properties need to be static, as the same "singleton" instance will be injected into any class that depends on it.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • Yes, I am using StructureMap to inject it as Singleton ... But will not LoggerSettings inside Settings be null because it is not static? – Miguel Moura Mar 28 '13 at 01:03
  • I haven't used StructureMap before, but presumably it will inject the (same) `LoggerSettings` dependency into your `Settings` class each time; i.e. it should *inject* that dependency into `Settings`. Since your property is marked `private set`, I'm not sure how it will do that, but I assume that's what you're trying to do. – Kirk Woll Mar 28 '13 at 01:05
  • I removed private set and in my StructureMap configuration I have simply the following: For().Singleton().Use(); This will successfully inject ISettings in my services but its Logger property is null. This is what I don't understand. – Miguel Moura Mar 28 '13 at 01:09
  • @Shapper, perhaps [this answer](http://stackoverflow.com/questions/4979453/structuremap-beginner-property-injection) will be of use to you. – Kirk Woll Mar 28 '13 at 01:12
  • Not really. That answers is saying how to inject objects into properties ... I do not want to inject LoggerSettings. I would like to to be initialized ... And since I am injecting Settings as singleton then its properties would also be initialized only once ... I think .... – Miguel Moura Mar 28 '13 at 01:21
  • I just added a constructor to Settings and since Settings is injected as Singleton I suppose that the LoggerSettings initialization occurs only once ... Correct? Other approach would be to use some static in this ... But since I am injecting it I don't since it would be a good approach ... Or would be? At least with the constructor LoggerSettings is not null anymore. – Miguel Moura Mar 28 '13 at 01:26