Last active
November 26, 2025 05:50
-
-
Save devops-school/d32c5096a5ecad277395fb4e93dd7440 to your computer and use it in GitHub Desktop.
Circular Dependency in DOT NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ============================================================ | |
| // Circular Dependency Example (BAD) + Fixed Example (GOOD) | |
| // Single-file demonstration for training | |
| // ============================================================ | |
| using System; | |
| using Microsoft.Extensions.DependencyInjection; | |
| namespace CircularDependencyDemo | |
| { | |
| // =============================== | |
| // ❌ BAD EXAMPLE: Circular Dependency | |
| // =============================== | |
| public interface IAService | |
| { | |
| void DoA(); | |
| } | |
| public interface IBService | |
| { | |
| void DoB(); | |
| } | |
| public class AService : IAService | |
| { | |
| private readonly IBService _b; | |
| public AService(IBService b) | |
| { | |
| _b = b; | |
| } | |
| public void DoA() | |
| { | |
| Console.WriteLine("AService running..."); | |
| _b.DoB(); | |
| } | |
| } | |
| public class BService : IBService | |
| { | |
| private readonly IAService _a; | |
| public BService(IAService a) | |
| { | |
| _a = a; | |
| } | |
| public void DoB() | |
| { | |
| Console.WriteLine("BService running..."); | |
| _a.DoA(); | |
| } | |
| } | |
| // =============================== | |
| // ✔ GOOD EXAMPLE: Fixed (Shared Dependency) | |
| // =============================== | |
| public interface ISharedLogic | |
| { | |
| void Log(string msg); | |
| } | |
| public class SharedLogic : ISharedLogic | |
| { | |
| public void Log(string msg) | |
| { | |
| Console.WriteLine($"[SharedLogic] {msg}"); | |
| } | |
| } | |
| public class AServiceFixed | |
| { | |
| private readonly ISharedLogic _shared; | |
| public AServiceFixed(ISharedLogic shared) => _shared = shared; | |
| public void DoSomething() => _shared.Log("AServiceFixed running..."); | |
| } | |
| public class BServiceFixed | |
| { | |
| private readonly ISharedLogic _shared; | |
| public BServiceFixed(ISharedLogic shared) => _shared = shared; | |
| public void DoSomething() => _shared.Log("BServiceFixed running..."); | |
| } | |
| // =============================== | |
| // Main Program | |
| // =============================== | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Console.WriteLine("=== Circular Dependency Demo ==="); | |
| var services = new ServiceCollection(); | |
| // Register BAD circular dependencies | |
| services.AddScoped<IAService, AService>(); | |
| services.AddScoped<IBService, BService>(); | |
| // Register GOOD implementations | |
| services.AddScoped<ISharedLogic, SharedLogic>(); | |
| services.AddScoped<AServiceFixed>(); | |
| services.AddScoped<BServiceFixed>(); | |
| var provider = services.BuildServiceProvider(); | |
| // ------------------------------- | |
| // 1. Demonstrate Circular Dependency Failure | |
| // ------------------------------- | |
| try | |
| { | |
| Console.WriteLine("\n--- BAD Example: Expect circular dependency error ---"); | |
| var a = provider.GetRequiredService<IAService>(); // Will crash | |
| a.DoA(); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine("❌ Circular Dependency Detected:"); | |
| Console.WriteLine(ex.Message); | |
| } | |
| // ------------------------------- | |
| // 2. Demonstrate FIXED version | |
| // ------------------------------- | |
| Console.WriteLine("\n--- GOOD Example: No circular dependency ---"); | |
| var aFixed = provider.GetRequiredService<AServiceFixed>(); | |
| var bFixed = provider.GetRequiredService<BServiceFixed>(); | |
| aFixed.DoSomething(); | |
| bFixed.DoSomething(); | |
| Console.WriteLine("\n✓ Demo completed."); | |
| } | |
| } | |
| } |
devops-school
commented
Nov 26, 2025
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment