Singleton (Thread Safe)
private static readonly Lazy<DbConnectionThreadSafe> _instance = null!;
  
  
  Using Lazy<T> Because
- Thread safe by default
 - Provides lazy initialization
 
static DbConnectionThreadSafe()
{
    _instance = new Lazy<DbConnectionThreadSafe>(
        () => new DbConnectionThreadSafe(_connectionString),
        LazyThreadSafetyMode.ExecutionAndPublication
    );
}
Let's break down this static constructor and the Lazy initialization:
static DbConnection()  // This is a static constructor
{
    _lazyInstance = new Lazy<DbConnection>(
        () => new DbConnection(_connectionString),  // Lambda expression
        LazyThreadSafetyMode.ExecutionAndPublication  // Thread safety mode
    );
}
Let's examine each part:
- 
Static Constructor (
static DbConnection())- A static constructor is called automatically before any static members are referenced
 - It runs only once per app domain
 - It's thread-safe by default (CLR guarantees this)
 - Used to initialize static fields
 
 - 
Lazy
- 
Lazy<T>is a class that provides lazy initialization - The actual object isn't created until it's first accessed
 - When you access 
Lazy<T>.Valuefor the first time, it creates the instance 
 - 
 - 
Lambda Expression (
() => new DbConnection(_connectionString))- This is a factory function that creates the DbConnection instance
 - It's only executed when the Lazy.Value is first accessed
 - The 
=>syntax defines a lambda expression (anonymous function) 
 - 
LazyThreadSafetyMode.ExecutionAndPublication
- This enum value specifies how the Lazy instance handles thread safety
 - Three possible modes:
 
 
// No thread safety
LazyThreadSafetyMode.None
// Locks initialization
LazyThreadSafetyMode.ExecutionAndPublication
// Multiple threads can attempt initialization
LazyThreadSafetyMode.PublicationOnly
The execution flow:
- When the class is first referenced, the static constructor runs
 - The 
_lazyInstanceis created, but the DbConnection instance is not yet created - When 
Instanceis first accessed:- The lambda expression runs
 - Creates new DbConnection with the connection string
 - Stores it in the Lazy instance
 
 - Subsequent accesses to 
Instancereturn the same stored instance 
    
Top comments (0)