1

Ok I am just learning DI. I was going through few pointers in net for understanding it. Then I have made a very small real time scenario just to understand it myself.

I am applying DI constructor injection pattern to compute the discount of a product price for different types of customers. If its an employee, then there is separate discount and if its a normal buyer then it is going to be a separate discount. I don't know if this is appropriate to apply DI in this context or any other better solution/patterns is available. I just compiled the code and I am happy it runs. However, I am not confident about the correctness of this. And would appreciate any corrections on this program as in fine tuning, and or suggestions like better approaches. Also, is it what is really a DI? If this is what is called dependency injection, aren't we hardcoding the class in the static main method? Is it right what am I doing and is it what we do in realtime scenario? It will also help others like me.

class Program
{
    public interface IDiscount
    {
        void Discount(int amount);
    }
    public class ApplyDiscount : IDiscount
    {
        public void Discount(int amount)
        {
            Console.WriteLine("Normal Discount calculated is {0}", amount);
        }
    }
    public class ApplyEmployeeDiscount : IDiscount
    {
        public void Discount(int amount)
        {
            Console.WriteLine("Employee Discount calculated is {0}", amount);               
        }
    }
    public class Compute
    {
        public readonly IDiscount discount;

        public Compute(IDiscount discount)
        {
            this.discount = discount;
        }

        public void ComputeTotal(int productAmount)
        {
            this.discount.Discount(productAmount);
        }
    }
    static void Main()
    {
        IDiscount regularDiscount = new ApplyDiscount();
        IDiscount employeeDiscount = new ApplyEmployeeDiscount();
        Compute c = new Compute(employeeDiscount);
        c.ComputeTotal(200);
        Console.ReadLine();
    }
}
Ahmad
  • 22,657
  • 9
  • 52
  • 84
Jasmine
  • 5,186
  • 16
  • 62
  • 114

1 Answers1

2

I'll split this into 3 parts:

  1. Yes/No answer, yes, you injected a dependency.

  2. Here's a short blog that will clarify what dependency injection is in a simple (and short) way: Blog by James Shore.

  3. The heavy guns: article by Martin Fowler. Not much to add to this (mind you, long deep article)

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • Noctis, thank you so much, yay, I injected DI. Well is it an appropriate place that I used dependency? Well also, please tell me why we hardcode in the Main method? – Jasmine Nov 09 '13 at 06:17
  • 1
    I'll take it you stopped after point **1.**. Now, go read post **2.** and come back. In the meanwhile, it's in the main because you put it there. Some frameworks will let you define them in a different place, and inject the proper classes/interfaces depending on different scenarios. Have a look at [ninject](http://www.ninject.org/), cool little DI framework. – Noctis Nov 09 '13 at 06:22
  • Noctis, thank you so much, well I will read all three ponters you gave me surely now itself :) :) – Jasmine Nov 09 '13 at 06:36
  • Did you read it? Also, if this answered your question, you can mark it as answered :) (and I've seen in your other questions you're already familiar with ninject ...) – Noctis Nov 11 '13 at 05:03
  • Noctis, well I just read James blog but the other one I am just reading. I will update you here. Meanwhile I was caught up with something else :( Well Ninject is interesting thing, but dont know how to use it. I am seeing their website. Let me update you my understandings and also seek your help for understanding it. Thank you again :) Sorry, as I haven't completed reading on it, I didn't mark it. Of course this answer helped me a lot. I will keep you noted further soon today. – Jasmine Nov 11 '13 at 07:00