Hola. I am having a hard time working with Guice. I'm from the .Net
world and comfortable with the IoC/ServiceLocator tool named Structure
Map. I enjoy using this tool so I decided to see if Guice could give
me what I am looking for with regards to implementing IoC.
I have an Android app that runs successfully using the AVD. The app
simply reads data from a DB and renders it to the screen. Nothing
fancy. This is my baseline.
The app contains a single Activity class named Start. The Start class
depends on an IDbService. Currently, I new up an instance of the
concrete impl of IDbService using the default constructor in Start.
Everything works fine. The code in the constructor looks like this:
_dbService = new DbService();
My goal: using Guice, I would like to inject the DbService into
Start.
I started by creating an AbstractActivity class which my Start class
extends. In the constructor of AbstractActivity, I setup Guice using
the following code.
public abstract class AbstractActivity extends Activity {
public AbstractActivity() {
Guice.createInjector(new GuiceModule());
}
}
My GuiceModule looks like this.
public class GuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(IDbService.class).to(DbService.class);
}
}
As a safety check, I ran my App on the AVD. It still works fine. So
far - so good.
Next, back in my Start class I added the following code. This may be
where I am incorrectly conceptualizing how Guice should be used.
@Inject IDbService _dbService;
Now when I run my App, it closes unexpectedly. I don't see anything
in the stack trace that tells me why; I'm still trying to find some
clues.
In the mean time, would someone mind commenting on my usage of Guice?
Ideally, I'd like to inject the IDbService into the constructor of the
Start activity but I don't know how to override how the system creates
instances of Activity. So, I figured starting with a method-level
injection would be easier.
I am also aware of RoboGuice however I choose to start with Guice as
after reading the doc for both, it seems the latter may give me more
choices of types that I can inject.
Thanks for your time.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.