• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dependency Injection

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

I've got to ask ye this so as I can put the record straight. In my business service class instead of doing:

BusinessService(DAODatabase database)

what I've done instead is

BusinessService(String Connection)

where the string connection is currently a file location but in the future can be an sql statement. Using the parameter, it calls on the scheme to get the DAO. Would it be correct to say that my constructor has dependency injection?

Thanks ranchers

Kevin
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my understanding your constructor doesn't use dependency injection.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Kevin!

Would it be correct to say that my constructor has dependency injection?



Hum... no, like my good buddy Roel showed above.

Other than that, the second approach you showed is very inflexible. If the data source changes in the future, then you'll have to open your BusinessService class to modify your code so it works correctly. Today, we have to deal with a .db file, but tomorrow, it could be a database. So, the first approach is much better.

If you have a factory or your Data class is a singleton, then you can obtain it, for instance, in the constructor of your BusinessService class. But receiving a String in the business component is not a good idea.
reply
    Bookmark Topic Watch Topic
  • New Topic