关于我目前的MVVM Android应用架构的建议:

huangapple go评论63阅读模式
英文:

Advice for my current MVVM android app architecture

问题

我有使用Java来开发后端Web应用的经验,我正试图理清MVVM架构的布局方式,但我无法停止思考Java中的Services、ServicesImpl、Controllers等等...

到目前为止,这是我Android应用UI的结构:

主活动 ---> 片段 -> 子片段
               |-> 片段2 -> 子片段2
               |-> 等等...

很简单,对吧?

但仅此而已。我没有使用服务或ViewModel(所以我猜这就不是MVVM结构了,哈哈)。

所有逻辑都写在主活动中以适用于全局内容,每个片段也有自己的逻辑。而且情况开始变得混乱,我一直在避免问这个问题,哈哈。

所以我有4个问题:

Q1) 在片段类中编写逻辑是否可以?例如,MyFragment加载使用Firestore文档的RV。在我以前的Java后端应用程序中,我总是使用服务等,代码保持得更加清洁,但在Android中,就像我上面说的,情况开始变得混乱,因为在MyFragment中,我需要Adapter,ViewHolder以及多个查询和方法,因为我实现了一些过滤器。而且因为这些过滤器是另一个独立的RV,我需要另一个Adapter,另一个ViewHolder,总之...你明白我的意思。在我应用程序的早期版本中,我将片段与适配器分开,创建了一个"MyFragmentAdapter.kt"文件。你认为这样做是一个好的解决方案吗?还是所有这些都应该放入ViewModel中?

Q2) 到目前为止,我对ViewModel没有用武之地,因为如果我需要持久化数据,我会将数据放在我的主活动中,就这样。主活动永远不会关闭,因此片段的数据也永远不会丢失。你认为这是一个好的做法吗?

Q3) 查看开源Android应用程序,我没有找到包含服务文件夹的结构,而对我来说,这几乎是必须的。为什么会这样?

Q4) 我的项目文件夹的布局如下:

.project.login -> 一些几乎没有任何逻辑的活动
        .models -> 只有一些POJO类
        .utils -> 一些utils.kt
        .ui -> 食谱(文件夹) -> 一堆有很多逻辑的片段
            -> 新闻(文件夹) -> 更多有很多逻辑的片段

对于这个结构,你会添加些什么吗?

英文:

I have experience using Java for back-end web applications and I'm trying to make some sense out of the way MVVM architecture has to be laid but I can't stop thinking about Java Services, ServicesImpl, Controllers, etc...

This is the structure of my android app ui so far:

Main activity ---> Fragment -> SubFragment
               |-> Fragment2 -> SubFragment2
               |-> etc...

Simple enough, right?

But that's it. I'm not using services nor ViewModels (so I guess it's not a MVVM structure then lol).

All the logic is written in Main Activity for global stuff, and each fragment has its own logic as well. And its starting to get messy and I've been avoiding making this question a lot too lol.

So I have 4 questions:

Q1) Is it OK that the logic is being written in the fragment class? I.e. MyFragment loads an RV with firestore documents. In my java back-end applications I'm always using services and stuff and the code remains a lot cleaner but in Android, as I said above, it's starting to get messy because in MyFragment I need the Adapter, the ViewHolder and multiple queries and methods because I've implemented some filters. And because these filters are another RV separately I need another Adapter, another ViewHolder, and well... you see my point. For an earlier version of my App I separated the fragment from the adapter creating a "MyFragmentAdapter.kt" file. Would you consider doing this a good solution? Or all of this should go into a ViewModel?

Q2) So far I have no use for a ViewModel because if I need to persist data, I put that data in my Main Activity and that's it. Main activity never dies so the fragments data never dies as well. Would you consider this a good practice?

Q3) Looking at open source android applications I found no structure containing a services folder, which for me is practically a given. Why is that?

Q4) The folders in my project are laid out like this:

.project.login -> some activities with barely any logic
        .models -> just pojos
        .utils -> some utils.kt
        .ui -> recipes (folder) -> bunch of fragments with lots of logic
            -> news (folder) -> more fragments with lots of logic

Would you add something to this structure?

答案1

得分: 1

我将尽量简单明了地回答。如果对你(你的示例)有效,那就可以。就是这么简单。在这个世界上,并没有固定的正确/错误答案、最佳架构、模式等等来创建 Android 应用。即使是更大的IT公司也会遵循自己的设计模式,有些开发人员喜欢,有些不喜欢。无论如何,回到你提出的问题:

1)将Fragment和Activity仅视为负责UI逻辑。即加载视图、更改视图属性(按钮颜色、可见性等)、在视图上显示数据等(从VM获取)。
MyFragmentAdapter-> 是的,创建单独的适配器类与ViewHolder。我甚至将单一职责放在一个独立的ViewHolder类中。
或者所有这些都应该放在ViewModel中?-> 不,ViewModel不持有对视图的引用!它只保存数据并与UI进行通信!

2)你认为这是一个好的做法吗?如果你考虑使用VM,那么这不是一个好主意。如上所述,Fragment(和Activity)仅负责UI逻辑。在VM中,开始查询、进行REST API调用等,然后将数据发送到UI。

主Activity永远不会销毁,因此片段的数据也永远不会销毁。-> 是的,但VM的主要奇妙之处在于它可以“幸存”配置更改。当Activity重新创建时,VM是“相同的”,并保存了在UI上显示所需的所有数据。

3)Service不涉及UI,不需要ViewModels。

4)好的,但我会考虑每个功能模块具有自己的逻辑。例如:recipes-> ui-> fragment、adapter、viewmodel、di(依赖项)、data(API、存储库)、domain(用例)

英文:

I will try to answer as simple as possible. If it works for you (your example), then it is ok. Simple as that. There is no wright/wrong answer, best arch, pattern etc in this world to make android app. Even bigger IT companies follow their own design patterns and some devs loves it, some don't. Anyway, back to your part questions:

  1. Consider Fragment and Activity just to be responsible for UI logic. That is loading views, changing view properties(button color, visibility etc), showing data on those views etc(getting from VM).
    MyFragmentAdapter-> yes, make separate adapter class with ViewHolder. I even make single class responsibility, that is separate ViewHolder class.
    Or all of this should go into a ViewModel?-> No, ViewModel holds no references to views! It just holds data and communicates with ui!

  2. Would you consider this a good practice? If you are looking into VM, then this is not a good idea. As I said above, Fragment(and Activity) is only responsible for UI logic. In VM, start your query, REST Api call etc and send that data to UI.

Main activity never dies so the fragments data never dies as well.-> TRUE, but the main MAGIC of VM is, it "survives" configuration changes. When activity recreates, VM is "the same" and holds all data needed to show on UI.

  1. Services holds no UI, no need for ViewModels.

  2. Ok, but I would consider every feature module with its own logic. Example: recipes-> ui-> fragment, adapter,viewmodel, di(dependency), data(api,repository), domain(usecases)

答案2

得分: 0

我不认为你正在进行的做法是好的。

无论你是遵循MVVM、MVP、MVC还是其他模式,思想都是将应用程序至少拆分为两个不同的层次:UI和逻辑。

在Android中,简而言之,这意味着不要在活动或片段中执行任何逻辑。

在MVVM中,所有的逻辑都应该放在ViewModel中,不应该放在活动或片段中。活动和片段应该仅通过观察ViewModel中的LiveData字段进行更新。LiveData并不是严格必需的,但应该使用一些观察者模式。目标是使你的视图尽可能地“愚笨”。实现数据驱动的UI。你的逻辑应该对平台一无所知,甚至不应该知道这是Android。

一些好处包括:
1)能够对所有内容进行单元测试(许多开发人员不理解这一点的重要性)
2)解耦,可以在不更改逻辑的情况下更改UI,反之亦然
3)可移植性,可以将相同的逻辑代码放到另一个平台或另一个应用程序中
4)更干净的代码,减少混乱,文件更小,遵循SOLID原则,更易于阅读、调试、重构、修改等。

英文:

I would not consider what you're doing good practice.

Whether you follow MVVM, MVP, MVC, or something else, the idea is to decouple your application into at least 2 different layers: UI and Logic.

What this means in a nutshell in Android is to not do any logic in your activities or fragments.

In MVVM, all logic should go in ViewModels. None should go in activities or fragments. Activities and fragments should be updated purely by observing LiveData fields in the ViewModel. LiveData isn't strictly necessary but some observer pattern should be used. The goal is for your views to be as dumb as possible. To have data-driven UI. And your logic should be ignorant of the platform, it shouldn't even know that this is Android.

Some benefits are:

  1. Ability to unit test everything (Many developers don't understand the importance of this)
  2. Decoupled, can change the UI without changing the logic and vice versa
  3. Portability, could take that same logic code and put it on another platform or in another application
  4. Cleaner code, less spaghetti, smaller files, follow SOLID principles, easier to read, debug, refactor, modify, etc.

huangapple
  • 本文由 发表于 2020年8月18日 01:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63455396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定