Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some good design patterns for CRUD? [closed]

I am working with a number of data entities which can be created, read, updated and deleted, and I find myself writing more or less the same code for them. For example, I need to sometimes output data as JSON, and sometimes in a table format. I am finding myself writing 2 different types of view to export the data to. Also, the creation of those entities within DB usually differs just by the SQL statements and the input parameters.

I am thinking of creating a strategy pattern to represent different 'contexts'. For example, the read() method of an AJAX context will be to return the data as JSON. However, I wonder if others have deal with this problem beforehand and will like to know what design patterns are usually use for CRUD operations.

Edit: One note is that sometimes compound entities are used to make up one whole one; for example, a location can have many descriptions, one for each supported language.

like image 635
Extrakun Avatar asked Apr 03 '10 07:04

Extrakun


1 Answers

There isn't one single pattern for CRUD, there are many overlapping/competing patterns. One of the best sources for these CRUD patterns is:

Martin Fowler's Patterns of Enterprise Application Architecture

In general, the best way to solve this particular problem is with an ORM (assuming a relational data store, which seems like a safe assumption in this case):

http://en.wikipedia.org/wiki/Object-relational_mapping

As far as exposing your objects through views, there are frameworks out there to help with that as well, but those frameworks are specific to your coding environment, which you did not specify.

like image 176
Michael Maddox Avatar answered Sep 29 '22 13:09

Michael Maddox