I don't know the name of this pattern but what is the purpose of such methods:


instead of single-operation ones?

  • 3
    Related: upsert. (Portmanteau of "update" and "insert")
    – D M
    Oct 24, 2023 at 13:57
  • 2
    To add to what Doc Brown said; when this is used, the nature of the update operation is often either to replace specific fields, or perhaps to replace the entire record (depending on the semantics the API specifies), so you can think of the "add" operation as doing an update on a empty record (even though it's created right at that moment). Sometimes people name such operations just Update and state in the docs that the method will create a new record if one doesn't exist. Sometimes you'll find Upsert (as D M pointed out), and sometimes people are more explicit, and use AddOrUpdate. Oct 24, 2023 at 20:29

1 Answer 1


I don't know if there is a common name which counts as a "pattern", but I can tell you the purpose.

With only singular Add and Update methods at hand, in an "Add-Or-Update scenario" the caller will have to test for the existence of a certain record. An AddOrUpdate method allows to shift this responsibility from the caller to the callee.

This is useful in a Client-/Server scenario or Web-API-scenario, where each Add, Update and Exists operation will cause one or more extra network roundtrips. AddOrUpdate will allow to bundle those calls into one. It may also help to avoid the necessity of creating an explicit transaction for the "if record exists update else add" sequence at the calling side.

  • 6
    SE stack exchange is lucky to have you. Seems like you answer a lot of question around here. Thanks for helping everyone out.
    – Treker
    Oct 24, 2023 at 15:56
  • It's also important to note that such methods allow the callee to perform the operation atomically, which is not always possible or desirable with individual operations.
    – Ccm
    Oct 25, 2023 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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