Search code examples
javaandroidarchitectureclean-architecture

How to arrange classes and interface in order to clean architecture in my data model?


I've created my app's structure in next way:

DataRepository class has 3 members' which are interfaces. Each interface serves the appropiate datas. The first member interface works with "Owner" model datas, the second one works with "Dog" model datas. And the third must work with "Breed" model. First and second datas (Owner and Dog) are input from user and stored locally in SQLiteDatabase and i have classes Dog/Owner/SQLDataSources classes, that implement those interfaces.

The third data model is my problem. This models' list named "Breed" i download from internet, then i have to write it in database and then to get List<Breed>. As a result my WebBreedDataSource uploads List<Breed> from API and SQLBreedDataSource fills the database (TableBreed) with that list. So these both classes have the same method getBreeds(). So i don't know which class of those does have to implements IBreedsDataSource interface? How to arrange this architecture clean? May be i should create some another class that uses those both and implements IBreed interface

Thanks a lot for advice! enter image description here


Solution

  • If I understand your problem correctly, I would probably add another another class between your repository and your database / api controller. This class can implement your interface to get your List<Breeds> and it also does the "coordination" between database and api.

    for example: you want to get list of breeds, okay, this "DataSourceCordinator" object does this: is the database empty?

    • If it is empty, call api , fill database and retrieve the list
    • otherwise retrieve what is currently in database.

    You probably need something that can coordinate these two data sources somehow.

    One more note, I imagine repository a bit differently than you do, I would add different data sources into single repository but for the same type of data - not mixing different types of data in one repository - only adding different ways to retrieve same type of data

    for example

    • for breeds you would have one repository with two data sources - database and api
    • for dogs you would have different repository with one data source - api

    Now in each repository I would coordinate these different data sources. Btw thats a nice diagram!