• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Wednesday, May 30, 2018

Implementation of Dependency Injection Pattern using Methods in C#

 Abhishek Tomer     May 30, 2018     .Net, C#, Interview Question, Programming     No comments   

Hi friends! Today we will try to understand another pattern for implementing Dependency Injection called Function Injection. In our last session, we have learned about Dependency Injection using Constructor.

Before starting with Function Injection, let us recall what we have learned in our last session.

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 and it is very necessary to implement it in applications.

In this lesson, we will implement 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 affected. So, the solution is to place one interface between them and they will talk via an interface. The following diagram represents our concept.
Implementation of Dependency Injection Pattern using Methods in C#
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.

Let's understand with the help of a program.

using System;

namespace ConsoleApp
{
    public interface IServiceInterface
    {
        void Start();
    }
    public class ServiceClass : IServiceInterface
    {
        public void Start()
        {
            Console.WriteLine("Service Started...");
        }
    }
    public class ServiceConsumer
    {
        private IServiceInterface _service;
        public void Start(IServiceInterface s)
        {
            this._service = s;
            Console.WriteLine("Service Called...");
            this._service.Start();
        }
    }
    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 ServiceClass());
            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 IServiceInterface and it's calling the appropriate method by calling the Start() function.

The following are few points about method injection:
  • Through the 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 the Output:
Implementation of Dependency Injection Pattern using Methods in C#
Output of the Method Injection approach
Summary:
So, Guys, this is all about Function Injection.
I Hope in this post covered all the points about Function/Method Injection which will be helpful to understand the concept Dependency Injection in C#.

Please share this post with your friends and colleagues.
For any queries please post a comment below.

Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape