Dagger 2 - 构造函数注入 - 非Activity
Dagger 2 - Constructor Injection - Non-Activity
刚开始学习 Dagger 2 以解决特定问题:我正在尝试遵循 MVVM 架构并且我的应用程序有一个存储库 class 可以在 CacheData class 中提取和保存设置基本上包装了 SharedPreferences。但是,SharedPreferences 具有上下文依赖性。由于我完成了将我的存储库和数据层与视图和应用程序 class 解耦的所有努力,因此传递上下文似乎是倒退了一步。
这是存储库class
public class ImgurRepository {
private ImgurDatabase imgurDatabase;
private ImgurGalleryDao galleryDao;
private CompositeDisposable disposables;
private LiveData<List<ImgurGallery>> imgurGalleries;
public ImgurRepository(){
this.imgurDatabase = ImgurDatabase.getInstance(MyRxApplication.getAppContext());
this.galleryDao = imgurDatabase.getGalleryDao();
disposables = new CompositeDisposable();
}
public String getCachedSearchTerm() {
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchTerm();
}
public String getCachedSearchWindow(){
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchWindow();
}
public String getCachedSearchType(){
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchType();
}
public void setCachedSearchParams(@NonNull final String term,
@NonNull final String type,
@NonNull final String window) {
CachedData cachedData = new CachedData();
cachedData.setCachedSearchParams(term, type, window);
}
public LiveData<List<ImgurGallery>> getCachedGalleries() {
return this.imgurGalleries;
}
public LiveData<List<ImgurGallery>> fetchGalleries(@NonNull final String searchType,
@NonNull final String searchWindow,
@NonNull final String searchTerm,
final int resultsPage){
requestGalleries(searchType, searchWindow, searchTerm, resultsPage);
return galleryDao.getAll();
}
private void requestGalleries(@NonNull final String searchType,
@NonNull final String searchWindow,
@NonNull final String searchTerm,
final int resultsPage) {
Timber.d("Running fetchGalleries with arguments:\nsort='%s' \nwindow='%s'\nsearch='%s'\npage='%s'",
searchType,
searchWindow,
searchTerm,
resultsPage);
ServiceGenerator.changeApiBaseUrl(IMGUR_API_BASE_URL);
ImgurService service = ServiceGenerator.createService(ImgurService.class);
Timber.d("finishing fetchGalleries request.");
disposables.add(service.getSearchGallery(searchType,searchWindow,resultsPage,searchTerm)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Response<ImgurGalleryList>>() {
@Override
public void accept(@NonNull final Response<ImgurGalleryList> response) throws Exception {
Timber.d("Consumer is subscribed to imgurGalleryObservable.");
Timber.d(response.body().toString());
List<ImgurGallery> galleries = response.body().getData();
galleryDao.insertAll(galleries);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Timber.e(throwable);
}
}));
}
public void clearGalleries() {
galleryDao.deleteAll();
}
}
根据 Samuel 的评论,这是 固定的 CachedData class:
public class CachedData {
private final String CACHED_SEARCH_TERM_KEY = "cached_search_term";
private final String CACHED_SEARCH_WINDOW_KEY = "cached_search_window";
private final String CACHED_SEARCH_TYPE_KEY = "cached_search_type";
@Inject public SharedPreferences sharedPrefs;
@Inject public Context context;
public CachedData() {}
public String getCachedSearchTerm() {
return sharedPrefs.getString(CACHED_SEARCH_TERM_KEY, "");
}
public String getCachedSearchWindow(){
return sharedPrefs.getString(CACHED_SEARCH_WINDOW_KEY, "");
}
public String getCachedSearchType(){
return sharedPrefs.getString(CACHED_SEARCH_TYPE_KEY, "");
}
public void setCachedSearchParams(@Nullable final String term,
@Nullable final String window,
@Nullable final String type) {
SharedPreferences.Editor editor = sharedPrefs.edit();
if (term != null) editor.putString((CACHED_SEARCH_TERM_KEY), term);
if (window != null) editor.putString(CACHED_SEARCH_WINDOW_KEY, window);
if (type != null) editor.putString(CACHED_SEARCH_TYPE_KEY, type);
editor.apply();
}
}
固定 AppComponent class:
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
void inject(MyRxApplication app);
void inject(CachedData cachedData);
void inject(BaseActivity activity);
}
AppModule class:
@Module
public class AppModule {
private final String SHARED_PREFERENCE_KEY = "PREFERENCE_FILE_KEY";
private final MyRxApplication application;
public AppModule(MyRxApplication application) {
this.application = application;
}
@Provides
@Singleton
public Context provideApplicationContext(){
return application;
}
@Provides
@Singleton
public SharedPreferences provideSharedPreferences() {
return application
.getSharedPreferences(
SHARED_PREFERENCE_KEY,
Context.MODE_PRIVATE);
}
}
申请class:
public class MyRxApplication extends Application {
private static Context context;
private AppComponent appComponent;
public MyRxApplication() {
}
@Override
public void onCreate() {
super.onCreate();
MyRxApplication.context = getApplicationContext();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
Stetho.initializeWithDefaults(this);
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
appComponent = buildComponent();
appComponent.inject(this);
appComponent.cachedData();
}
public AppComponent buildComponent(){
return DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
}
public AppComponent getAppComponent() {
return appComponent;
}
public static Context getAppContext() {
return MyRxApplication.context;
}
}
是否可以使用Dagger 将SharedPreferences 对象注入到CachedData 构造函数中?我正在构造许多 CachedData 对象,因为我最初不想使用 Singleton,但是将 CachedData 对象作为单例注入是否有意义?我真的不确定现在使用 Dagger 并让它工作的正确方法,因为我在 CacheData 构造函数中设置的每个参数都需要在我创建它时提供,即使我认为我正在使用 Dagger 来做到这一点...... ?
嗯,据我所知,从你的附加评论来看,你不想为你的 class CachedData
使用注入,所以想手动实例化它。如果是这种情况,您可以删除注入的构造函数参数并像这样直接注入变量:
@Inject
SharedPreferences sharedPrefs;
@Inject
Context context;
@Inject
public CachedData() {}
刚开始学习 Dagger 2 以解决特定问题:我正在尝试遵循 MVVM 架构并且我的应用程序有一个存储库 class 可以在 CacheData class 中提取和保存设置基本上包装了 SharedPreferences。但是,SharedPreferences 具有上下文依赖性。由于我完成了将我的存储库和数据层与视图和应用程序 class 解耦的所有努力,因此传递上下文似乎是倒退了一步。
这是存储库class
public class ImgurRepository {
private ImgurDatabase imgurDatabase;
private ImgurGalleryDao galleryDao;
private CompositeDisposable disposables;
private LiveData<List<ImgurGallery>> imgurGalleries;
public ImgurRepository(){
this.imgurDatabase = ImgurDatabase.getInstance(MyRxApplication.getAppContext());
this.galleryDao = imgurDatabase.getGalleryDao();
disposables = new CompositeDisposable();
}
public String getCachedSearchTerm() {
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchTerm();
}
public String getCachedSearchWindow(){
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchWindow();
}
public String getCachedSearchType(){
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchType();
}
public void setCachedSearchParams(@NonNull final String term,
@NonNull final String type,
@NonNull final String window) {
CachedData cachedData = new CachedData();
cachedData.setCachedSearchParams(term, type, window);
}
public LiveData<List<ImgurGallery>> getCachedGalleries() {
return this.imgurGalleries;
}
public LiveData<List<ImgurGallery>> fetchGalleries(@NonNull final String searchType,
@NonNull final String searchWindow,
@NonNull final String searchTerm,
final int resultsPage){
requestGalleries(searchType, searchWindow, searchTerm, resultsPage);
return galleryDao.getAll();
}
private void requestGalleries(@NonNull final String searchType,
@NonNull final String searchWindow,
@NonNull final String searchTerm,
final int resultsPage) {
Timber.d("Running fetchGalleries with arguments:\nsort='%s' \nwindow='%s'\nsearch='%s'\npage='%s'",
searchType,
searchWindow,
searchTerm,
resultsPage);
ServiceGenerator.changeApiBaseUrl(IMGUR_API_BASE_URL);
ImgurService service = ServiceGenerator.createService(ImgurService.class);
Timber.d("finishing fetchGalleries request.");
disposables.add(service.getSearchGallery(searchType,searchWindow,resultsPage,searchTerm)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Response<ImgurGalleryList>>() {
@Override
public void accept(@NonNull final Response<ImgurGalleryList> response) throws Exception {
Timber.d("Consumer is subscribed to imgurGalleryObservable.");
Timber.d(response.body().toString());
List<ImgurGallery> galleries = response.body().getData();
galleryDao.insertAll(galleries);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Timber.e(throwable);
}
}));
}
public void clearGalleries() {
galleryDao.deleteAll();
}
}
根据 Samuel 的评论,这是 固定的 CachedData class:
public class CachedData {
private final String CACHED_SEARCH_TERM_KEY = "cached_search_term";
private final String CACHED_SEARCH_WINDOW_KEY = "cached_search_window";
private final String CACHED_SEARCH_TYPE_KEY = "cached_search_type";
@Inject public SharedPreferences sharedPrefs;
@Inject public Context context;
public CachedData() {}
public String getCachedSearchTerm() {
return sharedPrefs.getString(CACHED_SEARCH_TERM_KEY, "");
}
public String getCachedSearchWindow(){
return sharedPrefs.getString(CACHED_SEARCH_WINDOW_KEY, "");
}
public String getCachedSearchType(){
return sharedPrefs.getString(CACHED_SEARCH_TYPE_KEY, "");
}
public void setCachedSearchParams(@Nullable final String term,
@Nullable final String window,
@Nullable final String type) {
SharedPreferences.Editor editor = sharedPrefs.edit();
if (term != null) editor.putString((CACHED_SEARCH_TERM_KEY), term);
if (window != null) editor.putString(CACHED_SEARCH_WINDOW_KEY, window);
if (type != null) editor.putString(CACHED_SEARCH_TYPE_KEY, type);
editor.apply();
}
}
固定 AppComponent class:
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
void inject(MyRxApplication app);
void inject(CachedData cachedData);
void inject(BaseActivity activity);
}
AppModule class:
@Module
public class AppModule {
private final String SHARED_PREFERENCE_KEY = "PREFERENCE_FILE_KEY";
private final MyRxApplication application;
public AppModule(MyRxApplication application) {
this.application = application;
}
@Provides
@Singleton
public Context provideApplicationContext(){
return application;
}
@Provides
@Singleton
public SharedPreferences provideSharedPreferences() {
return application
.getSharedPreferences(
SHARED_PREFERENCE_KEY,
Context.MODE_PRIVATE);
}
}
申请class:
public class MyRxApplication extends Application {
private static Context context;
private AppComponent appComponent;
public MyRxApplication() {
}
@Override
public void onCreate() {
super.onCreate();
MyRxApplication.context = getApplicationContext();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
Stetho.initializeWithDefaults(this);
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
appComponent = buildComponent();
appComponent.inject(this);
appComponent.cachedData();
}
public AppComponent buildComponent(){
return DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
}
public AppComponent getAppComponent() {
return appComponent;
}
public static Context getAppContext() {
return MyRxApplication.context;
}
}
是否可以使用Dagger 将SharedPreferences 对象注入到CachedData 构造函数中?我正在构造许多 CachedData 对象,因为我最初不想使用 Singleton,但是将 CachedData 对象作为单例注入是否有意义?我真的不确定现在使用 Dagger 并让它工作的正确方法,因为我在 CacheData 构造函数中设置的每个参数都需要在我创建它时提供,即使我认为我正在使用 Dagger 来做到这一点...... ?
嗯,据我所知,从你的附加评论来看,你不想为你的 class CachedData
使用注入,所以想手动实例化它。如果是这种情况,您可以删除注入的构造函数参数并像这样直接注入变量:
@Inject
SharedPreferences sharedPrefs;
@Inject
Context context;
@Inject
public CachedData() {}