Posts

Showing posts from September, 2025

DotNET Best Practices Every Developer Should Know

1. Use Dependency Injection (DI) Properly Why:  Keeps your code loosely coupled and testable. Without DI, you’re tightly coupling your logic, making your code rigid and hard to test. Bad: var service = new MyService(); Good: public class MyController { private readonly IMyService _service; public MyController ( IMyService service ) { _service = service; } } 🔍  My experience:  I once joined a project that had no DI — every service was created with  new . We couldn’t mock anything for tests. Moving to DI not only made things testable, but helped separate concerns much better. 2. Write Unit Tests — Even for Business Logic Why:  You’ll catch bugs early and protect against regressions. Focus especially on core business rules. Use:  xUnit, Moq, or NSubstitute. [ Fact ] public void Should_Return_True_When_Valid () { var validator = new OrderValidator(); var result = validator.IsValid( "ORD123" ); Assert.True(result)...