Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put domain logic which needs to fetch data from database

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 ?

like image 733
user256034 Avatar asked May 15 '11 17:05

user256034


People also ask

What is the use of domain 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.

What is repository in domain driven design?

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.

Are repositories part of the domain?

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.

Is a database object that can encapsulate SQL statements and business logic?

A procedure, also called a stored procedure, is a database object created via the CREATE PROCEDURE statement that can encapsulate logic and SQL statements.


1 Answers

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:

  • a service layer preloads any information that the domain object will need
  • the domain object can call a helper or repository that gets the data from the database
like image 152
Shiraz Bhaiji Avatar answered Sep 29 '22 02:09

Shiraz Bhaiji