Understand Dependency Injection: Function/Method Injection

In this article we will try to understand another pattern for implementing Dependency Injection called Function Injection.

Welcome to the Dependency Injection article series. In our previous two articles we learned what Dependency Injection is, why it is necessary to implement it in application development and many more. We have learned two patterns to implement Dependency Injection in C#. The first one is using constructor injection and the second one is property injection. You can read about them by visiting the following URLs.

In this article we will try to understand another pattern for implementing Dependency Injection called Function Injection. Before starting with Function Injection we will review the basic idea very quickly. If you are an old reader in this series then you can skip the following paragraph without breaking any continuation.

Dependency Injection is a kind of architectural style that talks about loose coupling among program components. We know that there are many advantages of loose coupling (we have explained that in the first article) and it is very necessary to implement it in applications.

In this example we will implement the same service example as in the previous article and we will try to understand how to implement the same using the Function Injection pattern. The concept is that, there will be one service class and one client class. They want to talk with each other, now if we directly call the method of one class from another class then there will be a tight dependency between them. If we want to change one then the other will be effected. So, the solution is to place one interface between them and they will talk via an interface. The following diagram represents our concept.

consume service via interface

In the picture, we see that the service is being consumed via an interface so there is no direct relation between the Service class and the Consumer class. Now, we will implement the above example in a console application. Have a look at the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
namespace ConsoleApp
{
   
public interface IServiceBroker
    {
       
void Serve();
    } 
   
public class ServiceProvider : IServiceBroker
    {
       
public void Serve()
        {
           
Console.WriteLine("Service Started...");
        }
    } 
   
public class ServiceConsumer
    {
       
private IServiceBroker _service; 
       
public void Start(IServiceBroker s)
        {
           
this._service = s;
           
Console.WriteLine("Service Called...");
           
this._service.Serve();
        }
    }
   
class Program
    {
       
static void Main(string[] args)
        {
           
//Create object of Client class
            ServiceConsumer client = new ServiceConsumer();

            //Call to Start method with proper object.
            client.Start(new ServiceProvider());
           
Console.ReadLine();
        }
    }
}

Let's try to understand the code above. This is one example of Dependency Injection using the Method Injection technique. The main part to understand of the above example is the Start() function within the ServiceConsumer class. This function is taking an argument of the IServiceBroker interface and it's calling the appropriate method by calling the Serve() function.

The following are a few words about method injection:

  • Through method, we are sending a specific object of the interface, so that it's called method injection.
  • It could be useful in those scenarios where an entire class does not need the dependency. Just one method needs the dependency.
  • It is one of the common types of Dependency Injection.

Here is sample output:

method injection

Conclusion

In this article we saw how to implement Dependency Injection using the Method Injection method. Hope you have understood the concept.