Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I have a service .Which call a host of controllers and that in turn call a host of DAOs.These DAos have a lot of class instance created that they use to do there job.
My problem is while trying to implement DI where do i create these objects

Now where do i move creation of all obj types object and similarly for DAOs and Controller.
Thanks I have been realy having a hard time with this

Just to clarify I understand i have to implement Interface and pass object around in those interface.But what will act as my container in this scenario.Where will i do new object creation.

class ServiceDemo{
conlroller1= new controller1();
conlroller2= new controller2();
class controller1{
DAO1= new DAO1();
DAO2= new DAO2();
DAO3= new DAO3();
DAO4= new DAO4();
class DAO1{
obj1= new obj1();
obj2= new obj2();
obj3= new obj3();
obj4= new obj4();
obj5= new obj5();
Updated 8-Dec-14 0:58am

1 solution

Please have a look at the following article:

An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection[^]

This will allow you to learn what DP is about.

Basically DP starts by defining a so-called interface which
defines which methods a class must implement.

Once you have defined an interface you can write classes that implement this interface.

e.g. one fake class for simulating database access
and another one for real access.
Share this answer
anurag.lko 8-Dec-14 6:59am    
Thankis @TheRealSteveJudge.. but my question is more about where should i create object and not about how to implement DI.
I have updated my question.
TheRealSteveJudge 8-Dec-14 7:13am    
You're welcome.
You can create objects where you like.

What about creating them by declaring them in the class which shall use them.

e.g. private IMyClass myClass

and then myClass = new FakeClass() before you access it.
anurag.lko 8-Dec-14 7:19am is my confusion...I have been told by my designer that any where a new keyword is a red flag even with the way you gave in eg.I am not sure if i move almost all new craetions out it would sound very crazy.
TheRealSteveJudge 8-Dec-14 7:29am    
Now I am confused as well...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900