This article is reprinted from How to Decouple:
https://codedecoupled.com/php…

Add and delete

If you’re a PHP backend developer, add, delete, update (CRUD) is no stranger to you. In simple terms, it means adding, deleting, modifying, and looking up basic operations to the database. Adding, deleting, modifying and searching is a modeling method for database. PHP was made for adding, deleting, and searching. It has a simple syntax, no need to compile, and ORM is everywhere. You can quickly connect database tables to web pages to add, delete, and searching. The pleasure gives the illusion that this is the core of a back-end developer’s job.

Let’s design a simple shopping cart using the idea of add, delete, change and search:

Database design

The first step of adding, deleting, changing and checking must be database design. We’ve kept the design simple and left out a lot of details for demonstration purposes, so don’t use this design in your product. We use only one table, CARTS, to store the shopping cart items.

demo

When product p-1 (ID:1) is added to the cart, add an item to the carts table:

When item p-2 (ID:2) is added to the cart, add another item to the carts table:

When product p-1 (ID:1) is added to the cart again, update the corresponding data in the carts table:

When product P-2 is removed from the cart, delete the corresponding data from the carts table:

The contents of the shopping cart are recorded in the database in real time, which is the main feature of adding, deleting, changing and checking.

Let’s look at a very different kind of thinking.

Event source

Event Sourcing is one of the architectural patterns in Domain Driven Design. Domain Driven Design is a business-oriented approach to modeling. It helps us put the focus back on the business itself.

The core of event traceability is the event, and the state source of all aggregations (a special class) comes from the event, so it is called event traceability. These events are called domain events, but rather than what we normally call events, domain events are what happens in business processes that are of concern to domain experts, and that are relevant to the business.

Let’s design the shopping cart above with the idea of event traceability.

Use domain events

In the shopping cart business, assuming we are a user, we are most interested in adding items to the shopping cart, removing items from the cart, and checking out the cart. So we can summarize several domain events. It is important to note that domain events should be in the past tense.

When product p-1 (ID:1) is added to the shopping cart, add event data to the events table:

When product p-2 (ID:2) is added to the shopping cart, add event data to the events table:

When the product P-1 is added to the cart again, add an event data to the Events table:

When product P-2 is removed from the cart, add an event data to the events table:

The contents of the shopping cart are recorded in the form of events, which can only be appended, not deleted or altered.

Reading Shopping Cart Items

When you need to display the contents of the shopping cart, you can replay the events stored in the Events table:

Embrace the source of the event

Event sourcing requires developers to change their traditional thinking of database design as the source and analyze problems from the business perspective. Not only does the system come with logs, but it can be easily troubleshooted based on domain events, and the event-driven aggregation model makes it very easy to write unit tests.

Adding, deleting, revising and searching are not evil

It should be added that adding, deleting, revising and checking is not without its merits. On the contrary, it is very powerful where it is appropriate. In some areas, adding, deleting, revising and inspecting is the business of the day. Such as a simple CMS, such as the storage of data in an IoT project.

conclusion

This article reprinted from decoupling 】 【 how: https://codedecoupled.com/php-to-ddd.html, if you also for TDD, DDD and concise code interested, welcome to decoupling 】 【 why pay attention to the public, and explored the way of software development.