Scenario for DI (Help needed)

Aug 31, 2011 at 6:10 PM



I am very new to DI and Unity block. I've read alot about DI and IOC pattern but I've never used it in any of my application.  Now I'm starting to develop a new web based application using MVC. But I'm not sure if I should use DI in my Business Layer or not. Let me explain my scnario with a small example of my application. I have a class called Customer and another class called Address. I've a function in customer class responsible to population Address object and saving it into database.


Class Customer


void SaveCustomerAddress()


Address address = new Address();

CustomerDB db = new CustomerDB();

address.street = "abcd";

address.houseNo = "xyz"; //and there are other fileds i need to populate in address





Now my Customer class is dependent on Adress and CustomerDB class and I understand that I can use DI to inject those object from outside. But my question is what is the advantage of doing that? If I know that to SaveCustomerAddress, I always need and object of Address class so why do I depend on some external source to provide me with that object?


Now I've another Scenario where I need to call database depending on the database provider (oralce, sqlserver etc). There I am confident that I should use DI as I'm not sure what type of provider (oracle or sqlserver) i would be using so it's better if I inject it from outside. But in my 1st Scenario, when I always know what object i want, y do i make my application more complex??


I would really appreciate any help on this.



TK Lee

Sep 1, 2011 at 11:35 AM


It seems that your example is not really "Dependency Injectable" since it already creates the Address object inside. I'm finding ways to explain it better but I hope this blog would provide you with some more ideas.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us