MVVM clean 架构中真的需要 Data Mapper 层吗?
我在一些代码库中看到他们使用带有用例和存储库层的映射器层。我们可以将映射器逻辑放在用例层中,对吧?或者我们需要该层有什么具体原因吗?
I have seen in some codebases that they are using mapper layer with usecase and repo layers. We can have the mapper logic inside the use case layer right? or is there any specific reason why we need that layer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,可以,但使用映射器通常是个好主意。 Mapper 不是 MVVM 特定的,使用映射器模式的原因是隔离逻辑,以便在必要时更容易测试和重用其他类中的转换逻辑。
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 是进一步分解领域层的一个例子,Coordinator 是进一步分解视图层的一个例子。
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模式不受CA的强约束,这种机制用于将框架模型与领域模型分开。一般来说,当我们谈论映射器时,我们通常指的是干净的架构,为什么会这样?
CA 基本上是一种“分层架构”,它使架构不同层中的依赖关系失去耦合。当我们进行网络调用或数据库调用时,我们使用属于框架层的模型,但是当我们将数据从框架传递到域或用例或表示层时,我们要做什么?打破架构?使用相同的数据模型?不,我们将使用映射器机制将框架模型映射到领域模型以保持分离以及(我应该早点提到)单元测试。
那么我们可以将 Mapper 模式与 MVVM、MVP 或任何其他表示层架构一起使用吗?是的,你可以。
您可以创建由 ViewModels、Presenters/Views 使用的模型,并将框架模型与网络(DTO)或 DB(实体)一起使用,并在存储库中使用映射器模式。
Mapper Pattern has is not strongly bound by the CA, this mechanism is used to separate framework models with the domain models. Generally when we talk about the Mapper we normally refer to Clean Architecture and why is that so?
CA is basically a "layered Architecture" which gives the lose coupling to dependencies in different layers of the architecture. 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.
So Can we use Mapper Pattern with MVVM, MVP or any other presentation layer architecture? 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.