Tutorials Design Patterns in C#
Singleton Pattern — Complete Guide
Singleton Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.
On this page
Introduction
Singleton Pattern — Complete Guide is essential for .NET architects building ShopNest Enterprise Architecture Platform — Toolliyo's 69-article design patterns master path covering GoF creational, structural, and behavioral patterns; enterprise patterns (Repository, CQRS, Saga, Outbox); microservices; ASP.NET Core architecture; and senior interview prep. Every article includes minimum two mandatory real-world examples.
In Indian delivery projects (TCS, Infosys, Wipro), interviewers expect singleton with real banking, e-commerce, or SaaS examples — not toy animal demos. This article delivers production depth on Authentication.
After this article you will
- Explain Singleton in plain English and in enterprise architecture terms
- Implement Singleton in ShopNest Enterprise Architecture (Authentication)
- Compare anti-pattern vs production-ready pattern implementation
- Answer fresher and senior design pattern interview questions confidently
- Connect this lesson to Article 2 and the 69-article Design Patterns roadmap
Prerequisites
- Software: .NET 10 SDK, VS 2022 or VS Code, xUnit + Moq
- Knowledge: C# basics, SOLID principles (helpful)
- Previous: None — this starts ShopNest Enterprise Architecture
- Time: 22 min reading + 30–45 min hands-on
Concept deep-dive
Level 1 — Analogy
Singleton is like one airport control tower — every flight coordinates through a single authority.
Level 2 — Technical
Singleton controls object creation on ShopNest — hide construction complexity, enforce single instances, or build complex Authentication aggregates step by step.
Level 3 — Architecture placement
[Client / API Gateway]
▼
[Application Layer — Handlers, Strategies, Commands]
▼
[Domain Layer — Entities, Domain Events, Specifications]
▼
[Infrastructure — EF Core, Message Bus, Polly, Cache]
▼
[Pattern Registration — Program.cs DI lifetimes]
▼
[xUnit + Moq — pattern behavior isolated]
Common misconceptions
❌ MYTH: Every class needs a design pattern.
✅ TRUTH: Patterns solve recurring problems — use judgment; a simple service method beats forcing Abstract Factory on a one-off.
❌ MYTH: GoF patterns are outdated in modern C#.
✅ TRUTH: The concepts persist — DI, MediatR, and Polly are modern implementations of established patterns.
❌ MYTH: More patterns always means better architecture.
✅ TRUTH: Overengineering slows teams — senior developers know when NOT to apply a pattern.
Project structure
ShopNest.EnterpriseArchitecture/
├── ShopNest.Domain/ ← Entities, domain events, interfaces
├── ShopNest.Application/ ← Commands, queries, handlers (MediatR)
├── ShopNest.Infrastructure/ ← EF Core, Redis, RabbitMQ, Polly
├── ShopNest.Api/ ← ASP.NET Core Web API + Minimal APIs
├── ShopNest.Workers/ ← Hosted services, outbox processors
└── ShopNest.Gateway/ ← YARP API Gateway
Hands-on implementation — Authentication
Implement Singleton in C# for Authentication: write a class or method, compile, and verify with a console or unit test.
- Open a console or class library project.
- Implement the concept in a focused class or method.
- Add null checks and meaningful exception messages.
- Run dotnet build and dotnet test.
- Review naming and SOLID boundaries.
Anti-pattern (god class, swallowed exceptions, magic strings)
// ❌ BAD — no pattern, tight coupling, untestable
public class OrderController : ControllerBase {
public IActionResult Place(OrderDto dto) {
var conn = new SqlConnection("Server=.;...");
// direct SQL, no repository, no UoW, no error handling
return Ok();
}
}
Production-style C# code
// ✅ CORRECT — Singleton on ShopNest (Authentication)
public sealed class PlaceOrderHandler(
IOrderRepository repo,
IUnitOfWork uow,
IPublisher events) : IRequestHandler<PlaceOrderCommand, Result<int>>
{
public async Task<Result<int>> Handle(PlaceOrderCommand cmd, CancellationToken ct) {
var order = Order.Create(cmd.CustomerId, cmd.Lines);
await repo.AddAsync(order, ct);
await events.Publish(new OrderPlacedEvent(order.Id), ct);
await uow.SaveChangesAsync(ct);
return Result.Success(order.Id);
}
}
Complete example
public sealed class AppSettingsSingleton {
private static readonly Lazy<AppSettingsSingleton> _instance = new(() => new());
public static AppSettingsSingleton Instance => _instance.Value;
}
// Prefer DI Singleton in ASP.NET Core over static
Real-World Example 1 — Banking Transaction System
MANDATORY: Enterprise-grade Singleton Pattern implementation in a production banking transaction system.
Business requirement
High-volume debit/credit operations require consistent audit trails, idempotent processing, and strict regulatory compliance.
Why Singleton Pattern is needed
Without Singleton Pattern, the Banking Transaction System team at ShopNest faces tight coupling, untestable code, and painful refactors every sprint. Singleton Pattern decouples responsibilities so the Authentication module can evolve independently while meeting scalability and compliance requirements.
Architecture
[Client/API] → [Singleton Pattern Abstraction]
→ [ShopNest.Authentication Service] → [EF Core / Redis / Message Bus]
→ [Downstream: Audit, Notifications, Reporting]
Tech stack: ASP.NET Core 9 Web API, EF Core, SQL Server, Redis cache, Azure Service Bus
Full working code
// REAL-WORLD EXAMPLE 1: Banking Transaction System
// ShopNest Enterprise Architecture — Authentication module
// Pattern: Singleton
namespace ShopNest.Architecture.Authentication;
public interface ISingletonService
{
Task ExecuteAsync(SingletonRequest request, CancellationToken ct = default);
}
public sealed class BankingTransactionSystemSingletonService : ISingletonService
{
private readonly ILogger _logger;
public BankingTransactionSystemSingletonService(ILogger logger)
=> _logger = logger;
public async Task ExecuteAsync(SingletonRequest request, CancellationToken ct)
{
_logger.LogInformation("[Singleton] Processing {Domain} request {Id}",
"Banking Transaction System", request.Id);
// Production implementation — see Program.cs for DI registration
await Task.Delay(10, ct);
return Result.Success(request.Id);
}
}
// Register in Program.cs:
// builder.Services.AddScoped();
Benefits achieved
- Loose coupling — swap implementations without changing controllers
- Unit testable — mock
ISingletonServicein xUnit tests - Scalable — horizontal scaling of Authentication workers under load
- Maintainable — new business rules added via new classes, not if-else chains
Real-World Example 2 — E-Commerce Order Platform
MANDATORY: Second complete example in a different domain — E-Commerce Order Platform.
Business problem
Order placement spans inventory, payment, shipping, and notification services — each must scale independently under flash-sale traffic.
Why Singleton Pattern solves it
In E-Commerce Order Platform, Indian IT delivery teams (TCS, Infosys, Wipro lateral rounds) frequently ask how Singleton Pattern applies to distributed systems. This example shows production-level implementation with ASP.NET Core integration, not toy animal/car demos.
Production implementation
// REAL-WORLD EXAMPLE 2: E-Commerce Order Platform
// ShopNest Enterprise Architecture — Authentication module
// Pattern: Singleton
namespace ShopNest.Architecture.Authentication;
public interface ISingletonService
{
Task ExecuteAsync(SingletonRequest request, CancellationToken ct = default);
}
public sealed class E-CommerceOrderPlatformSingletonService : ISingletonService
{
private readonly ILogger _logger;
public E-CommerceOrderPlatformSingletonService(ILogger logger)
=> _logger = logger;
public async Task ExecuteAsync(SingletonRequest request, CancellationToken ct)
{
_logger.LogInformation("[Singleton] Processing {Domain} request {Id}",
"E-Commerce Order Platform", request.Id);
// Production implementation — see Program.cs for DI registration
await Task.Delay(10, ct);
return Result.Success(request.Id);
}
}
// Register in Program.cs:
// builder.Services.AddScoped();
Scalability benefits
- Supports multi-region deployment on Azure with independent scaling
- Integrates with ShopNest distributed events (RabbitMQ) for async workflows
- Redis caching reduces database load for read-heavy E-Commerce operations
- Polly resilience policies handle transient failures in cloud-native environments
Pattern variations & ASP.NET Core integration
Modern C# 14 uses primary constructors, records, and DI. Register Singleton abstractions in Program.cs with appropriate lifetimes — Singleton for stateless, Scoped for request-bound, Transient for lightweight factories.
Microservices: Apply Singleton within bounded contexts — each ShopNest service (Authentication) owns its implementation.
Pattern comparison & when NOT to use
Compare Singleton with similar patterns. Avoid overengineering — if a simple function or DI registration suffices, do not force a pattern. Senior architects value judgment over pattern count.
Unit testing the pattern
public class SingletonPatternTests
{
[Fact]
public async Task ExecuteAsync_ReturnsSuccess()
{
var mock = new Mock<ISingletonService>();
mock.Setup(s => s.ExecuteAsync(default)).ReturnsAsync(Result.Success());
var result = await mock.Object.ExecuteAsync(default);
Assert.True(result.IsSuccess);
}
}
Pattern recognition
Object creation pain → Creational. Composing subsystems → Structural. Algorithm/communication variation → Behavioral. Persistence/messaging → Enterprise. Multi-service → Cloud patterns. ASP.NET pipeline → Middleware/Options/Hosted Service.
Common errors & fixes
- Singleton with mutable state shared across requests — Use Singleton only for stateless services; keep request state Scoped.
- Factory explosion — new class per trivial variation — Use Strategy or simple DI when behavior differs slightly, not Abstract Factory.
- Repository wrapping every EF call without domain logic — Repository adds value for testability and query composition — not as a pass-through.
- Saga/CQRS on a CRUD app with 3 tables — Start with simple layered architecture; add patterns when complexity demands.
Best practices
- 🟢 Name patterns by problem solved, not GoF catalog page number
- 🟢 Register abstractions in DI — depend on interfaces, not concretions
- 🟡 Match DI lifetime to pattern (Singleton vs Scoped)
- 🟡 Write one xUnit test proving the pattern's core behavior
- 🔴 Do not apply Saga/CQRS/Event Sourcing on simple CRUD
- 🔴 Document when you chose NOT to use a pattern — interviews love this
Interview questions
Fresher level
Q1: What is the Singleton pattern and when would you use it?
A: Singleton solves a specific recurring problem on ShopNest Authentication. Explain intent, structure (participants), and one real example — then state when NOT to use it.
Q2: Singleton vs similar patterns — how do you choose?
A: Compare intent and consequences; e.g. Strategy vs State, Repository vs DAO, Mediator vs Observer — pick by change axis.
Q3: How do design patterns relate to SOLID?
A: Patterns implement SOLID — Strategy/OCP, Repository/DIP, SRP via focused classes. SOLID is why; patterns are how.
Mid / senior level
Q4: Repository pattern — benefits and pitfalls?
A: Benefits: testability, query composition. Pitfalls: leaky abstraction, generic repo anti-pattern, duplicating EF features.
Q5: When would you NOT use a design pattern?
A: Simple CRUD, prototypes, or single-developer utilities — YAGNI until complexity appears.
Q6: How are patterns asked in TCS/Infosys lateral interviews?
A: Scenario-based: "Design payment retry" → Retry + Circuit Breaker; "Split monolith" → Strangler + API Gateway.
Coding round
Implement Singleton for ShopNest Authentication: interface, concrete class, DI registration, and xUnit test with Moq.
builder.Services.AddScoped<ISingletonService, SingletonService>();
public sealed class SingletonService : ISingletonService
{
public Task<Result> ExecuteAsync(CancellationToken ct) => Task.FromResult(Result.Success());
}
Summary & next steps
- Article 1: Singleton Pattern — Complete Guide
- Module: Module 1: Creational Design Patterns · Level: BEGINNER · Type: CREATIONAL
- Applied to ShopNest Enterprise Architecture — Authentication
Previous: None — start of Design Patterns path
Next: Factory Method Pattern — Complete Guide
Practice: Apply today's pattern in one module — commit with feat(patterns): article-01.
FAQ
Q1: What is Singleton?
Singleton helps ShopNest Enterprise Architecture implement Authentication with maintainable, testable C# structure.
Q2: Do I need to memorize all GoF patterns?
No — understand ~15 commonly used ones (Singleton, Factory, Strategy, Observer, Decorator, Repository, CQRS) deeply.
Q3: Is this asked in Indian IT interviews?
Yes — creational/behavioral basics in campus drives; enterprise and microservice patterns in lateral and architect rounds.
Q4: Which .NET version?
Examples target .NET 10 with C# 14, ASP.NET Core DI, MediatR, and Polly.
Q5: How does this fit ShopNest?
Article 1 applies Singleton to Authentication. By Article 69 you architect enterprise systems with sound judgment.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!