This .NET 8.0 solution demonstrates defensive coding practices across three projects: a main console application, a simulated third-party assembly, and an xUnit test suite. The project showcases differentiated exception handling between critical and non-critical operations, retry logic with configurable retry counts for transient failures, proper null handling with nullable reference types, and async/await best practices with ConfigureAwait(false) and CancellationToken support throughout the call chain.
Key patterns include GetAuthTokenAsync() which immediately rethrows UnauthorizedAccessException without retries but retries on other exception types, DoNiceToHaveWorkWithAuth() for graceful failure on non-critical operations, and DoCriticalWorkWithAuth() for operations where exceptions must propagate. The unit test project uses xUnit and Moq with MockRepository.VerifyAll() for strict verification, covering both success and failure paths with parameterized tests using MemberData.
- .NET 8.0 SDK or later
cd LetsGetDefensive/LetsGetDefensive
dotnet runIf you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:
Subscribe to Dev Leader Weekly
- All Links
- Website - Dev Leader
- YouTube - Dev Leader
- YouTube - Dev Leader Path To Tech
- YouTube - Dev Leader Podcast
- YouTube - CodeCommute
- Newsletter - Dev Leader Weekly
- LinkedIn - Nick Cosentino
- GitHub - ncosentino
- Twitter/X - Dev Leader
- Threads - Dev Leader
- Bluesky - Dev Leader
- Mastodon - Dev Leader
- Facebook - Dev Leader
- TikTok - Dev Leader
- Twitch - Dev Leader
- Stack Overflow - Nick Cosentino
Powered by BrandGhost 👻