Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SingletonAttribute.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Events;
5
7{
13 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
14 public class SingletonAttribute : Attribute
15 {
16 private static readonly Dictionary<SingletonKey, SingletonRecord> instances = new Dictionary<SingletonKey, SingletonRecord>();
17
24 {
25 }
26
27 internal static async Task Clear()
28 {
29 SingletonRecord[] Objects;
30
31 lock (instances)
32 {
33 Objects = new SingletonRecord[instances.Count];
34 instances.Values.CopyTo(Objects, 0);
35 instances.Clear();
36 }
37
38 foreach (SingletonRecord Rec in Objects)
39 {
40 if (Rec.Instantiated)
41 {
42 try
43 {
44 if (Rec.Instance is IDisposableAsync DisposableAsync)
45 await DisposableAsync.DisposeAsync();
46 else if (Rec.Instance is IDisposable Disposable)
47 Disposable.Dispose();
48 }
49 catch (Exception ex)
50 {
51 Log.Exception(ex);
52 }
53 }
54 }
55 }
56
64 public object Instantiate(bool ReturnNullIfFail, Type Type, params object[] Arguments)
65 {
66 SingletonKey Key = new SingletonKey(Type, Arguments);
67
68 lock (instances)
69 {
70 if (instances.TryGetValue(Key, out SingletonRecord Rec))
71 return Rec.Instance;
72 }
73
74 object Object = Types.Create(ReturnNullIfFail, Type, Arguments);
75 if (Object is null)
76 return null;
77
78 lock (instances)
79 {
80 if (instances.TryGetValue(Key, out SingletonRecord Rec))
81 {
82 if (Object is IDisposableAsync DisposableAsync)
83 {
84 Task.Run(async () =>
85 {
86 try
87 {
88 await DisposableAsync.DisposeAsync();
89 }
90 catch (Exception ex)
91 {
92 Log.Exception(ex);
93 }
94 });
95 }
96 else if (Object is IDisposable Disposable)
97 Disposable.Dispose();
98
99 return Rec.Instance;
100 }
101
102 instances[Key] = new SingletonRecord(Key, true, Object);
103 }
104
105 return Object;
106 }
107
113 public static void Register(object Object, params object[] Arguments)
114 {
115 SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);
116
117 lock (instances)
118 {
119 if (instances.ContainsKey(Key))
120 throw new InvalidOperationException("Singleton already registered.");
121
122 instances[Key] = new SingletonRecord(Key, false, Object);
123 }
124 }
125
132 public static bool Unregister(object Object, params object[] Arguments)
133 {
134 SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);
135
136 lock (instances)
137 {
138 return instances.Remove(Key);
139 }
140 }
141
148 public static void Replace(object Object, params object[] Arguments)
149 {
150 SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);
151
152 lock (instances)
153 {
154 instances[Key] = new SingletonRecord(Key, false, Object);
155 }
156 }
157
164 public static bool IsRegistered(Type Type, params object[] Arguments)
165 {
166 SingletonKey Key = new SingletonKey(Type, Arguments);
167
168 lock (instances)
169 {
170 return instances.ContainsKey(Key);
171 }
172 }
173
179 {
180 lock (instances)
181 {
182 SingletonRecord[] Result = new SingletonRecord[instances.Count];
183 instances.Values.CopyTo(Result, 0);
184 return Result;
185 }
186 }
187 }
188}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Defines a class or struct as singleton. This means that when instantiated, using Types....
SingletonAttribute()
Defines a class or struct as singleton. This means that when instantiated, using Types....
static bool Unregister(object Object, params object[] Arguments)
Unregisters a singleton instance of a type.
static SingletonRecord[] GetInstances()
Gets available singleton instances.
static void Replace(object Object, params object[] Arguments)
Replaces a singleton instance of a type.
object Instantiate(bool ReturnNullIfFail, Type Type, params object[] Arguments)
Returns an instance of the type Type .
static bool IsRegistered(Type Type, params object[] Arguments)
Checks if a singleton type (with optional associated arguments) is registered.
static void Register(object Object, params object[] Arguments)
Registers a singleton instance of a type.
Represents a type and a set of arguments, for which an object instance is the single instantiation.
Definition: SingletonKey.cs:10
A record of a singleton instance in memory.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static object Create(bool ReturnNullIfFail, Type Type, params object[] Arguments)
Returns an instance of the type Type . Creates an instance of a type. If the constructor requires arg...
Definition: Types.cs:1338
Interface for asynchronously disposable objects.