gpt4 book ai didi

android - MVVM - 很难理解如何在 Clean Architecture 中创建域层

转载 作者:行者123 更新时间:2023-12-04 23:55:43 26 4
gpt4 key购买 nike

我正在尝试学习 MVVM 以使我的应用程序的架构更加简洁。但是我很难掌握如何为我的应用程序创建“域”层。

目前我的项目结构是这样的:

我的 View 是 Activity 。我的 ViewModel 有一个 Activity 可以调用的公共(public)方法。调用 ViewModel 中的方法后,它会调用我的 Repository 类中的方法,该类执行网络调用,然后将数据返回给 ViewModel。然后,我更新 ViewModel 中的 LiveData,以便更新 Activity 的 UI。

这就是我对如何向结构添加 Domain 层感到困惑的地方。我已经阅读了很多关于域层的 Stackoverflow 答案和博客,它们大多都告诉您从 ViewModel 中删除所有业务逻辑并创建一个纯 Java/Kotlin 类。

所以代替

View --> ViewModel --> Repository

我将从 ViewModel 通信到 Domain 类,而 Domain 类将与 Repository 通信>?

View --> ViewModel --> Domain --> Repository

我正在使用 RxJava 从我的 ViewModel 调用 Repository 类。

@HiltViewModel
public class PostViewModel extends ViewModel {

private static final String TAG = "PostViewModel";

private final List<Post> listPosts = new ArrayList<>();
private final MutableLiveData<List<Post>> getPostsLiveData = new MutableLiveData<>();
private final MutableLiveData<Boolean> centerProgressLiveData = new MutableLiveData<>();
private final MainRepository repository;

@Inject
public PostViewModel(MainRepository repository) {
this.repository = repository;
getSubredditPosts();
}

public void getSubredditPosts() {
repository.getSubredditPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
centerProgressLiveData.setValue(true);
}

@Override
public void onNext(@NonNull Response response) {
Log.d(TAG, "onNext: Query called");
centerProgressLiveData.setValue(false);
listPosts.clear();
listPosts.addAll(response.getData().getChildren());
getPostsLiveData.setValue(listPosts);
}

@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: getPosts", e);
centerProgressLiveData.setValue(false);
}

@Override
public void onComplete() {
}
});
}
public class MainRepository {

private final MainService service;

@Inject
public MainRepository(MainService service) {
this.service = service;
}

public Observable<Response> getSubredditPosts() {
return service.getSubredditPosts();
}
}

有人可以给我举个例子说明我该怎么做吗?我在这里迷路了

最佳答案

我在尝试找出域层时遇到了困难。最常见的例子是用例。

您的 View 模型不会直接与存储库通信。正如你所说,你需要 viewmodel 》domain 》repository。

您可能会将用例视为每个存储库方法的抽象。

假设您有一个 Movies Repository,您可以在其中为电影列表调用一个方法,为电影详细信息调用另一个方法,为相关电影调用第三个方法。

每个方法都有一个用例。

这样做的目的是什么?

假设您有一个与 Detail Viewmodel 通信的 DetailActivity。您的 View 模型不需要知道所有存储库(在详细信息屏幕上调用电影列表方法的目的是什么?)。因此,您的 DetailViewModel 将只知道“Detail Usecase”(调用存储库中的 Detail 方法)。

谷歌几个小时前更新了架构文档,看看吧! https://android-developers.googleblog.com/2021/12/rebuilding-our-guide-to-app-architecture.html?m=1&s=09

PS:Usecase 不是一个特殊的 android 类,您不需要固有任何行为(如 fragment 、 Activity 、 View 模型......)它是一个普通类,将接收存储库作为参数。

你会得到类似的东西:

View 模型:

function createPost(post Post){
createUseCase.create(post)
}

用例

function createPost(post Post): Response {
return repository.create(post)
}

关于android - MVVM - 很难理解如何在 Clean Architecture 中创建域层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70353712/

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