After dealing with DDD for months now, I'm still confused about the general purposes of domain services, factories and aggregate roots in relation to each other, e.g. where they overlap in their responsibility.Example: I need to 1) create a complex domain entity in a saga (process manager) which is followed by 2) a certain domain event that needs to be handled elsewhere whereas 3) the entity is clearly an aggregate root that marks a bounded context to some other entities.The factory IS be responsible for creation of the entity/aggregate rootThe service CAN create the entity, since he also throws the domain eventThe service CAN act as a aggregate root (create 'subentity' of 'entity' with ID 4)The aggregate root can create and manage 'subentities'When I introduce the concept of a aggregate root as well as a factory to my domain, a service seems no longer needed. However, if I'm not, the service can handle everything needed as well with the knowledge and dependencies he has.Code Examplebased on the ubiquitous language of a car repair shoppublic class Car : AggregateRoot {    private readonly IWheelRepository _wheels;    private readonly IMessageBus _messageBus;    public void AddWheel(Wheel wheel) {        _wheels.Add(wheel);        _messageBus.Raise(new WheelAddedEvent());    }}public static class CarFactory {    public static Car CreateCar(string model, int amountofWheels);}..or...public class Car {    public ICollection<Wheel> Wheels { get; set; }}public interface ICarService {    Car CreateCar(args);    void DeleteCar(args);    Car AddWheel(int carId, Wheel wheel);}
Domain Services vs. Factories vs. Aggregate Roots
design patterns;domain driven design;separation of concerns;cqrs