Search code examples
phpnode.jsdatabaseormdomain-driven-design

Retrive data in database with complex computation using DDD pattern


I am experimenting building software using DDD pattern. I have no problem with creating and updating data. When the user fill-up the forms, it will automatically handle by my domain if there is a business logic involve with it.

My problem is how can I retrieve the data if there is some complex logic involve upon retrieving it? For instance, using DDD in order software with discounts,should I store the total data after my domain computation? or compute the discount when retrieving it the database?

Thanks for your explanation. (-:


Solution

  • In DDD your database is an external concern (part of the infrastructure) and plays no role in your model. In fact, the database is usually abstracted out to an interface for your Domain to use in it's computations. You use the DB to simply store and hydrate models.

    To answer your question: it depends on your UL. For example: you might store the discounted value in the database if you needed to "save" an order and retrieve it in it's exact state at a later time. Or maybe your Domain and UL requires users to transfer totals. For example: a balance transfer Application that allows the Application to shave a percentage off the amount transferred (it may be called a service fee). In this case, the total after the service fee is subtracted is most likely going to be saved.

    Most of the time though, it's better to compute your discount in the domain because your discount is a Domain concept.