DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Dependency Injection - 3 Questions!

Dependency Injection - 3 Questions!

Peter Karussell user avatar by
Peter Karussell
·
Jun. 27, 08 · Interview
Like (0)
Save
Tweet
Share
9.71K Views

Join the DZone community and get the full member experience.

Join For Free

normally the reason why i write blogs is to inform other people about interesting things i found or i am thinking about. today i will ask some questions and wait for your answer ;-)

one of my  latest posts  was about the spring rich client. because of this project and picocontainer i read a lot about design patterns. the most obvious design pattern in spring is called  dependency injection  . this design pattern helps the software architect to loosly couple components. it could work like follows (just for the jse-developers under us):

class classa {
myinterface objecta;
public void setmyinterface(myinterface obja) {objecta = obja;}
}

where we could have defined the interface like this:

interface myinterface{ string tostring(); }

now,  classa  defines a dependency on an instance of  myinterface  and the framework (for java e.g.  picocontainer  ,  spring  ,  juice  or  others  ) will inject an instance of an implementation of  myinterface  into  classa  . it is up to the configuration if  obja  is a newly created instance or if it is a ’singleton’. (myinterface could be a class as well.)

for example if you call

classa classa = framework.getbean(classa.class); //code is similar to picocontainer; not to spring!

you will get back an instance of class  classa  , where  objecta  is not  null  ! the dependency was defined by the method (setmyinterface) - this is called  setter injection  . other kinds of injections are:

  •  annotation-based injection  :

    class classa { @inject object objecta; } 

  •  constructor injection  :
    class classa { object objecta;
    public classa(objecta obja) {objecta = obja;}
    } 

where picocontainer campaigns for  constructor  and spring for  setter injection  (but both projects offer  at least  the mentioned kinds of injection).

it is correct that you can create your own small framework or even set up the objecs by hand to achieve the same: dependency injection. but i guess you are lazy and will choose one of the open source frameworks.

now, all is explained to understand my 3 questions. hopefully someone out there will have an answer!

  1. how should i design a library?
 

let me explain. you want to sell a nice designed, but complex library. the library offers a lot of functionality and you want to split it into loosly coupled components. that means you have to use dependency injection (di), but you don’t know if the customer has or wants such a di-framework.

so, if you use e.g. the  annotation-based injection  it would be nearly impossible for the customer to use your library; with  setter  or  constructor injection  it is quite difficult to set up the objects for using your library.

i don’t know what to do: should i really use dependency injection here? or should i use a (dynamic) service locator instead; to decouple my components within the library?

martin fowler  says  :

 “it (dependency injection) tends to be hard to understand and leads to problems when you are trying to debug. [...] this isn’t to say it’s a bad thing, just that i think it needs to justify itself over the more straightforward alternative (service locator).” 

 “   a common reason people give for preferring dependency injection is that it makes testing easier. the point here is that to do testing, you need to easily replace real service implementations with stubs or mocks. however there is really no difference here between dependency injection and service locator: both are very amenable to stubbing.  i suspect this observation comes from projects where people don’t make the effort to ensure that their service locator can be easily substituted.  “ 

 “so the primary issue is for people who are writing code that expects to be used in applications outside of the control of the writer. in these cases even a minimal assumption about a service locator is a problem.” 

so: what would you do? would you use a service locator or dependency injection inside the library?

and this leads me to the next important question:

  2. why is a singleton an antipattern?  

i stumbled over this question while i used picocontainer. they say you have to avoid singletons, because it is difficult to test and to replace. my question is: why? imagine the following singleton:

class logfactory{
public static logger getlogger(class clazz) { … }
}

now it is really easy to replace the implementation of the logger interface e.g. if one defines a setimpl method:

logfactory.setimpl(loggerfortesting.class);

so what’s soo wrong with a singleton?

  3. how can i avoid making all classes public?  

i don’t know if it is the case for autowiring in spring. but in picocontainer you have to set-up the configuration for the wiring of the classes by yourself e.g. in a separate class (with java; no xml! compiler checks references! yeah!) or in a separate file with a scripting language (or xml) and  nanocontainer  . that means you could specify the implementations of the interface by:

framework.setimpl(myinterface.class, firstimpl.class);

but wouldn’t it mean that i have to add the modifier public to the class  firstimpl  ? (to let picocontainer call the default constructor.) this can’t be good design to allow the user of my library to see the implementations of myinterface.

whats wrong with my use case and my conclusion?


 from    http://karussell.wordpress.com  

Dependency injection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Promises, Thenables, and Lazy-Evaluation: What, Why, How
  • How to Rescue Your Magento 2 Project
  • What Should You Know About Graph Database’s Scalability?
  • Hackerman [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: