code_junkie

Updating reference to a member variable in use

Nov 11th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. internal sealed class SektionCache : CacheBase
  2. {
  3. public static readonly SektionCache Instance = new SektionCache();
  4. private static readonly object lockObject = new object();
  5. private static bool isUpdating;
  6.  
  7. private IEnumerable<Sektion> sektioner;
  8.  
  9. static SektionCache()
  10. {
  11. UpdateCache();
  12. }
  13.  
  14. public IEnumerable<Sektion> Sektioner
  15. {
  16. get { return sektioner; }
  17. }
  18.  
  19. public static void UpdateCache()
  20. {
  21. // SNIP - getting data, locking etc.
  22. Instance.sektioner = newSektioner;
  23. // SNIP
  24. }
  25. }
  26.  
  27. internal sealed class SektionCache : CacheBase
  28. {
  29. //public static readonly SektionCache Instance = new SektionCache();
  30.  
  31. // this template is better ( safer ) than the previous one, for thread-safe singleton patter >>>
  32. private static SektionCache defaultInstance;
  33. private static object readonly lockObject = new object();
  34. public static SektionCach Default {
  35. get {
  36. SektionCach result = defaultInstance;
  37. if ( null == result ) {
  38. lock( lockObject ) {
  39. if ( null == result ) {
  40. defaultInstance = result = new SektionCache();
  41. }
  42. }
  43. }
  44.  
  45. return result;
  46. }
  47. }
  48. // <<< this template is better ( safer ) than the previous one
  49.  
  50. //private static readonly object lockObject = new object();
  51. //private static bool isUpdating;
  52. //private IEnumerable<Sektion> sektioner;
  53.  
  54. // this declaration is enough
  55. private volatile IEnumerable<Sektion> sektioner;
  56.  
  57. // no static constructor is required >>>
  58. //static SektionCache()
  59. //{
  60. // UpdateCache();
  61. //}
  62. // <<< no static constructor is required
  63.  
  64. // I think, you can use getter and setter for reading & changing a collection
  65. public IEnumerable<Sektion> Sektioner {
  66. get {
  67. IEnumerable<Sektion> result = this.sektioner;
  68. // i don't know, if you need this functionality >>>
  69. // if ( null == result ) { result = new Sektion[0]; }
  70. // <<< i don't know, if you need this functionality
  71. return result;
  72. }
  73. set { this.sektion = value; }
  74. }
  75.  
  76. //public static void UpdateCache()
  77. //{
  78. //// SNIP - getting data, locking etc.
  79. //Instance.sektioner = newSektioner;
  80. //// SNIP
  81. //}
  82. }
Add Comment
Please, Sign In to add comment