1

Can somebody with experience explain how the registerNew() method in the fflib_SObjectUnitOfWork class works? I do not understand how you can have two methods with the same name and different params. On top of that, I do not understand where this class holds all the to be inserted records.

Link below for those of you that are not sure what I am talking about.

https://github.com/financialforcedev/fflib-apex-common/blob/master/fflib/src/classes/fflib_SObjectUnitOfWork.cls

public void registerNew(SObject record)
{
    registerNew(record, null, null);
}

/**
 * Register a list of newly created SObject instances to be inserted when commitWork is called
 *
 * @param records A list of newly created SObject instances to be inserted during commitWork
 **/
public void registerNew(List<SObject> records)
{
    for (SObject record : records)
    {
        registerNew(record, null, null);
    }
}
  • as an aside, using registerNew/Dirty(sobject) rather than registerNew/Dirty(sobjectList) makes it somewhat easier to use apexMocks to test expected "new/updated" record DML. See my blog post on this topic – cropredy May 10 '19 at 18:00
7

I do not understand how you can have two methods with the same name and different params.

This is called method overloading. In languages like C, C++, Java, and Apex, each method has a particular "signature." When compiling the code, the method with the correct "signature" for the provided parameters is selected for execution. This is true even if there are two methods with the same number of parameters, but different types.

Trivial Example

public void printValue(String value) {
  System.debug(value);
}
public void printValue(Integer value) {
  System.debug(value);
}

On top of that, I do not understand where this class holds all the to be inserted records.

They are found in the following variable:

protected Map<String, List<SObject>> m_newListByType = new Map<String, List<SObject>>();

Which is to say that for each type of sObject, there is a list of records to be inserted.

3

This is known as method overloading

  • A feature of many (statically typed) languages, this has been around a very long time. Note that (dynamically typed) languages like JavaScript don't actually support this approach but rather allow you to interrogate the parameters you have received in a far more dynamic manner (which can result in a similar capability, albeit in a less clean way). – Phil W May 10 '19 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.