To note: I've just implemented fflib and have thus separated TriggerHandler logic from SObject Domain classes. That is, I have (for example) two classes:
- AccountsTriggerHandler
extendsfflib_SObjectDomain - Accounts
extendsfflib_SObjectsimplementsIAccounts
Let's say I have a simple method on Accounts:
public void setDescription(fflib_ISObjectUnitOfWork uow) { for(Account a : (List<Account>) getRecords()) { a.Description = 'My Description'; } uow.registerDirty(getRecords()); } I'm passing in the Unit of Work because I've seen this in several places of sample code for fflib. And, that's all well and good if the calling context is not a Before Trigger. But if it is, the Unit of Work shouldn't be used at all...so do I instantiate one, pass it in, but then never call commitWork()?
I could instead define the method without the Unit of Work parameter and just pass back the list of records so that the Trigger or any other calling context can decide what do to with it. That seems fine for this simple logic, but if there's more complexity within the method logic that requires calling various register methods throughout, that may not suffice. Though, admittedly, this may be an entirely theoretical issue since any such complex logic is unlikely to be needed in both a before trigger and from other calling contexts.
Or, am I just overthinking this and I should define the method now without a Unit of Work since that's what I need right now...then deal with other contexts if/when it's ever needed in the future?
setDescriptionshould be done only in a trigger context, it should be a method inAccountsTriggerHandler. One would typically use uow in a domain class method if it were being called from a Service layer