Android Dagger 2 和干净的架构实现,范围错误
Android Dagger 2 and clean architecture implementation, scope errors
我正在尝试弄清楚如何在 Android 中使用 Dagger 2 和干净的架构来处理依赖注入。我想要实现的是当我单击一个按钮时,一条消息将保存到 Firebase 数据库中。并向用户显示成功消息。当我构建我的项目时,出现此错误:
Error:(10, 1) error: com.example.mvpsample.home.HomeComponent
(unscoped) may not reference scoped bindings: @Provides @Singleton
com.google.firebase.database.FirebaseDatabase
com.example.mvpsample.data.DataModule.provideFirebaseDatabase()
这是我的应用 class:
public class MyApp extends Application {
private static MyApp app;
private HomeComponent homeComponent;
private AuthenticationComponent authenticationComponent;
@Inject
Presenter presenter;
@Override
public void onCreate() {
super.onCreate();
app = this;
homeComponent = DaggerHomeComponent
.builder()
.homeModule(new HomeModule(presenter.getView()))
.build();
}
public HomeComponent getHomeComponent() {
return homeComponent;
}
public static MyApp app() {
return app;
}
}
首页活动
public class HomeActivity extends AppCompatActivity implements BaseContract.View {
@Inject
public Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
MyApp
.app()
.getHomeComponent()
.inject(this);
}
@OnClick(R.id.tvHello)
public void clickTvHello() {
presenter.writeStringToDatabase("Hi");
}
@Override
public void showSuccessMessage() {
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
}
家庭模块
@Module
public class HomeModule {
private final BaseContract.View View;
@Inject
public HomeModule(BaseContract.View View) {
this.View = View;
}
@Provides
public BaseContract.View provideView() {
return View;
}
}
主页组件
@Component(modules = {HomeModule.class, DataModule.class})
public interface HomeComponent {
void inject(HomeActivity homeActivity);
}
数据模块
@Module
public class DataModule {
@Provides
@Singleton
public FirebaseDatabase provideFirebaseDatabase() {
return FirebaseDatabase.getInstance();
}
}
BaseContract
public interface BaseContract {
interface View {
void showSuccessMessage();
}
interface Presenter {
View getView();
void writeStringToDatabase(String string);
}
}
主持人
public class Presenter implements BaseContract.Presenter {
private final BaseContract.View View;
@Inject
FirebaseDatabase firebaseDatabase;
@Inject
public Presenter(BaseContract.View View) {
this.View = View;
}
@Override
public BaseContract.View getView() {
return View;
}
@Override
public void writeStringToDatabase(String string) {
firebaseDatabase.getReference()
.child("messages")
.push()
.child("value")
.setValue(string).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
getView().showSuccessMessage();
}
});
}
}
我遵循示例项目和教程,但不明白我在这里做错了什么。不是在寻找工作项目,但我想了解这方面的最佳实践以及如何管理和使用模块和组件。
问题是您的 HomeComponent
没有作用域,而您的 DataModule
提供作用域依赖(即 FirebaseDatabase
依赖)。
非作用域组件不能依赖作用域提供者。您要么必须删除 provideFirebaseDatabase()
提供商上的 @Singleton
,要么在 HomeComponent
.
上添加 @Singleton
我正在尝试弄清楚如何在 Android 中使用 Dagger 2 和干净的架构来处理依赖注入。我想要实现的是当我单击一个按钮时,一条消息将保存到 Firebase 数据库中。并向用户显示成功消息。当我构建我的项目时,出现此错误:
Error:(10, 1) error: com.example.mvpsample.home.HomeComponent (unscoped) may not reference scoped bindings: @Provides @Singleton com.google.firebase.database.FirebaseDatabase com.example.mvpsample.data.DataModule.provideFirebaseDatabase()
这是我的应用 class:
public class MyApp extends Application {
private static MyApp app;
private HomeComponent homeComponent;
private AuthenticationComponent authenticationComponent;
@Inject
Presenter presenter;
@Override
public void onCreate() {
super.onCreate();
app = this;
homeComponent = DaggerHomeComponent
.builder()
.homeModule(new HomeModule(presenter.getView()))
.build();
}
public HomeComponent getHomeComponent() {
return homeComponent;
}
public static MyApp app() {
return app;
}
}
首页活动
public class HomeActivity extends AppCompatActivity implements BaseContract.View {
@Inject
public Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
MyApp
.app()
.getHomeComponent()
.inject(this);
}
@OnClick(R.id.tvHello)
public void clickTvHello() {
presenter.writeStringToDatabase("Hi");
}
@Override
public void showSuccessMessage() {
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
}
家庭模块
@Module
public class HomeModule {
private final BaseContract.View View;
@Inject
public HomeModule(BaseContract.View View) {
this.View = View;
}
@Provides
public BaseContract.View provideView() {
return View;
}
}
主页组件
@Component(modules = {HomeModule.class, DataModule.class})
public interface HomeComponent {
void inject(HomeActivity homeActivity);
}
数据模块
@Module
public class DataModule {
@Provides
@Singleton
public FirebaseDatabase provideFirebaseDatabase() {
return FirebaseDatabase.getInstance();
}
}
BaseContract
public interface BaseContract {
interface View {
void showSuccessMessage();
}
interface Presenter {
View getView();
void writeStringToDatabase(String string);
}
}
主持人
public class Presenter implements BaseContract.Presenter {
private final BaseContract.View View;
@Inject
FirebaseDatabase firebaseDatabase;
@Inject
public Presenter(BaseContract.View View) {
this.View = View;
}
@Override
public BaseContract.View getView() {
return View;
}
@Override
public void writeStringToDatabase(String string) {
firebaseDatabase.getReference()
.child("messages")
.push()
.child("value")
.setValue(string).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
getView().showSuccessMessage();
}
});
}
}
我遵循示例项目和教程,但不明白我在这里做错了什么。不是在寻找工作项目,但我想了解这方面的最佳实践以及如何管理和使用模块和组件。
问题是您的 HomeComponent
没有作用域,而您的 DataModule
提供作用域依赖(即 FirebaseDatabase
依赖)。
非作用域组件不能依赖作用域提供者。您要么必须删除 provideFirebaseDatabase()
提供商上的 @Singleton
,要么在 HomeComponent
.
@Singleton