function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
niki s 7niki s 7 

Difference btn LDS and apex controller

When should we use LDS and apex controller. Explain me with different scenarios
Best Answer chosen by niki s 7
ANUTEJANUTEJ (Salesforce Developers) 
Hi Niki,

>> https://www.salesforceben.com/salesforce-lightning-data-service-big-deal/

As stated in the above link LDS removes the need for using @AuraEnabled Apex methods for database interaction but more importantly, improves performance and user interface consistency. 

Although at a higher level lds is similar to standard controller in visual force where is differs is in mentioned as below:
 
Where LDS differs from a Visualforce standard controller is that LDS has a built-in querying and caching mechanism, which optimises the database interaction. Consolidated requests are made and stored in a highly efficient cache, the caching is shared across components on the users’ browser, so server requests are not made on cache hits. LDS further optimises the cache, by only storing the fields that have been requested. If a component subsequently requires additional fields, these are loaded transparently and added to the local record cache.

When a component updates the viewed record, all the other components using that record on the page are notified, and in most cases refreshed automatically. This retains UI consistency and eliminates the need for a programmatic refresh.
To read more in regards to the article you can check the above link.

Additionally, I would suggest you have a look at the below link as well:

>> https://salesforce.stackexchange.com/questions/312138/replace-apex-class-with-the-lds-in-lightning-component

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Niki,

>> https://www.salesforceben.com/salesforce-lightning-data-service-big-deal/

As stated in the above link LDS removes the need for using @AuraEnabled Apex methods for database interaction but more importantly, improves performance and user interface consistency. 

Although at a higher level lds is similar to standard controller in visual force where is differs is in mentioned as below:
 
Where LDS differs from a Visualforce standard controller is that LDS has a built-in querying and caching mechanism, which optimises the database interaction. Consolidated requests are made and stored in a highly efficient cache, the caching is shared across components on the users’ browser, so server requests are not made on cache hits. LDS further optimises the cache, by only storing the fields that have been requested. If a component subsequently requires additional fields, these are loaded transparently and added to the local record cache.

When a component updates the viewed record, all the other components using that record on the page are notified, and in most cases refreshed automatically. This retains UI consistency and eliminates the need for a programmatic refresh.
To read more in regards to the article you can check the above link.

Additionally, I would suggest you have a look at the below link as well:

>> https://salesforce.stackexchange.com/questions/312138/replace-apex-class-with-the-lds-in-lightning-component

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
This was selected as the best answer
niki s 7niki s 7
When should you go for apex and when lds
ANUTEJANUTEJ (Salesforce Developers) 
We would be going for lds when we are operating the dml operations on a single record in lightning components or lightning web components.

Where as you can use apex to handle server side logic in conjunction with visualforce page, you can also use apex with lightning components and lightning web components but we use lds so as to reduce the time that is being taken to make a call to server side logic written in apex.

Do note that in cases like when you are showing a datatable with list of records in such cases we will be using apex aura enabled methods to perform the fetching of list of records and sending it to lightning components or lwc.