gpt4 book ai didi

java - 你如何组织你的 Dagger 2 模块和组件?

转载 作者:IT老高 更新时间:2023-10-28 20:51:59 25 4
gpt4 key购买 nike

你有一个特定的包来放置所有与 Dagger 相关的类吗?

或者您是否将它们放在它们注入(inject)的相关类旁边,例如如果您有 MainActivityModuleMainActivityComponent,则将它们放在与 MainActivity 相同的包中。

另外,我见过很多人将组件定义为内部类,例如在 Application 类中定义的 ApplicationComponent。你认为这是一个好习惯吗?

最佳答案

编辑:让我从这里接近事实的事实开始,但这是 Martin Fowler 的 Data Domain Presentation Layering 文章中描述的反模式 HERE (CLICK THE LINK!) ,它指定你不应该有一个 MapperModule 和一个 PresenterModule,你应该有一个 GalleryModule 和一个SomeFeatureModule 其中包含所有映射器、演示器等。

解决此问题的明智方法是使用组件依赖项来为您拥有的每个功能划分原始单例组件的子范围。这就是我描述的“全栈”分层 功能分离。

下面写的是“反模式”,您将应用程序的顶级模块切割成“层”。这样做有很多缺点。不要这样做。但是您可以阅读它并了解不该做什么。

原文:

通常,只要整个应用程序存在,您就会使用单个 Component(如 ApplicationComponent)来包含您在整个应用程序中使用的所有单例依赖项。您将在您的 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 可以注入(inject)到您想要进行字段注入(inject)的任何类的任何受包保护的字段中。

@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,但这并不是它的真正含义。这些只是创建子作用域的一种方法,而不是一种将组件切割成更小的部分的方法。所以我继承的接口(interface)其实只是一个普通的interface,带有provision方法。其他人也一样。

public interface AppContextComponent {
CustomApplication customApplication();

Context applicationContext();

AppConfig appConfig();

PackageManager packageManager();

AlarmManager alarmManager();
}

组件依赖项(允许您子范围,就像子组件一样)不允许多个范围组件,这也意味着您的模块将是无范围的。这是因为你不能从多个作用域继承,就像你不能从 Java 中的多个类继承一样。

无范围的提供程序使得模块不会保留一个单个实例,而是在每次注入(inject)调用时保留一个新实例。要获得作用域依赖项,您还需要在模块提供程序方法上提供作用域。

@Module
public class InteractorModule {
@Provides
@Singleton
public LeftNavigationDrawerInteractor leftNavigationDrawerInteractor() {
return new LeftNavigationDrawerInteractorImpl();
}

...
}

在应用程序中,如果您在任何地方都使用单例组件,则不需要更多组件,除非您创建子作用域。如果您愿意,您甚至可以考虑让您的模块成为您的 View 和演示者的完整数据提供者。

@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();
}
}

子范围允许您拥有多个演示者实例,然后可以存储状态。这在例如 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 onLoad(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 模块和组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31834056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com