java – 如何组织Dagger 2模块和组件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何组织Dagger 2模块和组件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6867字,纯文字阅读大概需要10分钟。
内容图文
你有一个特定的包,你把所有与Dagger相关的课程放在哪里?
或者你把它们放在他们注入的相关类旁边,例如如果您有MainActivityModule和MainActivityComponent,则将它们与MainActivity放在同一个包中.
另外,我见过很多人将组件定义为内部类,例如在Application类中定义的ApplicationComponent.你认为这是一个好习惯吗?
解决方法:
编辑:让我先从这里接近事实这一事实开始,但这是一个反模式,如Martin Fowler的Data Domain Presentation Layering文章HERE (CLICK THE LINK!)所述,它指定你不应该有一个MapperModule和一个PresenterModule,你应该有一个GalleryModule和一个SomeFeatureModule,其中包含所有的映射器,演示器等.
实现它的智能路线是使用组件依赖关系来为您拥有的每个功能提供原始单例组件.我所描述的是“全栈”分层,按功能分离.
下面写的是“反模式”,您可以将应用程序的顶级模块切割为“层”.这样做有很多缺点.不要这样做.但是你可以阅读它并学习不该做的事情.
原文:
通常,只要整个应用程序存在,您就可以像ApplicationComponent一样使用单个Component来包含您在整个应用程序中使用的所有单例依赖项.您可以在Application类中实例化它,并从其他地方访问它.
我目前的项目结构是:
+ injection
|- components
|-- ApplicationComponent.java
|- modules
|- data
|-- DbMapperModule.java
|-- ...
|- domain
|-- InteractorModule.java
|-- ...
|- presentation
|-- ...
|- utils
|-- ...
|- scope
|- subcomponents
|- data
|-- ...
|- domain
|-- DbMapperComponent.java
|-- ...
|- presentation
|-- ...
|- utils
|-- ...
|-- AppContextComponent.java
|-- AppDataComponent.java
|-- AppDomainComponent.java
|-- AppPresentationComponent.java
|-- AppUtilsComponent.java
例如,我的是这样的:
public enum Injector {
INSTANCE;
private ApplicationComponent applicationComponent;
private Injector() {
}
public ApplicationComponent getApplicationComponent() {
return applicationComponent;
}
ApplicationComponent initializeApplicationComponent(CustomApplication customApplication) {
AppContextModule appContextModule = new AppContextModule(customApplication);
RealmModule realmModule = new RealmModule(customApplication.getRealmHolder());
applicationComponent = DaggerApplicationComponent.builder()
.appContextModule(appContextModule)
.realmModule(realmModule)
.build();
return applicationComponent;
}
}
而且你需要一个ApplicationComponent,它可以注入任何你想要字段注入的类的受包保护的字段.
@Singleton
@Component(modules = {
AppContextModule.class,
DbMapperModule.class,
DbTaskModule.class,
RealmModule.class,
RepositoryModule.class,
InteractorModule.class,
ManagerModule.class,
ServiceModule.class,
PresenterModule.class,
JobManagerModule.class,
XmlPersisterModule.class
})
public interface ApplicationComponent
extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
void inject(CustomApplication customApplication);
void inject(DashboardActivity dashboardActivity);
...
}
对我来说,AppContextComponent将是一个@Subcomponent,但实际上并不是它的含义.这些只是创建子范围的一种方法,而不是将组件切割成较小部分的方法.所以我继承的接口实际上只是一个带有提供方法的普通接口.其他人也一样.
public interface AppContextComponent {
CustomApplication customApplication();
Context applicationContext();
AppConfig appConfig();
PackageManager packageManager();
AlarmManager alarmManager();
}
组件依赖性(允许您像子组件一样进行子视图)不允许多个作用域组件,这也意味着您的模块将是未作用域的.这是因为您无法从多个范围继承,就像您无法从Java中的多个类继承一样.
无范围提供程序使得模块不会在每次注入调用时保留单个实例,而是保留新实例.要获得作用域依赖项,还需要在模块提供程序方法上提供作用域.
@Module
public class InteractorModule {
@Provides
@Singleton
public LeftNavigationDrawerInteractor leftNavigationDrawerInteractor() {
return new LeftNavigationDrawerInteractorImpl();
}
...
}
在应用程序中,如果在任何地方使用Singleton组件,除非创建子范围,否则不需要更多组件.如果需要,您甚至可以考虑使您的模块成为您的视图和演示者的完整数据提供者.
@Component(dependencies = {ApplicationComponent.class}, modules = {DetailActivityModule.class})
@ActivityScope
public interface DetailActivityComponent extends ApplicationComponent {
DataObject data();
void inject(DetailActivity detailActivity);
}
@Module
public class DetailActivityModule {
private String parameter;
public DetailActivityModule(String parameter) {
this.parameter = parameter;
}
@Provides
public DataObject data(RealmHolder realmHolder) {
Realm realm = realmHolder.getRealm();
return realm.where(DataObject.class).equalTo("parameter", parameter).findFirst();
}
}
Subscoping允许您拥有演示者的多个实例,然后可以存储状态.这在例如Mortar / Flow中是有意义的,其中each screen has its own “path”, and each path has its own component – 将数据提供为“蓝图”.
public class FirstPath
extends BasePath {
public static final String TAG = " FirstPath";
public final int parameter;
public FirstPath(int parameter) {
this.parameter = parameter;
}
//...
@Override
public int getLayout() {
return R.layout.path_first;
}
@Override
public FirstViewComponent createComponent() {
FirstPath.FirstViewComponent firstViewComponent = DaggerFirstPath_FirstViewComponent.builder()
.applicationComponent(InjectorService.obtain())
.firstViewModule(new FirstPath.FirstViewModule(parameter))
.build();
return firstViewComponent;
}
@Override
public String getScopeName() {
return TAG + "_" + parameter;
}
@ViewScope //needed
@Component(dependencies = {ApplicationComponent.class}, modules = {FirstViewModule.class})
public interface FirstViewComponent
extends ApplicationComponent {
String data();
FirstViewPresenter firstViewPresenter();
void inject(FirstView firstView);
void inject(FirstViewPresenter firstViewPresenter);
}
@Module
public static class FirstViewModule {
private int parameter;
public FirstViewModule(int parameter) {
this.parameter = parameter;
}
@Provides
public String data(Context context) {
return context.getString(parameter);
}
@Provides
@ViewScope //needed to preserve scope
public FirstViewPresenter firstViewPresenter() {
return new FirstViewPresenter();
}
}
public static class FirstViewPresenter
extends ViewPresenter<FirstView> {
public static final String TAG = FirstViewPresenter.class.getSimpleName();
@Inject
String data;
public FirstViewPresenter() {
Log.d(TAG, "First View Presenter created: " + toString());
}
@Override
protected void onEnterScope(MortarScope scope) {
super.onEnterScope(scope);
FirstViewComponent firstViewComponent = scope.getService(DaggerService.TAG);
firstViewComponent.inject(this);
Log.d(TAG, "Data [" + data + "] and other objects injected to first presenter.");
}
@Override
protected void onSave(Bundle outState) {
super.onSave(outState);
FirstView firstView = getView();
outState.putString("input", firstView.getInput());
}
@Override
protected void onl oad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
if(!hasView()) {
return;
}
FirstView firstView = getView();
if(savedInstanceState != null) { //needed check
firstView.setInput(savedInstanceState.getString("input"));
}
}
public void goToNextActivity() {
FirstPath firstPath = Path.get(getView().getContext());
if(firstPath.parameter != R.string.hello_world) {
Flow.get(getView()).set(new FirstPath(R.string.hello_world));
} else {
Flow.get(getView()).set(new SecondPath());
}
}
}
}
内容总结
以上是互联网集市为您收集整理的java – 如何组织Dagger 2模块和组件?全部内容,希望文章能够帮你解决java – 如何组织Dagger 2模块和组件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。