CSLA.NET 6.0.0
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> : Core.IUseApplicationContext
11 where T : class
12 {
13 private readonly object _syncRoot = new object();
14 private T _value;
15 private readonly Func<T> _delegate;
16 private bool _isValueCreated;
17
18 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
19 private ApplicationContext ApplicationContext { get; set; }
20
26 {
27 _delegate = () => (T)ApplicationContext.CreateInstanceDI(typeof(T));
28 }
29
35 public LazySingleton(Func<T> @delegate)
36 {
37 _delegate = @delegate;
38 }
39
46 public bool IsValueCreated
47 {
48 get { return _isValueCreated; }
49 }
50
55 public T Value
56 {
57 get
58 {
59 if (!_isValueCreated) // not initialized
60 {
61 lock (_syncRoot) // lock syncobject
62 {
63 if (!_isValueCreated) // recheck for initialized after lock is taken
64 {
65 _value = _delegate.Invoke();
66 _isValueCreated = true; // mark as initialized
67 }
68 }
69 }
70
71 return _value;
72 }
73 }
74 }
75}
Provides consistent context information between the client and server DataPortal objects.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
Creates a new instance of the type
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.