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.