Last active
May 4, 2016 19:10
-
-
Save ZakTaccardi/26daf5b9341ef95e392215e85c873762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Handles getting a user. | |
*/ | |
public interface UserModel { | |
/** | |
* @return the currently logged in user, or null if the user is not logged in | |
*/ | |
Single<User> getUser(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simplified implementation of {@link UserModel}. The actual implementation of this class caches user in memory, loads it from the database, or from the network. | |
*/ | |
public class UserModelImpl implements UserModel { | |
//UserDb is an interface. Its implementation, UserDbImpl, has android dependencies and actual reads or writes a user to the database. | |
//UserDbImpl is part of the `:data` layer. | |
UserDb userDb; | |
//null by default | |
User currentUser; | |
public UserModelImpl(UserDb userDb) { | |
this.userDb = userDb; | |
} | |
@Override | |
public Single<User> getUser() { | |
return Single.concat( | |
Single.just(currentUser), | |
userDb.readUser() | |
) | |
.filter(user -> user != null) | |
.first() | |
.onErrorReturn( | |
throwable -> { | |
if (throwable instanceof NoSuchElementException) { | |
Timber.d(throwable, "Error"); | |
return null; | |
} else { | |
throw new RuntimeException(throwable); | |
} | |
} | |
) | |
.doOnNext( | |
//cache user in memory so next time the DB doesn't need to be accessed | |
user -> currentUser = user | |
) | |
.toSingle(); | |
} | |
} | |
Apologies, I drastically simplified the code for my UserModelImpl
class to use as an example. Maybe too much, so it looks like just a facade. I've updated it to add more complexity.
If I have given you enough context, would you say UserModelImpl
belongs in the "business" layer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based upon what I see here, this looks like a form of a facade (which would be between the UI layer and DB layer). So, not really a data/repository layer. Some people would call this the service layer or a manager of sorts. I hope that helps, it's hard with very little context (and everyones situation is ultimately different).