Utils classes are belongs to which layer in MVVM architecture?

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

Utils classes are belongs to which layer in MVVM architecture?

问题

I'm currently working on an Android project following the MVVM architecture pattern. While organizing my code, now I'm confused about the placement of utility classes. In which package should I place utility classes?

I want to create some utility classes like a Custom Toast class, Network state checking class by pinging the API website that I'm using in my project. Is creating static methods in these classes considered bad practice? And is using these classes directly in the activities bad practice for maintaining MVVM architecture? I'm updating my activities' data through the ViewModel and LiveData observer.

As we know in MVVM, we have the model layer, view layer, and ViewModel layer. In some articles, I saw some developers saying utils belong to the model layer, and somewhere I saw utils belong to the view layer.

Additionally, I'm also interested in understanding how the placement of utility classes can impact the overall maintainability, scalability, and testability of the codebase. Are there any best practices or guidelines that can help in making this decision?

Any guidance or experiences shared would be greatly appreciated. Thank you in advance for your help!

英文:

I'm currently working on an Android project following the MVVM architecture pattern. While organizing my code, now I'm confused about placement of utility classes. In which package i should place utils classes?

I want to create some utility classes like Custom Toast class , Network state checking class by pinging the api website that im using in my project. and also creating static methods in these classes will be bad practice? and using these class directly in the activities will be bad practice for maintaining mvvm architecture? as im updating my activities data through viewmodel and livedata observer.

as we know in MVVM, we have the model layer, view layer, and view model layer.In some article i saw some developer saying utils bilongs to model layer and somewhere i saw utils belongs to view layer.

Additionally, I'm also interested in understanding how the placement of utility classes can impact the overall maintainability, scalability, and testability of the codebase. Are there any best practices or guidelines that can help in making this decision?

Any guidance or experiences shared would be greatly appreciated. Thank you in advance for your help!
My packages:

Utils classes are belongs to which layer in MVVM architecture?

答案1

得分: 3

我喜欢遵循的一种做法是选择最窄的范围来编写实用代码,并仅在必要时扩展它。

假设您想引入一个函数,该函数返回字符串的最后3个字符 - 最佳位置将是放置该函数的地方是什么?如果它仅被代码库中的一个类使用 - 只需将该函数设置为该类中的私有函数。如果同一包中有多个类需要此函数 - 您可以将其移动到该包中的单独文件中。如果它在所有地方都被使用(这是罕见的) - 它可能可以放在“utils”包内的一个文件中。

我建议将实用工具放置在它们使用的地方附近:自定义 Toast 类将位于“views”包中,而网络状态检查类可以位于“network”包中。想象一下,如果您希望将代码库拆分为相互依赖并具有第三方依赖的单独Gradle模块:将网络实用程序放在“views”模块中将意味着该模块现在需要依赖于Retrofit,这不是理想的情况。因此,更好的位置将是“network”模块,对于该模块来说,依赖于Retrofit感觉更加自然。

英文:

A practice I like to follow is to choose the most narrow scope for your utility code and expand it only when necessary.

Let's say you want to introduce a function that returns the last 3 characters of a string - what would be the best place to put that function? If it's only used by a single class in your codebase - just make the function private in that class. If there are multiple classes in the same package that need this function - you can move it to a separate file in that package. If it's used everywhere (which is rare) - it can probably live in a file inside a "utils" package.

I'd suggest placing utils closer to where they're used: the custom Toast class will live in the "views" package, while the Network state checking class can live in the "network" package. Imagine at some point you wanted to split your codebase into separate Gradle modules that would depend on each other and have 3rd party dependencies: having networking utils in the "views" module would mean that module now needs a dependency on Retrofit, which isn't ideal. Hence a better place for it would be the "network" module, for which having a Retrofit dependency feels more natural.

huangapple
  • 本文由 发表于 2023年5月29日 20:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357303.html
匿名

发表评论

匿名网友

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

确定