Lightning Data Service – The standard controller for lightning
What is it?
The lightning data service (LDS) is essentially a data layer for lightning, acting as an interface between the lightning components and the server, as shown in the pic above.
1. No Apex No SOQL
When you are retrieving or sending data via the LDS, you dont need to have your own Apex controller and you don’t need to write any Aura methods and there is no need for any SOQL either. Everything is taken care of by the LDS.
2. FLS and sharing rules
You remember the with sharing we had to use with our Apex controller back in the Sample Student App? With LDS you don’t need to worry about that. LDS enforces it automatically.
3. Client-side shared caching
LDS is built on top of an awesome client-side caching mechanism and the best part? It is shared caching. What it essentially means is that any number of components referencing the same record can benefit from this shared cache. If one of the components has made a request for the record then the others don’t need to maintain their own cache for the same record. In fact if multiple components are dependent on the same record then only the first one to fetch it actually hits the server. The remaining components can reference it from the shared cache. Do you see the performance benefit??
4. Auto notification on change
If one of the components makes a change to the record then the others are immediately notified. So no need to worry about data inconsistency.
5. Offline support for Salesforce 1 mobile app
This is obvious. Because of the amazing client-side caching, there is offline support for the Salesforce 1 mobile app. It essentially means, a Sales rep can make changes on the fly even when not connected to the server and once he/she is connected back everything is synced back to the server. Isn’t it amazing?
That’s why the LDS is often referred to as the standard controller for lightning components!
1. As of now LDS supports only one record at a time and cannot be used to retrieve bulk data.
2. If you need to implement some complex logic in the server side then you need to fall
back on your good old Apex controller.
3. If the record gets changed by any other means except for the components (e.g., data loader or workbench) then auto-nofitication won’t work.
LDS with Apex Controller ?
Yes. You can use LDS in conjunction with an Apex controller. Because the truth is, in real world scenarios, you would hardly ever have situations where a given component will only deal with a single record and that too straight forward CRUD operations. You will most often deal with complex business scenarios involving complex logic for which you would need to have your own Apex controller. You can still leverage caching with Apex controller, though it won’t be because of LDS. But that is for another time and another post.
My next post would demonstrate a live example of the powers of LDS (with source code of course!)