15 Add and remove custom methods

 

This chapter covers

  • How to manage many-to-many relationships implicitly
  • The benefits and drawbacks of implicit rather than explicit association
  • How to use custom methods to associate resources together
  • Handling issues with data integrity of associated resources

In this chapter, we’ll explore an alternative pattern for modeling many-to-many relationships that relies on custom add and remove methods to associate (and disassociate) two resources. This pattern allows consumers to manage many-to-many relationships without introducing a third association resource as a necessary requirement.

15.1 Motivation

As we learned in chapter 14, sometimes we have a need to keep track of the relationships between resources, and sometimes those relationships can be complicated. In particular, we often have to handle situations where two resources can both have many of one another, known as a many-to-many relationship.

While the association resource pattern offers a very flexible and useful way to model this type of relationship, it seems worth asking whether there could be a simpler way to do this if we can live with some limitations. In other words, given some restrictions, can we make the API simpler and more intuitive? And if so, what are the specific limitations? This pattern explores a simpler alternative to the association resource pattern.

15.2 Overview

15.3 Implementation

15.3.1 Listing associated resources

15.3.2 Data integrity

15.3.3 Final API definition

15.4 Trade-offs

15.4.1 Nonreciprocal relationship

15.4.2 Relationship metadata

15.5 Exercises

Summary