Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:37:36+00:00 2026-05-30T12:37:36+00:00

I feel like using Dependency Injection is changing the way I write my object

  • 0

I feel like using Dependency Injection is changing the way I write my object oriented code. For instance, below is what I would do without DI

Interface: DataAdapter
SqliteDataAdapter implements DataAdapter
XMLDataAdapter    implements DataAdapter
OracleDataAdapter implements DataAdapter

// Initialization
DataAdapter adapter = new SqliteDataAdapter();
DataAdapter adapter = new XMLDataAdapter();
DataAdapter adapter = new OracleDataAdapter();

but using DI my code structure would be:

Interface: DataAdapter
SqliteDataAdapter implements ISqliteDataAdapter,  DataAdapter
XMLDataAdapter    implements IXMLDataAdapter, DataAdapter
OracleDataAdapter implements IOracleDataAdapter, DataAdapter

// Initialization
ISqliteDataAdapter adapter = new SqliteDataAdapter();
IXMLDataAdapter    adapter = new XMLDataAdapter();
IOracleDataAdapter adapter = new OracleDataAdapter();

The reason for this change is that in my module I can bind 1 interface to 1 class.
Is this a bad practice? If yes what is the correct solution?

Doesn’t DI change the whole purpose of using interfaces?

EDIT:
The following is my binding for DI container

bind(ISqliteDataAdapter.class).to(SqliteDataAdapter.class);
bind(IXMLDataAdapter.class).to(XMLDataAdapter.class);
bind(IOracleDataAdapter.class).to(OracleDataAdapter.class);

If i do as suggested, how would I be able to use multiple adapters? What if I need to use both XMLDataAdapter and SQLDataAdapter in my application?

bind(DataAdapter.class).to(SqliteDataAdapter.class);

Edit:

Here is the current call to get an instance:

@inject protected ISqliteDataAdapter dataAdapter;

Here is how I should do it with having 1 interface only:

@inject protected DataAdapter dataAdapter;
// In this case I don't have a choice on which type of data adapter It's going to create
// It's already defined in my module file and it's pointing to one of the 3 DataAdapters

So what I’m trying to understand is, how can I structure my code in a way that I have control over the type of object it’s injecting, without having interface for every type of DataAdapter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:37:37+00:00Added an answer on May 30, 2026 at 12:37 pm

    I would say that DI is a natural consequence of having interfaces. The reason we have interfaces is so that we can write code that doesn’t depend on particular class, but instead an work with a multitude of classes without any changes. Even if we expect there to only be one class which does we need, this may change in the future. By programming to interface, we can even account for changes we can’t imagine.

    DI just uses the above notions in a peculiar way, to maximize the testability and adaptability of code. So I would say the DI is a really good practice.

    That said, I think there is one warning to be issued for anyone involved with these kinds of systems. There’s the danger that we write full-fledged classes like your SqliteDataAdaptor, and then push a “extract the interface” button to give the ISqliteDataAdaptor interface. This practice is very bad, particularly if it happens a lot. Instead, ISqliteDataAdaptor should usually be designed first, and then a sensible implementation can be written.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this is not a very efficient way of using linq. I
I feel like this is easy but I am missing something... Using jQuery, I
I am using jaxb for my application configurations I feel like I am doing
I'd like to get a feel for what people are using for IoC containers.
So I totally lost it when someone showed me some code using select() like:
I've been using Dependency Injection (DI) for a while, injecting either in a constructor,
I'm using some off-the-shelf OSGi bundles in my application and would like to repackage
due to performance reasons I didn't feel like using fstream for just one time.
I feel like a fool, but here goes: public interface IHasErrorController{ ErrorController ErrorController {
I feel like I should know this, but I haven't been able to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.