Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dependency Injection - hello world?  C#

Posted on 2014-08-07
3
Medium Priority
?
391 Views
Last Modified: 2014-08-08
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?
0
Comment
Question by:Tom Knowlton
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1800 total points
ID: 40247792
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
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 200 total points
ID: 40247994
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
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 40248759
I agree with eghtebas, kaufmed.  : )

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

Tom
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question