Dependency Injection
Automatic discovery and creation of services, view and components
Dynamics Mobile SDK eliminates the need for the developer to create class instance for certain classes. The framework automatically discovers if specific class instance must be created and automatically creates it for the developer. The developer in this case needs to only declare the need for an instance of a specific class and then just use it.
Such a concept of class instance discovery and creation is used widely in the software industry today and known as Dependency Injection, which is a part of a more broad concept called Inversion of Control
You might noticed already that above we talk about automatic discovery and creation of certain classes. It means that in order for the framework to be able to discover and inject instance of specific class, the class must be "marked" as injectable.
Dynamics Mobile SDK defines 3 types of injectable classes:
A view is a class, which has a number of well-defined methods with fixed signatures, which controls the rendering and behavior of the UI presented to the user. When the developer wants to display certain screen to the user, the developer needs to implement View class.
Let's see how we can define a view class:
//file-name: customer-list-view.ts
import { View } from '@dms';
@View()
export class CustomerList {
}
Actually in order for us to declare a view class, we need to place a decorator for our class called @View(). This instructs the framework that this class is a View ( e.g. it will be responsible to render UI ) and that it will be able to eventually take advantage of the automatic injection of other class instances.
A service class is such class, which has a decorator @Service and provides non UI related functionalities. Such a class may fetch list of customers from the local on-device database or from a remote source and then filter them based on their revenues and returning the top 10 by revenue. We can declare a service class like this:
//filename: customer-service.ts
import { Service } from '@dms';
import { Customer } from '@dms-bo';