Some properties on an entity define what it is: its Id, and often the parent it belongs to. Others describe what state it is in, like a name, a status or a power level. When every property uses private set, the two look identical, so nothing stops a method inside the entity from quietly reassigning its own Id.
Use private init for identity properties and private set for state properties. The compiler then lets identity be set only while the object is being created, and the class declaration shows at a glance which properties define the entity.
Id, the TeamId a mission belongs to, the HeroId on a hero's power assignmentName, Status, PowerLevelpublic class Mission : AggregateRoot{public MissionId Id { get; private set; }public TeamId TeamId { get; private set; }public string Description { get; private set; } = null!;public MissionStatus Status { get; private set; }public void Reassign(TeamId newTeamId){// Compiles, but now the mission's history, events and audit trail// point at a team that never had this missionTeamId = newTeamId;}public void Complete(){Status = MissionStatus.Complete;}}
❌ Figure: Bad example - Identity and state both use private set, so a method can quietly change which team a mission belongs to
public class Mission : AggregateRoot{public MissionId Id { get; private init; }public TeamId TeamId { get; private init; }public string Description { get; private set; } = null!;public MissionStatus Status { get; private set; }private Mission() { }public static Mission Create(TeamId teamId, string description){return new Mission{Id = new MissionId(Guid.CreateVersion7()),TeamId = teamId,Description = description,Status = MissionStatus.Planned};}public void Complete(){Status = MissionStatus.Complete;}}
✅ Figure: Good example - Identity uses private init and can only be set in the factory, while state uses private set and changes through behaviour
With private init, writing TeamId = newTeamId; inside a normal method is a compile error. If a mission really has to move to another team, the domain should say so explicitly, for example by cancelling the original mission and creating a new one for the other team.
private set only protects identity from code outside the class. private init also protects it from the class itselfTip: Combine private init with strongly typed IDs, so identity can't be mixed up with another entity's ID or changed after creation. See Do you use Strongly Typed IDs to avoid Primitive Obsession.
Use private init for properties that:
Id or the parent it belongs toUse private set for properties that:
The trade-off is that a class mixes two accessor patterns, and the team needs to understand the difference between init and set. That small cost is worth it for a model where identity can't drift.