The acronym CRUD, (create, read, update, delete), is common in object oriented programming. Being relatively new to OOP, I am wondering about the context of this functionality. When building a class with CRUD methods, should those methods be creating, reading, updating, deleting object attributes OR should they be performing those actions on database data OR some combination?
Explaining the CRUD:
- create: this part refers to constructors and factory methods that 'create' new data objects for you to use, or add new records to a dabase.
- read: these are the getter methods in your code. Since you should never expose internal variables outside the class, you provide getters so other code can get information about the object's state. This is also reading the fields of various database entries.
- update: these are the setter methods, complimentary to the getters for instances where other code also needs to be able to modify the object or database entry.
- destroy: this refers to the object's destructor, the piece of code that frees up any allocated resources and ensures that the object can be disposed of cleanly, or that removes the record from the database.
The connection between Object Oriented Programming and Databases is the basic idea that database entries are, in a fairly basic sense, objects. Each entry in the database has various fields that correspond directly to fields of an object in an object-oriented language.