Use Model Mapper during DTO between different layers?


#1

Modle mapper library

UserNetworkresponse.java -> Network layer
User.java -> Entity (Storage layer)
UserProfile.java -> UI layer

public class UserResponseModel {

@SerializedName("basic_token")
private String basicToken;

@SerializedName("phone_no")
private String phoneNo;

@SerializedName("user_id")
private long userId;

@SerializedName("basic_token")
private String basicToken;

@SerializedName("user_name")
private String userName;

@SerializedName("user_email")
private String userEmail;

@SerializedName("user_address")
private String userAddress;

@SerializedName("company_address")
private String companyAddress;

//more fields

}

==================

@Entity(tableName = “trip”)
public class UserEntity{

@PrimaryKey
@NonNull
public final long userId;

//constructor

@ColumnInfo(name = "auth_token")
public String basicToken;

@ColumnInfo(name = "user_phone_number")
public String phoneNo;

public String basicToken;

public String userName;

public String userEmail;

public String userAddress;

public String companyAddress;

//more fields

}

==================

public class UserProfile implements Serializable(or Parcelable){

private long userId;

private String userName;

private String userEmail;

private String userAddress;

private String companyAddress;

// no more fields

}

==================

Questions:

  1. UserResponseModel is the response received from network layer(retrofit). Should I map UserResponseModel to UserEntity uisng modelmapper? If yes then in which layer?
  2. While showin on UI UserProfile modle is used. When accessing data from caching layer or storage layer should I map UserEntity to UserProfile uisng modelmapper?
    If yes then in which layer?
  3. User can edit information on UI screen and UserProfile modle is updated according using two way data binding. UserProfile needs to be persisted in storage layer.
    Should I map UserProfile to UserEntity uisng modelmapper? If yes then in which layer?
  4. Now the updated UserEntity needs to be synced with the server. UserResponseModel is used as the request modle with retrofit. Should I map UserEntity to UserResponseModel
    uisng modelmapper? If yes then in which layer?

What does in which layer means?
Either in presentation layer, domain layer, data layer.

I feel the modle mapper is required because I cannot mix and match and let UserEntity implement Parcelable. Layers are more independent and also unit testable.

It wold be good if the above could be explained with additional source code which could be from other open source projects. May be a web link from github.

What do you have to say?


#2

I do not know if you are using the term “modelmapper” to refer to something specific, like a library.

With respect to #1 and #2, what sorts of conversions you do, and between what representations, depends a lot on your architecture. I cannot tell you to always or never do the specific things that you suggest. My general recommendation is that you treat server responses and database responses as being data transfer objects (DTOs) — they are not necessarily the same as one another, and neither are necessarily the same as what you use as POJOs in your UI.

With respect to #3 and #4, again, this depends a lot on your architecture. I cannot tell you what architecture to use and how your proposed code fits there. Personally, I would stick with the POJO representation until as late as possible, and so I would hide the DTOs inside of a repository. Personally, I tend not to think in terms of layers as you are describing. But those are my personal approaches, and I do not claim to be an expert on all aspects of Android app architecture.

If you have specific programming questions, I can try to help with those. I apologize for not being more useful with your architecture concerns.


#3

I am not using the term to refer to some specific library. The library in the questions is just for the reference.
I am using the term layer in reference to “Clean Architecture” by Robert C. Martin.

Thankyou