简体   繁体   English

MVVM 清洁架构中是否真的需要 Data Mapper 层?

[英]Does the Data Mapper layer is really required in MVVM clean architecture?

I have seen in some codebases that they are using mapper layer with usecase and repo layers.我在一些代码库中看到他们正在使用带有用例和 repo 层的映射器层。 We can have the mapper logic inside the use case layer right?我们可以在用例层中拥有映射器逻辑,对吗? or is there any specific reason why we need that layer?或者我们需要那个层有什么具体原因吗?

Yes, you can, but it's usually a good idea to user a mapper.是的,你可以,但使用映射器通常是个好主意。 Mapper is not MVVM specific, the reason to use the mapper pattern is to isolate the logic making it easier to test and reuse the conversion logic in other classes if necessary. Mapper 不是特定于 MVVM 的,使用mapper 模式的原因是为了隔离逻辑,以便在必要时在其他类中测试和重用转换逻辑。

It's a kind of tradeoff when you are designing your architecture.在设计架构时,这是一种权衡。 In principle, application architecture is a code breakdown process by logical groups.原则上,应用程序架构是逻辑组的代码分解过程。 Logic defines groups as related entities with the same level of abstraction.逻辑将组定义为具有相同抽象级别的相关实体。 So in applications, logical groups are the view layer, business use-case layer, data management layer.所以在应用程序中,逻辑组是视图层、业务用例层、数据管理层。 If you want to break down these layers into their logical sub-components it is possible but it reduces code readability and could slow down development.如果您想将这些层分解为它们的逻辑子组件,这是可能的,但它会降低代码的可读性并可能会减慢开发速度。 on the other hand, it gives you the ability to test sub-components.另一方面,它使您能够测试子组件。 So it's really a tradeoff that you have to deal with when designing your architecture.因此,在设计架构时,这确实是您必须处理的权衡。 mapper is an example of breaking down the domain layer further and coordinator is an example of breaking down the view layer further. mapper 是进一步分解域层的示例,协调器是进一步分解视图层的示例。

Mapper Pattern has is not strongly bound by the CA, this mechanism is used to separate framework models with the domain models. Mapper Pattern 不受CA 的严格约束,这种机制用于将框架模型与领域模型分开。 Generally when we talk about the Mapper we normally refer to Clean Architecture and why is that so?一般来说,当我们谈论 Mapper 时,我们通常指的是 Clean Architecture,为什么会这样?
CA is basically a "layered Architecture" which gives the lose coupling to dependencies in different layers of the architecture. CA 基本上是一个“分层架构”,它使架构不同层的依赖关系失去耦合。 When we make the Network calls or DB calls we use the models that belong to the Framework layer but when we pass the data from the framework to domain or usecases or presentation layer what we are gonna do?当我们进行网络调用或数据库调用时,我们使用属于框架层的模型,但是当我们将数据从框架传递到域或用例或表示层时,我们要做什么? Break the architecture?破坏架构? use the same data models?使用相同的数据模型? NO, we would use mapper mechanism to map the framework model to the domain model to keep the separations and as well as( I should have mentioned it earlier ) Unit Testing.不,我们将使用映射器机制将 map 框架 model 映射到域 model 以保持分离以及(我应该在前面提到过)单元测试。
So Can we use Mapper Pattern with MVVM, MVP or any other presentation layer architecture?那么我们可以将 Mapper 模式与 MVVM、MVP 或任何其他表示层架构一起使用吗? Yes you can.是的你可以。
You can make the model which is consumed by ViewModels,Presenters/Views and use the Framework models with the Networks(DTO) or DB(Entities) and use the mapper pattern inside the Repository.您可以制作视图模型、演示者/视图使用的 model,并将框架模型与网络(DTO)或数据库(实体)一起使用,并在存储库中使用映射器模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有实时数据的 Android MVVM 清洁架构 - Android MVVM clean architecture with livedata MVVM - 很难理解如何在 Clean Architecture 中创建域层 - MVVM - Having a hard time understanding how to create the Domain layer in Clean Architecture 清洁架构:如何在UI中反映数据层的变化 - Clean Architecture: How to reflect the data layer's changes in the UI 可以在 Clean Architecture 的上下文中在数据层中调用用例吗? - Can a use case be called in data layer in context of Clean Architecture? 使用Architecture Components构建的应用程序是否将其命名为“ MVVM”,如果是,则应用程序的哪些部分与MVVM的哪一层相关? - Does an app built with Architecture Components make it “MVVM” and if yes, which parts of the app relate to which layer of MVVM? 功能与 MVVM 相似的 2 个片段的干净架构? - Clean architecture for 2 fragments that are similar in function with MVVM? 在 Clean architecture MVVM 中,将对象转换为适配器? - In Clean architecture MVVM where transform objects to adapter? 如何在 MVVM 中为 Repository 提供 Drawable,干净的架构 - How to Providing Drawable for Repository in MVVM, Clean Architecture 如何在 android 中为 MVVM Clean 架构创建用例 - How to create UseCase for MVVM Clean architecture in android 刷新MVVM体系结构数据 - Refresh MVVM architecture data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM