dependency-injection A Very Simple Example of Property Injection with C# with a Lazy-loaded Local Default


Example

using System;

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

            foo.DoSomething();

            var bar = new InjectedDependency();

            foo.Dependency = bar; //Injecting the dependency via a property

            foo.DoSomething();

            Console.ReadLine();
        }

    }

    public interface IDependency
    {
        void DoSomething();
    }

    public class DefaultDependency: IDependency
    {
        public void DoSomething()
        {
            Console.WriteLine("Default behaviour");
        }
    }

    public class InjectedDependency: IDependency
    {
        public void DoSomething()
        {
            Console.WriteLine("Different behaviour");
        }
    }

    public class ClassWithDependency
    {
        private IDependency _dependency;

        public IDependency Dependency
        {
            get
            {
                if (_dependency == null) Dependency = new DefaultDependency();
                return _dependency;
            }
            set { _dependency = value; }
        }

        public void DoSomething()
        {
            Dependency.DoSomething();
        }
    }
}