public void Register(EndUserId endUserId, string?name, RobotApplication?application) { void AddRegistration() { var registrationId = new RobotRegistrationId(Guid.NewGuid()); var e = RobotRegistered.Create(registrationId, endUserId, name, application); Apply(e); Append(registrationId.Value, RobotRegistered.EventType, e); } var latestRegistration = Registrations.LastOrDefault(); if (latestRegistration == null) { AddRegistration(); } else { latestRegistration.Apply( registration => throw new ValidationException("Robot is already registered"), unregistration => AddRegistration() ); } }
public Registration(Robot robot, RobotRegistered e) { Id = new RobotRegistrationId(Guid.NewGuid()); EndUserId = e.EndUserId; Name = e.Name ?? $"{robot.Product:G} - {robot.SerialNumber}"; Application = e.Application; }
private void Apply(RobotRegistered e) { void AddRegistration() { var registration = new RobotRegistration.Registration(this, e); Registrations = Registrations.Add(registration); } var latestRegistration = Registrations.LastOrDefault(); if (latestRegistration == null) { AddRegistration(); } else { latestRegistration.Apply( registration => throw new InvalidOperationException("Robot is already registered"), unregistration => AddRegistration() ); } }