Previous episode: 08. Parse Domains
Next episode: 10. Conclusion
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Earlier, when introducing the model and repository, we discussed the app architecture. A key aspect of architecture is how your app components in classes are connected to one another, or depend on one another. For example, here we have a garage class that has a strong reference to a car. This is strongly coupled. Meaning, if anything changes with the car's implementation, the garage may need to be changed as well. As mentioned, this is strong coupling. To avoid this problem, we define abstract classes to get around it. In our case, we'll define a car as a type of vehicle and code against the vehicle versus coding against the car. That way if the car's implementation changes, the garage won't be affected. This is called loose coupling. Dependency injection is used alongside abstract classes to achieve loose coupling. In our app, the course repository property is created directly inside the courses controller class that introduces a strong coupling between the controller in the courses repository. First, we should abstract our course repository to an abstract type. This makes our app adaptable and more importantly, capable of dependency injection. Now, we can simply pass in a course repository to the courses controller. That way the user of the controller can decide what concrete type of repository to pass to the controller, and the controller is no longer responsible for creating it. The controller only knows about the repository interface and can work with any kind of repository. For testing your controller, that will let you pass a test repository to the controller instead of using the courses repository that makes real network calls. Passing in the property using the constructor is a form of dependency injecting. Since you are passing or injecting the dependency into the controller, instead of just creating it inside the controller. Like with our architecture discussion, we won't go too deep into this theory here. This episode is just meant to expose you to the idea of dependency injection, so when you come across it again someday, you'll have some background on what it means and why it is used. To get started, open your project in progress, or download the starter project. With your project open, open up course controller.dart in the courses folder located in the ui folder. So here we are setting the repository directly in the controller. This isn't a form of dependency injection, we want to pass in the repository instead. Update the property to the following. Now, let's delete the unneeded import. Now, we need to set up the repository. Add a constructor for the controller. The constructor lets you inject the repository into the controller. The controller is no longer aware of a specific repository type and only knows about the interface of the repository. So now we can inject a course repository. Open up courses_page.dart, add the following, and then add the import. Rerun the app to make sure all is good. It looks and behaves exactly the same, except this time, you are injecting the repository instead of hard coding it. It's a small change, but you've made the code flexible to change and easier to test as well.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.