Click here to Skip to main content
15,886,720 members
Articles / Programming Languages / C#

Inversion of Control: Overview with Examples

Rate me:
Please Sign up or sign in to vote.
4.70/5 (27 votes)
8 May 2012CPOL7 min read 126.3K   955   52  
This article discusses the basic concepts of IoC, how to achieve it, and Dependency Injections.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IoCSample
{
    public class IoCClass
    {
        ILogger logger = null;

        public string FileName { get; set; }

        public void WriteLogUsingConstructorInjection(string message)
        {
            logger = new FileLogger(FileName);

            Constructor_Dependency_Injection.LoggingEngine cEngine = 
                new Constructor_Dependency_Injection.LoggingEngine(logger);
            cEngine.Log(message);
            cEngine = null;

            logger = new ConsoleLogger();

            cEngine = new Constructor_Dependency_Injection.LoggingEngine(logger);
            cEngine.Log(message);
            cEngine = null;
        }

        public void WriteLogUsingSetterInjection(string message)
        {
            logger = new FileLogger(FileName);

            Setter_Dependency_Injection.LoggerEngine sEngine = 
                new Setter_Dependency_Injection.LoggerEngine();

            sEngine.Logger = logger;
            sEngine.Log(message);

            logger = new ConsoleLogger();

            sEngine.Logger = logger;
            sEngine.Log(message);

            sEngine = null;
        }

        public void WriteLogUsingInterfaceInjection(string message)
        {
            Interface_Dependency_Injection.LoggerEngine iEngine = 
                new Interface_Dependency_Injection.LoggerEngine();

            Interface_Dependency_Injection.ILoggerInject loggerInject = 
                new Interface_Dependency_Injection.FileLoggerInject() { FileName = FileName };

            iEngine.Log(loggerInject, message);
            loggerInject = null;

            loggerInject = new Interface_Dependency_Injection.ConsoleLoggerInject();

            iEngine.Log(loggerInject, message);
            loggerInject = null;

            iEngine = null;
        }

        public void WriteLogUsingServiceInjection(string message)
        {
            Service_Dependency_Injection.LoggerEngine slEngine = 
                new Service_Dependency_Injection.LoggerEngine();

            Service_Dependency_Injection.LoggerService.Logger = new FileLogger(FileName);
            slEngine.Log(message);

            Service_Dependency_Injection.LoggerService.Logger = new ConsoleLogger();
            slEngine.Log(message);

            slEngine = null;
        }

        public void WriteLogUsingGenericTypeInjection(string message)
        {
            Generic_Dependency_Injection.LoggerEngine gEngine = 
                new Generic_Dependency_Injection.LoggerEngine();
            gEngine.Log<FileLogger>(message);

            gEngine.Log<ConsoleLogger>(message);
            gEngine = null;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Automation Anywhere Inc.
India India
I am Himanshu Manjarawala, Garduate in Computer Science and MCA From Veer Narmad South Gujarat University, Surat Gijarat India. Currently working as Sr. Software Developer in Automation Anywhere Softwares Pvt. Ltd. Vadodara, Gujarat

Comments and Discussions