Receive a quick refresher on dependencies, the dependency injection development pattern, and what Xamarin.Forms offers to facilitate development of this concept.
- [Teacher] So two terms that you might hear thrown around all the time are dependency service and dependency injection. There seems to be some confusion sometimes for developers on this concept of dependency injection. In fact, I've talked to developers that at first were thinking that this was similar to SQL or TSQL injection or denial of service attack or other things of that nature. They didn't realize that dependency injection was actually a good thing. Dependency injection is this concept that we have an inversion of control for resolving dependencies. So in a typical scenario, you would have a project, you would have references in that project that would serve as dependencies in that project and you would rely on the functionality and then child projects or nested projects would then be able to inherit from those.
The concept of dependency injection is that we do this in reverse, so we may have now platform specific dependencies that exist at the service level. So these might be IOS, Android or Windows objects that can only be accessed from those platform specific services, but we want to cull methods or expose methods up to the client in a common way, so that service dependency is being injected back up to the client. So an injection is really the passing of the dependency to a dependent object or in this example, a client that could then use it.
So the concept of dependency injection in creating dependency services is pretty powerful, but practically speaking how do we accomplish this in a Xamarin Forms app. So the actual implementation of a dependency service is really distributed across various levels meaning we're going to be putting code in different projects in our solution to fully implement a dependency service start to finish and these are really three or four different pieces depending on your perspective. Number one, we have an interface from interface declaration.
Number two, we have the platform specific implementation. This is always going to reside in platform specific code. We have the service registration, which is really a way for our dependency service to tell the client, hey, I exist, hey, I'm here, hey, you can use me, you're allowed to use me. And then finally, we have a service call calling into the method, which is really calling into an instance of the interface and accessing the actual functionality.
- Dependency service concepts
- Implementation concepts
- Registration concepts
- Calling a dependency service
- Creating a dependency service
- Platform-dependent concepts
- Implementing platform-dependent services
- Advanced scenarios