Beispiel #1
0
        public void Instance_WhenSerializedWithBinaryFormatterNullField_ShouldBeSerializable()
        {
            var instance     = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, null);
            var deserialized = SerializationTesting.SerializeDeserializeWithBinaryFormatter(instance);

            Assert.That(deserialized, Is.EqualTo(instance));
        }
Beispiel #2
0
        public void Instance_WhenSerializedWithDataContractFormatter_ShouldBeSerializable()
        {
            var instance     = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, SourceId);
            var deserialized = SerializationTesting.SerializeDeserializeWithDataContractSerializer(instance);

            Assert.That(deserialized, Is.EqualTo(instance));
        }
Beispiel #3
0
        public void EqualsGetHashCodeAndEqualityOperators()
        {
            var a = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, SourceId);
            var b = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, SourceId);
            var c = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, SourceId);

            var otherCommandId      = new ImportEmployee(Guid.NewGuid(), EmployeeNumber + 1, FirstName, Lastname, SourceId);
            var otherEmployeeNumber = new ImportEmployee(CommandId, EmployeeNumber + 1, FirstName, Lastname, SourceId);
            var otherFirstName      = new ImportEmployee(CommandId, EmployeeNumber, FirstName + "x", Lastname, SourceId);
            var otherFirstNameNull  = new ImportEmployee(CommandId, EmployeeNumber, null, Lastname, SourceId);
            var otherLastName       = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname + "x", SourceId);
            var otherLastNameNull   = new ImportEmployee(CommandId, EmployeeNumber, FirstName, null, SourceId);
            var otherSourceId       = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, SourceId + 1);
            var otherSourceIdNull   = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, null);

            EqualityTesting.TestEqualsAndGetHashCode(a, b, c,
                                                     otherCommandId,
                                                     otherEmployeeNumber, otherFirstName, otherFirstNameNull,
                                                     otherLastName, otherLastNameNull,
                                                     otherSourceId, otherSourceIdNull);

            EqualityTesting.TestEqualityOperators(a, b, c,
                                                  otherEmployeeNumber, otherFirstName, otherFirstNameNull,
                                                  otherLastName, otherLastNameNull,
                                                  otherSourceId, otherSourceIdNull);
        }
Beispiel #4
0
        public void Constructor_WithAllValues_ShouldSetProperties()
        {
            var actual = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, SourceId);

            Assert.That(actual.CommandId, Is.EqualTo(CommandId));
            Assert.That(actual.EmployeeNumber, Is.EqualTo(EmployeeNumber));
            Assert.That(actual.FirstName, Is.EqualTo(FirstName));
            Assert.That(actual.LastName, Is.EqualTo(Lastname));
            Assert.That(actual.SourceId, Is.EqualTo(SourceId));
        }
Beispiel #5
0
        public async Task <(CommandHandleStatus status, IReadOnlyEmployee employee)> Handle(
            ImportEmployee command, CancellationToken ct = default)
        {
            //Simple but effective retry policy. Having an http endpoint exposes us to concurrency issues.
            //Or is there a glitch in the network?
            var retryPolicy = Policy
                              .Handle <DbUpdateException>()
                              .RetryAsync(CommandHandleMaxRetries,
                                          (exception, i) =>
            {
                _logger.LogDebug(exception,
                                 $"Failed to import employee due to concurrency. Retry {i} of {CommandHandleMaxRetries}");
            });

            var policyResult =
                await retryPolicy.ExecuteAndCaptureAsync(() => ImportEmployee(command, ct));

            return(HandleResult(policyResult));
        }
Beispiel #6
0
        public void Instance_WhenConventionsDeclareABaseInterface_MustImplementTheInterface()
        {
            var instance = new ImportEmployee(CommandId, EmployeeNumber, FirstName, Lastname, null);

            Assert.That(instance, Is.AssignableTo <ICommand>());
        }
Beispiel #7
0
        private async Task <(Employee NewEmployee, DomainError DomainError)> ImportEmployee(ImportEmployee command,
                                                                                            CancellationToken cancellationToken)
        {
            //We can predetermine our employee ID based on FistName + LastName + Age + Height.
            var employeeId = EmployeeIdGenerator.Create(Encoding.UTF8.GetBytes(command.ToUniqueIdentifier)).ToString();

            var employee = await _employeesRepository.GetById(employeeId, cancellationToken);

            if (employee != null)
            {
                return(employee, DomainError.None);
            }


            if (command.Age < 18) //lets assume minimum working age is 18
            {
                //hey we are doing something doggy here
                return(null, new DomainError("Child labor long time ago abolished. There must be a mistake somewhere"));
            }
            //we wont check for now the max life limit

            //This gets interesting since we don't have the actual date of birth. but our company needs it to send a birthday postcard every year
            employee =
                Employee.CreateNew(employeeId, _getUtcNow())
                //should be good approximation. Anyway we will mark that the real birthday is not supplied and later on in UI someone will notice
                .WithEstimatedBirthDate(_getUtcNow().AddYears(-command.Age))
                .WithFirstName(command.FirstName)
                .WithLastName(command.LastName)
                .WithHeightInMeters(command.HeightInMeters);

            await _employeesRepository.AddNew(employee);

            await _employeesRepository.CommitChanges(cancellationToken);

            _logger.LogDebug("Employee {displayName} imported", $"{command.FirstName} {command.LastName}");
            return(employee, DomainError.None);
        }