CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
LazySingleton.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3
4namespace Csla
5{
10 public sealed class LazySingleton<T> where T : class
11 {
12 private readonly object _syncRoot = new object();
13 private T _value;
14 private readonly Func<T> _delegate;
15 private bool _isValueCreated;
16
17
23 : this(() => (T)Csla.Reflection.MethodCaller.CreateInstance(typeof(T))) { }
24
30 public LazySingleton(Func<T> @delegate)
31 {
32 _delegate = @delegate;
33 }
34
41 public bool IsValueCreated
42 {
43 get { return _isValueCreated; }
44 }
45
50 public T Value
51 {
52 get
53 {
54 if (!_isValueCreated) // not initialized
55 {
56 lock (_syncRoot) // lock syncobject
57 {
58 if (!_isValueCreated) // recheck for initialized after lock is taken
59 {
60 _value = _delegate.Invoke();
61 _isValueCreated = true; // mark as initialized
62 }
63 }
64 }
65
66 return _value;
67 }
68 }
69 }
70}
An alternative to Lazy<T>
LazySingleton()
Initializes a new instance of the LazySingleton<T> class.
T Value
Gets the instance.
bool IsValueCreated
Gets a value indicating whether this instance is initilized and contains a value.
LazySingleton(Func< T > @delegate)
Initializes a new instance of the LazySingleton<T> class.