Companion Repository for the Medium Article: > Robust Strategy Pattern in .NET 10: Building a Fail-Safe Tax Engine
Robust Strategy Pattern in .NET 10: Building a Fail-Safe Tax Engine This repository demonstrates a production-grade implementation of the Strategy Design Pattern using .NET 10. The project focuses on high encapsulation, fail-safe logic, and Clean Architecture principles to build a maintainable Tax Calculation Engine. This codebase is part of a technical deep-dive. For a full explanation of the theory and implementation details, check out the article: "Robust Strategy Pattern in .NET 10: Building a Fail-Safe Tax Engine."
Most implementations of the Strategy Pattern suffer from "Leaky Abstractions," where the consuming layer knows too much about the implementation details. This project introduces the "Internal Shield"—a design where concrete strategies are hidden from the outer layers, enforcing a strict dependency on interfaces.
The solution is divided into three distinct projects to ensure a clean separation of concerns:
-
StrategyDesignPattern.Core: The "Engine Room." Contains all business logic, interfaces, and the Strategy Locator.
-
StrategyDesignPattern.ConsoleApp: The "Presentation Layer." A lightweight client that interacts only with public interfaces.
-
StrategyDesignPattern.TestProject: The "Quality Control." Unit tests that verify the mathematical correctness of each strategy.
-
The Internal Shield All concrete tax strategies (Daily, Monthly, Yearly) are marked as internal. This prevents the Console Application from manually instantiating them, ensuring that the Dependency Inversion Principle is strictly followed.
-
Strategic Locator (Fail-Safe) Instead of a giant switch statement in the business logic, we use a TaxCalculatorLocator. It acts as a smart factory that picks the correct strategy at runtime. If an invalid strategy is requested, the system is designed to handle it gracefully without crashing.
-
Centralized Configuration We use a public configuration gateway to wire up our internal dependencies:
public static void RegisterTaxCalculation(ServiceCollection services) { services.AddSingleton<ITaxCalculatorStrategy, DailyTaxCalculation>(); services.AddSingleton<ITaxCalculatorStrategy, MonthlyTaxCalculation>(); // ... services.AddSingleton<ISalaryCalculator, SalaryCalculator>(); }
Solution 'Robust.Strategy.Design.Pattern'
├── 🖥️ StrategyDesignPattern.ConsoleApp (Entry Point)
│ └── Program.cs
├── 🛡️ StrategyDesignPattern.Core (The "Engine Room")
│ ├── ⚙️ Configurations
│ │ └── DependencyConfigurations.cs (DI Registry)
│ ├── 🏷️ Enums
│ │ └── SalaryType.cs
│ ├── 🧮 TaxCalculation
│ │ ├── ITaxCalculatorStrategy.cs (Internal)
│ │ ├── TaxCalculatorLocator.cs (Internal)
│ │ ├── DailyTaxCalculation.cs (Internal)
│ │ ├── MonthlyTaxCalculation.cs (Internal)
│ │ └── YearlyTaxCalculation.cs (Internal)
│ ├── 📄 ISalaryCalculator.cs (Public Interface)
│ └── 📄 SalaryCalculator.cs (Internal Implementation)
└── 🧪 StrategyDesignPattern.TestProject (Unit Tests)
└── StrategyTests.cs
Prerequisites
.NET 10 SDK ### Installation
- Clone the repository:
git clone https://github.com/your-username/Robust.Strategy.Design.Pattern.git
- Navigate to the solution folder:
cd Robust.Strategy.Design.Pattern - Restore dependencies:
dotnet restore
- Running the App:
dotnet run --project StrategyDesignPattern.ConsoleApp
- Running Tests:
dotnet test
Contributions are welcome! Please ensure that any PR maintains the "Green" status of the Architecture Tests.
Aman Toumaj Senior Software Developer & Architecture Enthusiast