Method injection

Other topics

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();
        }
    }
}

Contributors

Topic Id: 6638

Example Ids: 22614

This site is not affiliated with any of the contributors.