コード例 #1
0
ファイル: Robot.cs プロジェクト: Nillerr/EventSourcing.Demo
        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()
                    );
            }
        }
コード例 #2
0
 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;
 }
コード例 #3
0
ファイル: Robot.cs プロジェクト: Nillerr/EventSourcing.Demo
        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()
                    );
            }
        }