1

I'm working on an Android app in Kotlin and am trying to implement Clean Architecture with viewmodels, repositories, datasources, interfaces etc.

Right now I have a DTO class that I'm parsing with Retrofit:

@Parcelize
data class NewsArticleDto(
    val author: String, val title: String, val description: String,
    val url: String, val urlToImage: String, val publishedAt: String, val content: String
) : Parcelable

I would like to have an interactor called "GetNews" get the data from NewsRepository which in turn gets it from the datasource (retrofit), map it to NewsArticle entities and send a list of NewsArticles back to the viewmodel.

The data layer is inside a different module("core"), which is a java library. The repository/interactors dont know about the DTO class since it's in the "app" module.

The core module isn't supposed to know about the "app" module. It's also not supposed to have SDK-logic, as I understand it.

So the problem here is that the interactor doesn't know about a DTO class and if I move it altogether to the "core" module, Android Studio can't handle the @parcelize and parcelable.

How do I structure my project or am I overengineering things and should I leave the DTO altogether in the "app" module? If so, I still won't be able to use a "GetNews" interactor.

This is the GetNews interactor:

class GetNews(private val newsRepository: NewsRepository) {
    suspend operator fun invoke(keyword: String, apiKey: String): Result<List<NewsArticle>> =
        try {
            newsRepository.getNews(keyword, apiKey) as Result<List<NewsArticle>>
        } catch (throwable: Throwable) {
            Result.Failure(throwable)
        }
}

What I have tried:

This is my project structure:

Text](https://imgur.com/eQ14M7H)[![enter image description here]1

Any help is appreciated.

JeeferSan
  • 21
  • 6
  • May I ask why you need the DTOs to be Parcelable? If you don't have to, then you can move the DTO to the data layer, since that's where they "belong" in theory. – Segun Famisa Mar 03 '20 at 15:25
  • It's not 100% necessary to be parcelable. it's just that from what I understand, parcelable objects are faster in terms of performance. Please correct me if I'm wrong – JeeferSan Mar 04 '20 at 09:26
  • Oh not quite. The only case where Parcelable objects become beneficial is when you are trying to serialize the objects and pass them through intents or bundles (through activities, or saving them in the saved instance state bundle). And I assume you are not doing any of these with the DTO objects. Look at [this answer](https://stackoverflow.com/a/23647471/5992987) for when and how parcelable objects perform better than serializable ones. – Segun Famisa Mar 04 '20 at 09:46
  • One more reason to make it a parcelable object is that I have to pass it to another fragment. So I either have two options: create another data class in presentation layer that implements parcelable and map the objects in my viewmodel, or I could use a shared viewmodel between those fragments. The latter imo breaks the rule of single responsibility and the first seems a bit much. Looks like it's a matter of a compromising choice – JeeferSan Mar 08 '20 at 10:20
  • 1
    Yes, that's why I asked the question. I thought you may plan to pass the objects around. I believe that with clean architecture, you should not have the DTOs in the UI layer and definitely should not pass a DTO through fragments. Instead, you should have an object different from the DTO that should drive your presentation layer. – Segun Famisa Mar 09 '20 at 11:03

0 Answers0