Created
November 18, 2016 16:38
-
-
Save exallium/5812a1992173d16fa66b3cf2405339fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Database interface | |
interface Database { | |
fun getUserById(id: String): Observable<User> | |
fun getAllUsers(): Observable<List<User>> | |
fun getUserChangeEvents(): Observable<ChangeEvent<User>> | |
} | |
// Describes what happened to what item | |
data class ChangeEvent<T>(val eventType: ChangeEventType, val t: T) | |
// Describes what happened to an item | |
enum class ChangeEventType { ADDED, REMOVED, CHANGED, MOVED } | |
// Concrete Impl | |
class DatabaseImpl : Database { | |
// Impl... | |
} | |
// Database module (optional, you could just add this to AppModule) | |
@Module | |
class DatabaseModule { | |
@Provides | |
@Singleton | |
fun provideDatabase(): DatabaseImpl() | |
} | |
// Application level module | |
@Module(includes=arrayOf(DatabaseModule::class)) | |
class AppModule { | |
/* ... */ | |
} | |
// Application level dagger component | |
@Singleton | |
@Component(modules=arrayOf(AppModule::class)) | |
class AppComponent { | |
fun database(): Database | |
} | |
class SomeFragment : Fragment { | |
@Inject | |
lateinit var database: Database | |
fun onCreate(/*...*/) { | |
database.getAllUsers().subscribe { | |
// Do something with your users | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment