android10 / android10/Android-CleanArchitecture

Pass an object from the Presentation Layer -> Domain Layer -> Data Layer

Open
#72 33 comments 3 reactions 0 assignees View on GitHub
discussion question
Dominant language
Java
Stars
15.5k
Forks
3.3k
PR merge metrics
No merged PRs in 30d

Description

Hi @android10,

I have a couple of questions regarding how to pass an object from the presentation layer to the domain layer and then to the data layer.
I have a use case when a user signs up within my app.
1. Pass and object from the **domain layer** to the **data layer**:

This is the interface (in the domain layer) between the domain and the data layer:

``` java
public interface UserRepository {
//The User class belongs to the Domain Layer
Observable signUp(User user);
}
```

...and this is the UseCase subclass:

``` java
public class SignUpUseCase extends UseCase {
// Members set up in the SignUpUseCase constructor
private User user;
private UserRepository userRepository;

@Override
protected Observable buildUseCaseObservable() {
return userRepository.signUp(user);
}
}
```

Now, in the data layer, the implementation of the `UserRepository` interface (of the domain layer) is defined as follows:

``` java
public class UserDataRepository implements UserRepository {

private final UserEntityDataMapper userEntityDataMapper;

public Observable signUp(User user){

final UserDataStore userDataStore = userDataStoreFactory.createCloudDataStore();

// Declared variable to make this code easier to read
UserEntity transformedUserEntity = userEntityDataMapper.transform(user)

return userDataStore.signUp(transformedUserEntity)
.map(userEntity -> this.userEntityDataMapper.transform(userEntity));
}
}
```

This takes me to define 2 methods in the `UserEntityDataMapper` class as follows:

``` java
public User transform(UserEntity userEntity) {
...
}

public UserEntity transform(User user) {
...
}
```

**Is this a correct implementation on how to pass objects from the domain to the data layer?**
2. Pass and object from the **presentation layer** to the **domain layer**:

For the `SignUpUseCase` use case, I need a `User` (defined in the domain layer) to be supplied via it's constructor in the `UserModule`.

``` java
@Module
public class UserModule {

//The UserModel class belongs to the Presentation Layer
private UserModel userModel;

@Provides
@PerActivity
@Named("signUp")
UseCase provideSignUpUseCase(UserRepository userRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread){
return new SignUpUseCase(userModel, userRepository, threadExecutor, postExecutionThread);
}
}
```

But then, I'd need a User mapper to transform a `UserModel` (defined in the presentation layer) to a `User` (defined in the domain layer)

**Where should I place this User mapper, in the `UserModule` or in the `SignUpUseCase` class?**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.