Dependency Injection - hello world? C#

Can anyone create a simple "hello world" example of Dependency Injection?  Please use C#.


I have a pair of web references.  Depending on factors I won't know until runtime...I will be using one or the other.

Is that enough information?
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Dependency Injection is just a fancy term for parameters...suffice it to say. Anywhere you see "Blah x = new Blah()" in your code is potential for leveraging DI. As an example:

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            Remote remote = new Remote();

            remote.TurnOnAppliance();
        }
    }

    class Remote
    {
        public void TurnOnAppliance()
        {
            Light appliance = new Light();

            appliance.TurnOn();
        }
    }

    class Light
    {
        public void TurnOn()
        {
            // Turn on the light
        }
    }
}

Open in new window


The Remote class above has a dependency on the Light class. You know this because you can see a "Blah x = new Blah()" in line 17. What if you wanted to use the Remote for something else? Well, right now you can't because it's hard-coded to turn on lights. What if we could tell the Remote to turn on any kind of appliance? How about a Television:

class Television
{
    public void TurnOn()
    {
        // Turn on the T.V.
    }
}

Open in new window


What if we modified the Remote class to extract the dependencies? Say:

class Remote
{
    public void TurnOnAppliance(IAppliance appliance)
    {
        appliance.TurnOn();
    }
}

interface IAppliance
{
    void TurnOn();
}

Open in new window


Now I've created a new interface that declares the TurnOn method. I also modified the TurnOnAppliance method to accept objects that implement this interface. So, how might an appliance implement this interface?

class Light : IAppliance
{
    public void TurnOn()
    {
        // Turn on the light
    }
}

class Television : IAppliance
{
    public void TurnOn()
    {
        // Turn on the T.V.
    }
}

Open in new window


I cheated a little:  Both classes already had the TurnOn method. Nevertheless, now they both implement the IAppliance interface. Now I can inject appliances into my remote:

class Program
{
    static void Main(string[] args)
    {
        Remote remote = new Remote();
        Light light = new Light();
        Television tv = new Television();

        remote.TurnOnAppliance(light);
        remote.TurnOnAppliance(tv);
    }
}

Open in new window


I made the dependencies external to the Remote class, and such dependencies now have to be passed in (as a parameter). Now my Remote class doesn't have to change if I get new appliances. I only have to ensure that each appliance that I want the Remote to interact with implements the IAppliance interface.

The type of DI I show above is called "parameter injection". There is also "constructor injection"--same as parameter injection, but passed to the constructor rather than a method--and "property injection", where you assign the dependency to a property of the dependent class. The latter is the least-favorable approach of the three.
0
Mike EghtebasDatabase and Application DeveloperCommented:
kaufmed,

I have been following and reading about this topic since this morning. Your explanation is so simple and effective that even I get to understand it (FYI, I am new to .net).

Thank you for the generous post.

Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
I agree with eghtebas, kaufmed.  : )

You've de-mystified DI for me.  Great job on the example.

Tom
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.