.net - Singleton in DependencyInjection -


i struggling concept of singletons in dependency injection. not sure whether classes should implemented in way support singleton / per instance instancing classes intended used singletons or whether should rely on proper settings of instancing programmer.

following class work expected if marked singleton in dependency container

... builder.registertype<applicationsettings>().asself().singleinstance(); ...   /// <summary> /// allows create many applicationsettings instances each of them have collection of settings.  /// cannot guarantee 1 of class has complete settings /// </summary> public class applicationsettings {     private readonly object _locker = new object();     private readonly dictionary<string, object> _settings;     private readonly ilog _log;      public applicationsettings(ilog log)     {         _log = log;         _settings = loadsettings();         thread.sleep(3000); //inner hardwork, e.g. cashing of     }      public object getsettings(string key)     {         lock (_locker)         {             return _settings.containskey(key) ? _settings[key] : null;         }     }      public void setsettings(string key, object value)     {         lock (_locker)         {             _settings.remove(key);             _settings.add(key, value);         }     }      public void remove(string key)     {         lock (_locker)         {             _settings.remove(key);         }     }      public void save()     {         thread.sleep(5000); //saving somewhere     }      private dictionary<string, object> loadsettings()     {         thread.sleep(5000); //long loading somewhere         return new dictionary<string, object>();     } } 

all classes need use applicationsettings class share 1 instance , settings contain information when saving somewhere. on other hand if programmer not mark class singleinstance there issue when saving because if implemented replacing whole collection in storage place not settings saved. correct functionality depends on programmer knowledge of class , using singleton.

in second example using static field settings allows me use class singleton or instancing per instance without effecting core functionality (i mean not settings saved if more instances of applicationsettings2 used)

/// <summary> /// allows create many applicationsettings2 instances each of them share same collection of settings.  /// </summary> public class applicationsettings2 {     private static readonly object locker = new object();     private static readonly dictionary<string, object> settings;     private readonly ilog _log;      static applicationsettings2()     {         settings = loadsettings();         thread.sleep(3000); //inner hardwork, e.g. cashing of     }       public applicationsettings2(ilog log)     {         _log = log;     }      public object getsettings(string key)     {         lock (locker)         {             return settings.containskey(key) ? settings[key] : null;         }     }      public void setsettings(string key, object value)     {         lock (locker)         {             settings.remove(key);             settings.add(key, value);         }     }      public void remove(string key)     {         lock (locker)         {             settings.remove(key);         }     }      public void save()     {         thread.sleep(5000); //saving somewhere     }      private static dictionary<string, object> loadsettings()     {         thread.sleep(5000);         return new dictionary<string, object>();     } } 

both approach of class usage

... builder.registertype<applicationsettings>().asself().singleinstance(); ... 

or

... builder.registertype<applicationsettings>().asself(); ... 

will lead same expected functionality. difference not singleton instancing mode lead slower functionality (there sleep in ctor), in end of day changing instancing mode not break anything.

my questions:

  • who responsible defining class should used singleton in dependency container configuration?
  • where should information instancing stored?
  • should implement classes intending used singleton able work in environment instancing per instance?
  • if adding 3rd party dll's class dependency container configuration, responsible inform me how should instancing class? (how programmer can know kind of instancing should use? should information mandatory part of documentation, or should programmer use "try/use" approach?)
  • should "singleton" used way how make system faster should singletons able work if instanced many times? (in dependency container configuration singleinstance keyword missing)

we use singleinstance lifetimescope when need single instance shared across container. i.e when need singleton semantics, use singleinstance lifetimescope.

you shouldn't manually code against static fields able use class single instance. because defeats point of using "ioc containter" managing object lifetime. if that, can't use object instance per dependency lifetimescope you'll sharing data between 2 unrelated instances.

so first example right , second 1 wrong.

who responsible defining class should used singleton in dependency container configuration?

the developer decides needs singleton semantics 1 decides it.

where should information instancing stored?

not sure mean that. update answer after clarification.

should implement classes intending used singleton able work in environment instancing per instance?

there nothing special have make work instance per dependency. if class designed act singleton (i.e thread safe) can work instance per dependency (with unnecessary synchronization such locks).

if adding 3rd party dll's class dependency container configuration, responsible inform me how should instancing class? (how programmer can know kind of instancing should use? should information mandatory part of documentation, or should programmer use "try/use" approach?)

you need read documentation component you're using make sure thread safe. if is, may use single instance. otherwise, need wrapper on component adds proper synchronization make thread safe. if there no documentation available, may need consult author, read source code (if available) or use decompiler find implementation make sure thread safe.

you can pretty use class instance per dependency (maybe memory overhead).

should "singleton" used way how make system faster should singletons able work if instanced many times? (in dependency container configuration singleinstance keyword missing)

not sure understand this, if mean singleton instance should work when registered instance per dependency. yes, should.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -