IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Agile Estimation | Deployment-Strategies | |
Singleton Pattern in C# |
The Singleton Pattern ensures a class has only one instance and provides a global point of access to it.
public sealed class Singleton {
private static Singleton instance = null;
private Singleton() { }
public static Singleton Instance {
get {
if (instance == null)
instance = new Singleton();
return instance;
}
}
}
public sealed class Singleton {
private static Singleton instance = null;
private static readonly object padlock = new object();
private Singleton() { }
public static Singleton Instance {
get {
lock (padlock) {
if (instance == null)
instance = new Singleton();
return instance;
}
}
}
}
public sealed class Singleton {
private static Singleton instance = null;
private static readonly object padlock = new object();
private Singleton() { }
public static Singleton Instance {
get {
if (instance == null) {
lock (padlock) {
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}
public sealed class Singleton {
private static readonly Singleton instance = new Singleton();
static Singleton() { }
private Singleton() { }
public static Singleton Instance => instance;
}
public sealed class Singleton {
private Singleton() { }
public static Singleton Instance => Nested.instance;
private class Nested {
static Nested() { }
internal static readonly Singleton instance = new Singleton();
}
}
public sealed class Singleton {
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance => lazy.Value;
private Singleton() { }
}
sealed to prevent inheritanceLazy<T> for simplicity and thread safetyThe Singleton Pattern can lead to global state, reduced testability, tight coupling, hidden dependencies, and potential concurrency issues. These challenges can make code harder to understand, maintain, and debug.
| Agile Estimation | Deployment-Strategies | |