Repeatedly looping over same Records property in Domain class #332
-
Hi all, This week we have started adopting the Enterprise framework in our project and it is really interesting to discover all that it has to offer! But there is something I can't wrap my head around yet; I have been taught to keep methods small, really small! They should do one thing and one thing only. So for instance if I need to update the owner, as well the status of a given Case record, I would separate these two operations into two small methods in the Domain layer. By doing this, I believe I increase the reusability of those methods. However now the problem is that it seems a bit odd and expensive to call each method individually from the Service layer. With that I mean, with each method invocation in the Domain layer it enters a for loop. As each method defined in the Domain layer is looping over the Records property defined in the fflib_SObjectDomain class. This is done as such in most of the examples that I saw in the book 'Salesforce Lightning Platform Enterprise Architecture - by Andrew Fawcett' and in this sample code I am wondering now if I should merge the two methods into one method, although it then becomes less reusable. Or if I should remove the for loop statements in the domain class and instead pull in the records from the Records property in my Service implementation class and then invoke each method in the execution block and pass a single record as an argument for processing in the respective method. Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @jens-dazn, Great to hear that you are also adopting the Enterprise patterns! In my company I hear your question often from people who are just starting to learn the framework. It is indeed true that you would want to create two separate methods iterating over all the records of the domain. Each method is just only changing their own field. But, if the goal was to write very fast software, then we all would have been writing in Assembly code. Then you will spend a huge amount of your own CPU (your own brain :-P ) to read it. So, we need to compromise. The next question you might ask is; what about the limits, if we use more CPU we will start hitting the limits. But luckily Salesforce have given us a wide range of feature that we can use to avoid that limit. In your case your domain and service would indeed look something like:
Sometimes it can be that there is one method on the domain combining two other setter methods. That method then contains domain specific business logic. Like when we need to close a case, and by doing that we need to update multiple fields on the same records. Like in this example:
With this approach, we only need to invoke a single method in the service method.
|
Beta Was this translation helpful? Give feedback.
-
@wimvelzeboer I should read these closed issues more often; I too did not known about |
Beta Was this translation helpful? Give feedback.
Hi @jens-dazn,
Great to hear that you are also adopting the Enterprise patterns! In my company I hear your question often from people who are just starting to learn the framework. It is indeed true that you would want to create two separate methods iterating over all the records of the domain. Each method is just only changing their own field.
And yes, this indeed would look like a kinda CPU expensive way of doing things.
But, if the goal was to write very fast software, then we all would have been writing in Assembly code. Then you will spend a huge amount of your own CPU (your own brain :-P ) to read it. So, we need to compromise.
Since your salary is in most cases more expensive than b…