728x90 AdSpace

  • Latest News

    Tuesday 28 March 2017

    [Tips] What is Dependency Injection, Dependency injection example in CSharp

    Introduction

    The dependency injection pattern one of the most popular design paradigms today. It is process of removing dependency of object which creates the independent business objects. It is very useful for Test Driven Development.

    This example show you how to write log file with C# dependency injections pattern


    Take a look this example. First we need to create one employee class create a constructor objects, next we need to create ILogger interface with writeToLog method. Finally create two LoggerOne and LoggerTwo implement ILogger to modified writeToLog method.


    What is Dependency Injection, Dependency injection example in CSharp







    Code demo:

    We have one Interface and three classes:
    - Interface: ILogger.cs
    - Three Classes: Employee, LoggerOne.cs, LoggerTwo.cs

    Interface: ILogger.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DependencyInjection
    {
    public interface ILogger
    {
    void writeToLog(string text);
    }
    }



    Three Classes:


    Employee.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DependencyInjection
    {
    public class Employee
    {
    public Employee(ILogger logger)
    {
    logger.writeToLog("New employee created");
    }
    }
    }


    LoggerOne.cs implement from ILogger.cs


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DependencyInjection
    {
    class LoggerOne : ILogger
    {
    public void writeToLog(string text)
    {
    Console.WriteLine(text);
    }
    }
    }


    LoggerTwo.cs implement from ILogger.cs


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DependencyInjection
    {
    class LoggerTwo : ILogger
    {
    public void writeToLog(string text)
    {
    Console.WriteLine("*************** \n {0} \n ***************" + text);
    }
    }
    }


    Main Program:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DependencyInjection
    {
    class Program
    {
    static void Main(string[] args)
    {
    Employee employee1 = new Employee(new LoggerOne());
    Employee employee2 = new Employee(new LoggerTwo());
    Console.ReadLine();
    }
    }
    }


    And the output:

    New employee created
    *******************
    New employee created
    *******************


    If you have any feedback or questions, leave your comment, we can discuss about it!
    Kind Regards!

    Zidane




    • Blogger Comments
    • Facebook Comments

    0 comments:

    Post a Comment

    Item Reviewed: [Tips] What is Dependency Injection, Dependency injection example in CSharp Rating: 5 Reviewed By: Unknown
    Scroll to Top