OiO.lk Blog Android Where should I inject mapper and network chekc in Uncle bob clean architecture
Android

Where should I inject mapper and network chekc in Uncle bob clean architecture


I am learning about Uncle bob clean architecture. Full Source code is uploaded on Github

Does UseCase , Repository and Data Sources return the same return type ?

Mapper should be injected at which level Repository or Data Source?

Network Check should be done at Repository level or Data Source Level ?

Domain Layer

BaseUseCase.kt

abstract class BaseUseCase<ResultType, in ParamsType> {
    abstract fun execute(params: ParamsType): Flow<Either<Failure, ResultType>>

    // Operator function to simplify calling the use case
    operator fun invoke(params: ParamsType): Flow<Either<Failure, ResultType>> {
        return execute(params)
    }
}

// A class representing no parameters
object NoParams

LoginUseCase.kt

class LoginUseCase @Inject constructor(
    private val loginRepository: LoginRepository
) : BaseUseCase<LoginResponseEntity, LoginRequestEntity>() {

    override fun execute(params: LoginRequestEntity): Flow<Either<Failure, LoginResponseEntity>> {
        return loginRepository.login(params)
    }
}

LoginRepository.kt

interface LoginRepository {
    fun login(params: LoginRequestEntity): Flow<Either<Failure, LoginResponseEntity>>
}

Data Layer

LoginRepositoryImpl.kt

class LoginRepositoryImpl @Inject constructor(
    private val loginDataSource: LoginDataSource,
    private val loginRequestMapper: LoginRequestMapper,
    private val loginResponseMapper: LoginResponseMapper
) : LoginRepository {

    override fun login(params: LoginRequestEntity): Flow<Either<Failure, LoginResponseEntity>> {
        val loginRequestDTO = loginRequestMapper.mapToDomainModel(params)
        return loginDataSource.login(loginRequestDTO)
            .map { result ->
                result.map { loginResponseMapper.mapToDomainModel(it) }
            }
    }
}

I have changed LoginDataSource request and response to Network DTO . Am I doing it correctly. As if any api change I only have to change Data Source
LoginDataSource.kt

interface LoginDataSource : BaseDataSource {
    fun login(request: LoginRequestDTO): Flow<Either<Failure, LoginResponseDTO>>
}

LoginDataSourceImpl.kt

class LoginDataSourceImpl @Inject constructor(
    private val apiService: ApiService,
    @IoDispatcher private val dispatcher: CoroutineDispatcher
) : LoginDataSource {

    override fun login(request: LoginRequestDTO): Flow<Either<Failure, LoginResponseDTO>> {
        return flow {
            emit(safeApiCall { apiService.loginAuthentication(request) })
        }.flowOn(dispatcher) // Ensuring network call is on IO thread
    }
}



You need to sign in to view this answers

Exit mobile version