Domain Services and Entity Methods in a Domain Model

I know the difference between domain and application services. But it can’t see the difference between the methods in the domain entities and the domain services: /

I have a game that has State , Players , etc. It also has methods such as AddPlayer , MoveLeft , Jump . Where do these methods go? Should I create bare KOGame with only properties, and then KOGameServices with functionality?

Uncle Bob in his article here wrote: "An entity can be an object with methods or it can be a set of data structures and functions."

I don’t even want to mention that methods such as Move or Jump will have to be in App Services, in KOGameAPI - they force these methods to use the interface (via interfaces, of course).

Here is my class:

 public class KOGame { public GameState State { get; set; } public IList<Player> Players { get; set; } public int PlayersCount; public KOGame() { State = GameState.New; PlayersCount = 2; Players = new List<Player>(); } public void AddPlayer(Player player) { } public bool MoveRight(int id) { return false; } public bool MoveLeft(int id) { return false; } public bool Jump(int id) { return false; } } 

So, close my question: what methods apply to domain services and what methods apply to domain objects? Having, for example, the class Class1 , when should I create the class Class1Services ?

EDIT: Just a brief explanation of why I choose DDD : I want to create a cross-platform application, and I want one layer to be common for each platform. I choose C# , called using Xamarin . I can easily implement a single-user model and even services for each platform. I was just stuck in deciding which methods should go to Services and what as part of the objects in the Domain Model

+5
source share
2 answers

If, for using the use case, it is necessary that at the domain level 2 or more aggregates are coordinated, you put out the coordination logic in the domain services that invoke the aggregated methods. If only one aggregate is required, none of the domain services is involved. Just call the aggregation method from the application service.

+6
source

If the method logically belongs to the entity, put it there. If there is no organization in which the method will make sense, put it in a domain service (stateless!).

For example, the player’s moving method must be on the player’s object — it feels natural to be there because it modifies that particular player.

On the other hand, a method, for example, calculating the difference in the score of two players can be implemented as a domain service.

+5
source

Source: https://habr.com/ru/post/1232759/


All Articles