Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

CRUD has nothing to do with object oriented programming. It's usually all about database operations. It is sometimes used for HTTP requests too.

From wikipedia

Operation           SQL     HTTP
Create              INSERT  PUT / POST
Read (Retrieve)     SELECT  GET
Update (Modify)     UPDATE  PUT / PATCH
Delete (Destroy)    DELETE  DELETE

You hear about it in OOP contexts because these people you're spending time with use OOP languages like Java.

As evidence, you can find a CRUD implementation in Haskell here

You shouldn't be building classes with CRUD methods.

You should be building a class with methods that represent the behavior of the object represented in the context of the domain your modelling. Building a class with generic methods like CRUD would be a OOP design smell.

For example, this is wrong (so so so wrong)

class Person(object):
    def __init__(name, age, location)
        self.name = name
        self.age = age
        self.location = location

    def get_name(self):
        return self.name

    def update_name(self, new_name):
        self.name = new_name

    def delete_name(self):
        self.name = None

These methods have nothing to do with the behavior of a Person. It turns the Person into just a dumb data object, which is the opposite of what Object Orientated design is attempting to do.

If your person object has to talk to a database to persist itself this functionality should be hidden from the public interface. And a better option is to not tie your object to a database in the first place, use Plain Old Objects in your domain and write to a database using some other method.

Most of the time, I would use crud on the data that is stored in your database. I will personally write couple of stored procedures to execute those functionalities.

CRUD is a basic actions for declared variables as well. For example in C#, you can declare a variable List<Of T>, and then perform CRUD actions on to it before uploading it into the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top