I know that domain logic should be placed into domain objects. But what if my domain logic needs data from the database ? (e.g. checking unique value, computed values.. etc ) I think injecting repositories into my domain objects isn't the right thing. Also the service layer should not contain business rules. So how to solve this kind of business logic ?
Domain logic (aka business logic, business rules, and domain knowledge) is the logic that makes business-critical decisions. All other types of logic orchestrate the decisions made by the domain model and transform them into side-effects: save them to the data store, show to the user, or pass to 3rd-party services.
Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.
The repository is implemented in the domain layer, because it works with domain objects. But in the domain layer we should have no idea about any database nor any storage, so the repository is just an interface.
A procedure, also called a stored procedure, is a database object created via the CREATE PROCEDURE statement that can encapsulate logic and SQL statements.
You are right your domain object should not read data directly from the database. The classic error here is that the domain object is sent over a web service and tries to read data from the database, when it is on a server without access to the database.
There are several ways to do this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With