In Autofac, the IContainer interface provides the dependency injection container. For more information about Autofac, see Autofac on readthedocs.io. Autofac facilitates building loosely coupled apps, and provides all of the features commonly found in dependency injection containers, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into constructors of objects that it resolves. There are many dependency injection containers available, with the eShopOnContainers mobile app using Autofac to manage the instantiation of view model and service classes in the app.
In the context of a Xamarin.Forms app that uses MVVM, a dependency injection container will typically be used for registering and resolving view models, and for registering services and injecting them into view models.
XAMARIN FOR VISUAL STUDIO 2015 COMMUNITY REGISTRATION
However, using a container provides additional capabilities such as lifetime management, and registration through assembly scanning. If those dependencies have not yet been created, the container creates and resolves their dependencies first.ĭependency injection can also be implemented manually using factories. During the objects creation, the container injects any dependencies that the object requires into it. The class that's responsible for instantiating the IOrderService object, and inserting it into the ProfileViewModel class, is known as the dependency injection container.ĭependency injection containers reduce the coupling between objects by providing a facility to instantiate class instances and manage their lifetime based on the configuration of the container. Therefore, the ProfileViewModel class doesn't have any knowledge of the class that's responsible for instantiating the IOrderService object. The only dependency in the ProfileViewModel class is on the interface type. The ProfileViewModel constructor receives an IOrderService instance as an argument, injected by another class.
XAMARIN FOR VISUAL STUDIO 2015 COMMUNITY CODE
The following code example shows how the ProfileViewModel class is structured when using dependency injection: With dependency injection, another class is responsible for injecting dependencies into an object at runtime. Introduction to Dependency Injectionĭependency injection is a specialized version of the Inversion of Control (IoC) pattern, where the concern being inverted is the process of obtaining the required dependency. Therefore, this chapter will focus solely on performing constructor injection with a dependency injection container.
There are also other types of dependency injection, such as property setter injection, and method call injection, but they are less commonly seen.
It generally uses a container that holds a list of registrations and mappings between interfaces and abstract types, and the concrete types that implement or extend these types. The dependencies the object needs are injected into the constructor.īy specifying dependencies as interface types, dependency injection enables decoupling of the concrete types from the code that depends on these types. This is an example of dependency injection, and specifically is known as constructor injection. Typically, a class constructor is invoked when instantiating an object, and any values that the object needs are passed as arguments to the constructor.