Singleton.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace GreenTree.Nachtragsmanagement.Web.Framework
  7. {
  8. /// <summary>
  9. /// A statically compiled "singleton" used to store objects throughout the
  10. /// lifetime of the app domain. Not so much singleton in the pattern's
  11. /// sense of the word as a standardized way to store single instances.
  12. /// </summary>
  13. /// <typeparam name="T">The type of object to store.</typeparam>
  14. /// <remarks>Access to the instance is not synchrnoized.</remarks>
  15. public class Singleton<T> : Singleton
  16. {
  17. static T instance;
  18. /// <summary>The singleton instance for the specified type T. Only one instance (at the time) of this object for each type of T.</summary>
  19. public static T Instance
  20. {
  21. get { return instance; }
  22. set
  23. {
  24. instance = value;
  25. AllSingletons[typeof(T)] = value;
  26. }
  27. }
  28. }
  29. /// <summary>
  30. /// Provides a singleton list for a certain type.
  31. /// </summary>
  32. /// <typeparam name="T">The type of list to store.</typeparam>
  33. public class SingletonList<T> : Singleton<IList<T>>
  34. {
  35. static SingletonList()
  36. {
  37. Singleton<IList<T>>.Instance = new List<T>();
  38. }
  39. /// <summary>The singleton instance for the specified type T. Only one instance (at the time) of this list for each type of T.</summary>
  40. public new static IList<T> Instance
  41. {
  42. get { return Singleton<IList<T>>.Instance; }
  43. }
  44. }
  45. /// <summary>
  46. /// Provides a singleton dictionary for a certain key and vlaue type.
  47. /// </summary>
  48. /// <typeparam name="TKey">The type of key.</typeparam>
  49. /// <typeparam name="TValue">The type of value.</typeparam>
  50. public class SingletonDictionary<TKey, TValue> : Singleton<IDictionary<TKey, TValue>>
  51. {
  52. static SingletonDictionary()
  53. {
  54. Singleton<Dictionary<TKey, TValue>>.Instance = new Dictionary<TKey, TValue>();
  55. }
  56. /// <summary>The singleton instance for the specified type T. Only one instance (at the time) of this dictionary for each type of T.</summary>
  57. public new static IDictionary<TKey, TValue> Instance
  58. {
  59. get { return Singleton<Dictionary<TKey, TValue>>.Instance; }
  60. }
  61. }
  62. /// <summary>
  63. /// Provides access to all "singletons" stored by <see cref="Singleton{T}"/>.
  64. /// </summary>
  65. public class Singleton
  66. {
  67. static Singleton()
  68. {
  69. allSingletons = new Dictionary<Type, object>();
  70. }
  71. static readonly IDictionary<Type, object> allSingletons;
  72. /// <summary>Dictionary of type to singleton instances.</summary>
  73. public static IDictionary<Type, object> AllSingletons
  74. {
  75. get { return allSingletons; }
  76. }
  77. }
  78. }