Reputation: 73
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?
Upvotes: 3
Views: 2811
Reputation: 1030
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.
Upvotes: 1
Reputation: 490
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.
Upvotes: 2
Reputation: 1031
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.
Upvotes: 2