I'm trying to wrap my head around OOP when building simple CRUD systems, using the Repository Pattern to handle object retrieval/saving to persistent storage.
I've already designed and implemented a system that doesn't use OOP, just straight up get, insert, update and delete functions that contain SQL queries.
Non-OOP way
For example, for updating a customer I would call this function:
// PUT customer
function updateCustomer(idCustomer, data) {
db("customer").update(data).where("idCustomer", idCustomer);
}
OOP way
Now, to implement this in OOP using the Repository Pattern I would do something like:
// PUT customer
customer = customerRepo.getById(idCustomer);
customer.setData(data);
customerRepo.update(customer);
OOP way seems to require an extra DB roundtrip
This OOP pattern seems to require DB roundtrips since I need to:
- Instantiate a new customer object from the DB using the repo
- Update the data in the object
- Save the object back to the DB using the repo
Question
Isn't my current, non-OOP way of doing things more efficient? If yes, are there any common solutions to these roundtrips when following the 2nd OOP method?