Ranjithkumar October 22, 2023 0

Lookup DI services using keys in .NET 8

One of the new features in .NET 8 is the ability to dynamically lookup DI services using keys. This can be a useful way to decouple your code from the concrete implementation of a service. For example, let’s say you have a service that sends notifications. You could have a concrete implementation for each type of notification, such as EmailNotificationService and SmsNotificationService. Here is an example of how to register keyed DI services in ASP.NET Core: In the past, you would have to inject the specific service you needed into your constructor. For example, if you wanted to send an…

Ranjithkumar October 18, 2023 0

Do not resolve dependencies manually in ASP.NET Core Unit Testing

Dependency Injection (DI) is a core concept in ASP.NET Core, and it’s not just for production code. You should also leverage DI in your unit tests. Instead of manually resolving dependencies, using ServiceCollection to handle this is the recommended way. In this blog post, we’ll delve into why you should avoid manually resolving dependencies in unit testing and how to utilize ServiceCollection for a cleaner and more maintainable testing setup. The Pitfall of Manual Dependency Resolution Before we explore the right way to handle dependencies in unit tests, let’s discuss a common pitfall: manually resolving dependencies. I’ve seen people manually…