• 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

  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • Implementing basic authentication in ASP NET Web API
    Overview: In this post I am going to explain to how to implement basic authentication using custom authentication filter. Here am not using...
  • Introduction to ASP.NET Core Framework
    In this article, I am going to give you a brief introduction to ASP.NET Core Framework . Nowadays, when it comes to software development, e...
  • Kestrel Web Server in ASP.NET Core
    In this article, I am going to discuss the Kestrel Web Server in ASP.NET Core Application. Please read our previous article before proceed...
  • Blazor Project Structure
    In this article, I am going to discuss Blazor Project Structure in detail. Please read our previous article, where we discussed the Blazor...
  • Environment Tag Helper in ASP.NET Core
    In this article, I am going to discuss the Environment Tag Helper in ASP.NET Core Application with some examples. Please read our previous...

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