1595757720
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.
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.
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.
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
1596757307
#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #delegates in c#
1596757525
#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #single delegates in c#
1597803199
#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #predicate delegates in c#
1597629700
#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #action delegates example in c#
1596758949
#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #action generic delegates in c#