Not a single software developer ever said that a more loosely coupled code gives him headaches. On the contrary, tight coupling can and will create a bunch of things that are hard to deal with.
But first, what is that “loose coupling”? Loose coupling means not instantiating dependencies of a class directly but getting them from a centralized place. Dependencies are simply classes which are needed by other classes. For example, a view model needs a repository, so the repository is a dependency of the view model.
Loose coupling also means that your classes operate with their dependencies through an interface and not directly with their concrete implementation. This allows you to swap implementations really easily. For example, a production implementation of a database will store data on a remote server, while a test implementation will store data only locally.
In this tutorial you are going to learn how to put what’s written above into practice by using a dependency injection framework called Kodein. It is specifically designed to work well with Kotlin and it’s also compatible with Java.
?Get the code from this tutorial?
I have learned a lot from your tutorials and have slowly gotten more comfortable with MVVM and Kotlin on Android (coming from Xamarin world and finally favoring native dev instead).
Please can you make this sample include validation because that’s something I’m struggling with.
By validation I mean, the button should be disabled unless both input texts are filled up. Thanks. Or at least can you make a little tutorial like that as I am not sure how to handle validation with 2 or more edit texts
Did you give up on Xamarin? I’m asking myself whether I should program natively or using Xamarin.
I’d suggest to check out Flutter.
I actually will not. Flutter has a big issue that I don’t see being fixed soon: Instance state not saved when app is killed by OS(https://github.com/flutter/flutter/issues/6827). I think for most reasonable apps, this is a big deal. My advice is to either go the React Native route as they plan to rewrite their entire architecture soon and they say it will be a seamless transition or just go the native route
I actually will advice against Flutter for now. Flutter has an issue that I think is very important for any reasonable sized app: Instance state not saved when app is killed by OS(https://github.com/flutter/flutter/issues/6827)and some other limitations that you should be interested in before diving into it(https://www.reddit.com/r/FlutterDev/comments/a6r5ow/very_excited_about_flutter_is_it_as_amazing_as_it/ebxjoow)
So @Rinaldi, I suggest either go the ReactNative route (ReactNative’s entire architecture is being rewritten codenamed fabric and it’s supposed to seamless) or just go the plain native route(which I think is the best option if there is no time constraint)
RN has its own limitations and performance concerns but I think it’s the best bet in the cross platform world (I quit Xamarin, too buggy. MS needs an overhaul in a lot of things. I also am still actively learning Flutter as it interests me a whole lot!)
I craete same app exactly with same code but i got this error in QuoteActivity on line code
val viewModel= ViewModelProviders.of(this,factory).get(QuotesViewModel::class.java)
the error : Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to org.kodein.di.KodeinAware
Same thing happened to me when trying to apply the injections.
In the end just forgot the first step i.e. declaring the application (the class app “MyApp” you are binding stuff to) in AndroidManifest.xml
ex :
<application
android:name=".MyApp"
android:allowBackup="true"
The following doc might be useful for refresher and in-depth explanations on the subject :
https://kodein.org/Kodein-DI/?5.0/android
Great tutorial. thansk
could you help us how can i save data into database with this implementation? you create some class as DataBase in this project structure and i’m not sure how can save data in database,
you save data from viewModel or repository class or Activity?
Thanks in advance
Actually, you should create a Data Access Object and work with the database directly only from there. Then you call this DAO from the repository, which is in turn called from the ViewModel.
I have a full tutorial series about MVVM app: https://www.youtube.com/playlist?list=PLB6lc7nQ1n4jTLDyU2muTBo8xk0dg0D_w
Do you have the final source code available ?