dependency-injection

Method injection

A Simple Example of Method Injection in c#

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var foo = new Dependency();
            var bar = new ClassWithDependency();

            bar.DoSomething(foo); //Inject dependency as method parameter

            Console.ReadLine();
        }
    }

    public interface IDependency
    {
        void DoSomething();
    }

    public class Dependency: IDependency
    {
        public void DoSomething()
        {
            Console.WriteLine("Hello");
        }
    }

    public class ClassWithDependency
    {
        public void DoSomething(IDependency dependency)
        {
            dependency.DoSomething();
        }
    }
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow