Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

@inject presentation

Introduction to dependency injection.
by

G G

on 29 August 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of @inject presentation

@inject String name;
Inversion of control
Martin Fowler
usual case
List<Person> contacts = Contacts.query(...);
refactor
copy & paste !?
Lister<Person> contactLister = new OnlineContactLister(connection, ...);
Factory
Change specs !?
Lister<Person> contactLister = Creator.createContactLister(connection);

//in settings file, set ContactLister to NetContactLister
Constructor parameters ??
injector
//injector settings
Lister<Person> => NetPersonLister

//injector usage
@Inject Lister<Person> contactLister;
injector
- maps interface to implementation
- construct and inject objects
- construct and inject parameters
refactor with injection
only change injector settings !
injector modules
Collection of injection settings
public class netRunModule {
Lister<Person> => NetContactLister
}

public class localRunModule {
Lister<Person> => DBConactLister
}
public class TestModule {
Lister<Person> => DummyLister
}
Constructor injection
@Inject public Clazz (Dependency d) {
this.mD = d;
//imp
}
Method injection
@inject void setDependency (Dependency d) {
this.mD = d;
};
Member injection
@inject private Dependency mD;
// impossible without injection FW !
- clarify dependencies
- easy refactor
- perfect for testing
Injection frameworks
- Pico: minimal, efficient
- Guice: full-featured, runtime
- Spring
- Roboguice: based on guice, Android specific features
- Proton: small footprint
- Dagger: for Android, static injection
Android
Activity, Application, etc instantiated by the system !
Roboguice
public class RoboActivity extends Activity {
public void onCreate(...) {
super.onCreate(...);
Injector i = RoboGuice.getInjector(applicationContext);
i.inject(this);
}
Roboguice usage
public class MyClass extends RoboActivity {
@inject Something mS;

public onCreate(...) {
// mS == null
super.onCreate(...);
// mS instantiated !
Scope
- instance / request
- context / session
- application / singleton
Full transcript