Tia  Gottlieb

Tia Gottlieb

1595757720

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.

You could argue that this is a code smell because the interface doesn’t group methods that would commonly go together. So why create an interface? The answer is usually “because that’s how IoC containers work” or “so you can mock the interface for unit testing”. Both these answers are wrong because delegates offer an alternative way of performing dependency injection without having to create an interface. You can mock delegates, use them in IoC containers, and verify that they get called. It’s actually more straightforward when you use a delegate instead of an interface with one method.

Lastly, you could argue that a delegate has a fixed number of inputs and outputs, while a class can have an unlimited number of dependencies injected into the constructor. This is true, but the latter part of this article demonstrates how to register a service by a delegate that takes an unlimited number of dependencies.

The Basics

Take a look at this interface. In the real world, we need to name the interface, the method, mock the method, and implement the class.

But, there is a more straightforward way with a delegate.

At this point, you’re probably thinking, “What about IoC containers?”. Don’t they need dependencies as interfaces? The answer is no. Delegates work just as well.

Dependency Injection with Delegates

Delegates are reference types in the same way that interfaces are reference types. You can pass any reference type object into a class as a dependency via the constructor. Passing a delegate implementation into a class via the constructor is the same as interface implementations. Notice in this code that when we call the delegate, we only need to supply the variable name. We do not need an unnecessary method name. It’s convenient syntax sugar.

#software #c# #function

What is GEEK

Buddha Community

C# Delegates with IoC Containers and Dependency Injection

Delegates in C# | Delegates and events | what is a delegates | C# Tutorial | Advanced C#

https://youtu.be/72C6R_BkwCc

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #delegates in c#

Single delegates in C# | Delegates and events | C# Bangla Tutorial | Advanced C#

https://youtu.be/lv2VvZJJr0U

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #single delegates in c#

Predicate delegate in C# | Generic Delegate In C# | | C# Bangla Tutorial | Advanced C#

https://youtu.be/iULLuVpqrGQ

#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #predicate delegates in c#

Action generic delegates in C# | Generic delegates in C# | C# Bangla Tutorial |Advanced C#

https://youtu.be/hXkztwCUZz4

#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #action generic delegates in c#

Action delegates example in C# | Generic delegates in C# | C# Bangla Tutorial |Advanced C#

https://youtu.be/9j1NvBxiVzc

#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #action delegates example in c#