What are some good design patterns for CRUD?

I work with several data objects that can be created, read, updated and deleted, and I find that I am writing more or less the same code for them. For example, I sometimes have to output data as JSON, and sometimes in the form of a table. I find that I am writing 2 different types of views for exporting data. In addition, the creation of these objects in the database usually differs only in SQL statements and input parameters.

I am thinking of creating a strategy template to represent different "contexts". For example, the read () method for an AJAX context will return data as JSON. However, I wonder if others have problems with this problem beforehand and would like to know which design patterns are commonly used for CRUD operations.

Edit: one note is that sometimes composite objects are used to create one whole; for example, a location can have many descriptions, one for each supported language.

+4
source share
3 answers

There is more than one template for CRUD, there are many matching / competing templates. One of the best sources for these CRUD templates:

Martin Fowler Enterprise Application Architecture Templates

In general, the best way to solve this particular problem is to use ORM (assuming a relational data warehouse, which in this case seems safe):

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

Regarding the disclosure of your objects with representations, you also need frameworks for this, but this framework is specific to your coding environment that you did not specify.

+2
source

Not quite a template answer - sorry. But I would suggest using the ORM system (repository template) for the database interface. JPA , Hibernate , iBatis , etc. You no longer need to write SQL.

If you want to minimize the amount of code you have to write, try a framework like Grails . (Write a complete CRUD website with approximately 30 lines of code.) You no longer need to write any boilerplate code.

+1
source

Check ODATA ( http://www.odata.org ) - this one has pretty decent settings for CRUD over HTTP - including a mechanism for releasing batch updates for the server.

0
source

Source: https://habr.com/ru/post/1305917/


All Articles