Skip to main content
added 376 characters in body
Source Link
Ewan
  • 84.6k
  • 5
  • 91
  • 189

Now when it comes to Domain Driven Design, the goal is to have both data and behavior encapsulated in a single entity (the model class)

I would challenge this. If you have your Models as data structs and put the logic in "Domain Services" then all your problems (from this question) will be solved.

The structure will be something like

App
   Construct Repository
   Construct Business Logic Service
   Loop Over things to do
     Fetch Model from Repo
     Call Logic, passing in Model Model
     Save model back to Repo

If you have this, but then want a more object orientated approach, you can just move the code blocks around to get:

App
   Construct DTO->Model and back Mapper
   Construct Repository, passing in Mapper
   Loop Over things to do
     Fetch Model from Repo
     Call Model.Logic
     Save Model back to Repo

You'll find the code is pretty much identical, You've just moved it into different objects.

The problem with this style, happens when you have more than one logical operation. The you have to either put both on the same Model, or start having Model versions for each Bounded Context. Rather than just having a Logic Service class per operation.

Problem 1 and 3 are solved by having the DTO mapper hidden inside the repository. This separates the Model from the ORM, although this should be possible to do with the correct setup in most ORMS without an extra mapper.

Models should have all the data for the aggregate in them, so you can perform operations purely in memory without recourse to the database. So you should never have problem 4

This should also help you with problem 2. As you have all the data in the Model, there should be no need to inject a repo into your Model or Logic

Now when it comes to Domain Driven Design, the goal is to have both data and behavior encapsulated in a single entity (the model class)

I would challenge this. If you have your Models as data structs and put the logic in "Domain Services" then all your problems (from this question) will be solved.

The structure will be something like

App
   Construct Repository
   Construct Business Logic Service
   Loop Over things to do
     Fetch Model from Repo
     Call Logic, passing in Model Model
     Save model back to Repo

If you have this, but then want a more object orientated approach, you can just move the code blocks around to get:

App
   Construct DTO->Model and back Mapper
   Construct Repository, passing in Mapper
   Loop Over things to do
     Fetch Model from Repo
     Call Model.Logic
     Save Model back to Repo

You'll find the code is pretty much identical, You've just moved it into different objects.

The problem happens when you have more than one logical operation. The you have to either put both on the same Model, or start having Model versions for each Bounded Context.

Models should have all the data for the aggregate in them, so you can perform operations purely in memory without recourse to the database. So you should never have problem 4

Now when it comes to Domain Driven Design, the goal is to have both data and behavior encapsulated in a single entity (the model class)

I would challenge this. If you have your Models as data structs and put the logic in "Domain Services" then all your problems (from this question) will be solved.

The structure will be something like

App
   Construct Repository
   Construct Business Logic Service
   Loop Over things to do
     Fetch Model from Repo
     Call Logic, passing in Model Model
     Save model back to Repo

If you have this, but then want a more object orientated approach, you can just move the code blocks around to get:

App
   Construct DTO->Model and back Mapper
   Construct Repository, passing in Mapper
   Loop Over things to do
     Fetch Model from Repo
     Call Model.Logic
     Save Model back to Repo

You'll find the code is pretty much identical, You've just moved it into different objects.

The problem with this style, happens when you have more than one logical operation. The you have to either put both on the same Model, or start having Model versions for each Bounded Context. Rather than just having a Logic Service class per operation.

Problem 1 and 3 are solved by having the DTO mapper hidden inside the repository. This separates the Model from the ORM, although this should be possible to do with the correct setup in most ORMS without an extra mapper.

Models should have all the data for the aggregate in them, so you can perform operations purely in memory without recourse to the database. So you should never have problem 4

This should also help you with problem 2. As you have all the data in the Model, there should be no need to inject a repo into your Model or Logic

Source Link
Ewan
  • 84.6k
  • 5
  • 91
  • 189

Now when it comes to Domain Driven Design, the goal is to have both data and behavior encapsulated in a single entity (the model class)

I would challenge this. If you have your Models as data structs and put the logic in "Domain Services" then all your problems (from this question) will be solved.

The structure will be something like

App
   Construct Repository
   Construct Business Logic Service
   Loop Over things to do
     Fetch Model from Repo
     Call Logic, passing in Model Model
     Save model back to Repo

If you have this, but then want a more object orientated approach, you can just move the code blocks around to get:

App
   Construct DTO->Model and back Mapper
   Construct Repository, passing in Mapper
   Loop Over things to do
     Fetch Model from Repo
     Call Model.Logic
     Save Model back to Repo

You'll find the code is pretty much identical, You've just moved it into different objects.

The problem happens when you have more than one logical operation. The you have to either put both on the same Model, or start having Model versions for each Bounded Context.

Models should have all the data for the aggregate in them, so you can perform operations purely in memory without recourse to the database. So you should never have problem 4