Uma palestra sobre o histórico do uso de injeção de dependências no mundo android, passando pelo Guice, Dagger e Dagger 2. Link para o código de exemplo no ultimo slide.
2. QUEM SOU EU
▸ Clêrton Leal
▸ Nascido e criado na cidade de Fortaleza
▸ Trabalhando como consultor para a Avenue Code de Belo Horizonte
▸ Morando em BH a pouco mais de 5 meses =)
4. TÓPICOS DE HOJE
▸ Histórico sobre Injeção de Dependências no mundo Android e porque
devemos usar o Dagger 2 para isso.
▸ Injeção de Dependências como um padrão de projetos e quais suas
vantagens.
▸ Como aplicar Dagger 2 em seus projetos Android.
8. É UMA EXTENSÃO DO PROJETO
GUICE, CRIADO PELO GOOGLE EM
2006 POR BOB LEE E JESSE WILSON.
9. LANÇADO EM 2010 E FOI UMA
ENORME EVOLUÇÃO NA CRIAÇÃO DE
APPS ANDROID.
10. PONTOS POSITIVOS
▸ Módulos facilmente testáveis
▸ Códigos de fácil manutenção
▸ Nada de XMLs de configuração
▸ Muitas facilidades para o desenvolvimento Android
12. PONTOS NEGATIVOS
▸ Problemas de injeção aconteciam apenas em Runtime.
▸ Logs de erros extremamente ruins.
▸ Debug difícil e com uma stack confusa.
▸ Muito lento para inicialização e injeção em geral.
18. PONTOS POSITIVOS
▸ O Dagger tem como lema: “Falhe o mais cedo possível”.
▸ Análise de todas as dependências e injeções em tempo de compilação.
▸ Eliminado uso de Reflection na injeção de métodos, campos e processamento
de annotations.
▸ Escalabilidade. Pode ser usado sem problemas em grandes projetos.
▸ Detecção de dependências cíclicas em tempo de compilação.
21. EM 2013 A GOOGLE PUBLICOU UMA
ESPECIFICAÇÃO DE UMA VERSÃO
MELHORADA DO DAGGER.
22. A COMUNIDADE DO DAGGER SE
DIVIDIU ENTRE SQUARE E GOOGLE.
ASSIM O PROJETO FOI FORKED.
23. EM 2014 O DAGGER 2 FOI LANÇADO COMO
UM PROJETO DO GOOGLE PARA INJEÇÃO
DE DEPENDÊNCIA DE ALTA PERFORMANCE.
24. DAGGER 2
▸ Proposto, desenvolvido e mantido pelo Google.
▸ Eliminou 100% do uso de reflection no projeto.
▸ Transferiu toda a analise de código restante de Runtime para compilation time.
▸ Fácil de debugar e com uma stack de execução limpa.
▸ Stack trace limpo e de fácil análise.
29. public class TwitterApi {
private final OkClient okClient;
private final TwitterAuth twitterAuth;
public TwitterApi() {
this.okClient = new OkClient();
this.twitterAuth = new TwitterAuth(okClient);
}
public void postTweet(String tweet) {
/*Posting tweet*/
}
}
30. public void doTweet() {
TwitterApi twitterApi = new TwitterApi();
twitterApi.postTweet("Dagger é amor. <3");
}
32. public class TwitterApi {
private final OkClient okClient;
private final TwitterAuth twitterAuth;
public TwitterApi() {
this.okClient = new OkClient();
this.twitterAuth = new TwitterAuth(okClient);
}
public void postTweet(String tweet) {
/*Posting tweet*/
}
}
33. public class TwitterApi {
private final OkClient okClient;
private final TwitterAuth twitterAuth;
public TwitterApi(OkClient okClient, TwitterAuth twitterAuth) {
this.okClient = okClient;
this.twitterAuth = twitterAuth;
}
public void postTweet(String tweet) {
/*Posting tweet*/
}
}
34. public void doTweet() {
TwitterApi twitterApi = new TwitterApi();
twitterApi.postTweet("Dagger é amor. <3");
}
35. public void doTweet() {
OkClient okClient = new OkClient();
TwitterAuth twitterAuth = new TwitterAuth(okClient);
TwitterApi twitterApi = new TwitterApi(okClient, twitterAuth);
twitterApi.postTweet("Dagger é amor. <3");
}
40. public class TwitterApi {
private final OkClient okClient;
private final TwitterAuth twitterAuth;
public TwitterApi() {
this.okClient = new OkClient();
this.twitterAuth = new TwitterAuth(okClient);
}
public void postTweet(String tweet) {
/*Posting tweet*/
}
}
41. public class TwitterApi {
private final OkClient okClient;
private final TwitterAuth twitterAuth;
public TwitterApi(OkClient okClient, TwitterAuth twitterAuth) {
this.okClient = okClient;
this.twitterAuth = twitterAuth;
}
public void postTweet(String tweet) {
/*Posting tweet*/
}
}
48. API
▸ @Module: Mecanismo para prover dependências a serem injetadas.
▸ @Inject: Mecanismo que requer a injeção de uma dependência.
▸ @Component: Elo entre os modules e os injetáveis.