.NET Blog

Professional .NET development tips and best practices

C# Delegates with IoC Containers and Dependency Injection

C# Delegates with IoC Containers and Dependency Injection

Developers are usually encouraged to do dependency injection with interfaces. Some developers don’t know that they can do dependency injection with delegates, and there are good reasons to do this. Moreover, developers can use delegates with modern IoC containers like ASP.NET Core’s IoC container, mock delegates, and verify calls. It is good practice and should be encouraged. Let’s have a look at why. All source code for this article can be found in this repo. Why use Delegates instead of Interfaces? Firstly, let’s focus on the interface segregation principle. It’s one of the SOLID principles. It essentially means that you should minimize the number of members on an interface. It sometimes leads to an interesting situation where interfaces end up with only one method. The classic case is factory interfaces. Here is an example.  HttpClient CreateClient(stringname); You could argue that...
Christian Findlay Christian Findlay May 16, 2020
System.Text.Json Rest Client

System.Text.Json Rest Client

The System.Text.Json namespace provides high-performance, low-allocating, and standards-compliant capabilities to process JavaScript Object Notation (JSON), which includes serializing objects to JSON text and deserializing JSON...
Christian Findlay Christian Findlay Jan 21, 2020