public void Configure(EntityTypeBuilder <Author> builder)
        {
            builder.ToTable("Author").HasKey(k => k.Id);

            builder.OwnsOne(p => p.Name, p =>
            {
                p.Property(pp => pp.First).HasColumnName("FirstName").IsRequired().HasMaxLength(50);
                p.Property(pp => pp.Last).HasColumnName("LastName").IsRequired().HasMaxLength(50);
            });

            builder.Property(p => p.DateOfBirth)
            .IsRequired()
            .HasConversion(p => p.Value, p => BirthDate.Create(p).Value);

            builder.Property(p => p.DateOfDeath)
            .HasConversion(p => p.Value, p => DeathDate.Create(p).Value);

            builder.Property(p => p.MainCategory)
            .IsRequired()
            .HasMaxLength(50)
            .HasConversion(p => p.Value, p => MainCategory.Create(p).Value);

            builder.HasMany(p => p.Books).WithOne(p => p.Author)
            .OnDelete(DeleteBehavior.Cascade)
            .Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
        }
 public void Customize(IFixture fixture)
 {
     fixture.Customizations.Add(new CurrentDateTimeGenerator());
     fixture.Register(() => Name.Create(fixture.Create <string>(), fixture.Create <string>()).Value);
     fixture.Register(() => BirthDate.Create(fixture.Create <DateTimeOffset>().AddDays(-fixture.Create <int>())).Value);
     fixture.Register(() => DeathDate.Create(fixture.Create <DateTimeOffset>().AddDays(-fixture.Create <int>())).Value);
     fixture.Register(() => MainCategory.Create(fixture.Create <string>()).Value);
 }
示例#3
0
        public void Create_DeathDate_Should_Fail_For_NullDate()
        {
            //Arrange
            DateTimeOffset?deathDate = null;

            //Act
            var result = DeathDate.Create(deathDate);

            //Assert
            result.IsFailure.Should().BeTrue();
            result.Error.Should().Be("Death Date should not be null.");
        }
示例#4
0
        public void Create_DeathDate_Should_Create_For_PastDate()
        {
            //Arrange
            var fixture = new Fixture();

            fixture.Customizations.Add(new CurrentDateTimeGenerator());
            var deathDate = fixture.Create <DateTimeOffset>().AddDays(-fixture.Create <int>());

            //Act
            var result = DeathDate.Create(deathDate);

            //Assert
            result.IsSuccess.Should().BeTrue();
            result.Value.Value.Should().BeSameDateAs(deathDate);
        }
示例#5
0
        public void Create_DeathDate_Should_Fail_For_FutureDate()
        {
            //Arrange
            var fixture = new Fixture();

            fixture.Customizations.Add(new CurrentDateTimeGenerator());
            var deathDate = fixture.Create <DateTimeOffset>().AddDays(fixture.Create <int>());

            //Act
            var result = DeathDate.Create(deathDate);

            //Assert
            result.IsFailure.Should().BeTrue();
            result.Error.Should().Be("Death Date should not be future date.");
        }
            public async Task <Result <Author> > Handle(CreateAuthorWithDeathDateCommand command)
            {
                var nameResult         = Name.Create(command.FirstName, command.LastName);
                var birthDateResult    = BirthDate.Create(command.DateOfBirth);
                var deathDateResult    = DeathDate.Create(command.DateOfDeath);
                var mainCategoryResult = Entities.Authors.MainCategory.Create(command.MainCategory);

                var authorResult = Result.Combine(nameResult, birthDateResult, deathDateResult, mainCategoryResult)
                                   .Ensure(() => birthDateResult.Value.Value.Date < deathDateResult.Value.Value.Value.Date, "Death date should not be less than birth date.")
                                   .Map(() => new Author(nameResult.Value, birthDateResult.Value, deathDateResult.Value, mainCategoryResult.Value));

                if (authorResult.IsFailure)
                {
                    return(Result.Failure <Author>(authorResult.Error));
                }

                await _unitOfWork.AuthorRepository.AddAsync(authorResult.Value).ConfigureAwait(false);

                await _unitOfWork.SaveChangesAsync().ConfigureAwait(false);

                return(Result.Success(authorResult.Value));
            }
示例#7
0
            public async Task <Result <Author> > Handle(CreateAuthorWithDeathDateCommand command)
            {
                Result <Name> nameResult = Name.Create(command.FirstName, command.LastName);

                if (nameResult.IsFailure)
                {
                    return(Result.Failure <Author>(nameResult.Error));
                }

                Result <BirthDate> birthDateResult = BirthDate.Create(command.DateOfBirth);

                if (birthDateResult.IsFailure)
                {
                    return(Result.Failure <Author>(birthDateResult.Error));
                }

                Result <DeathDate> deathDateResult = DeathDate.Create(command.DateOfDeath);

                if (deathDateResult.IsFailure)
                {
                    return(Result.Failure <Author>(deathDateResult.Error));
                }

                Result <MainCategory> mainCategoryResult = Entities.Authors.MainCategory.Create(command.MainCategory);

                if (mainCategoryResult.IsFailure)
                {
                    return(Result.Failure <Author>(mainCategoryResult.Error));
                }

                var author = new Author(nameResult.Value, birthDateResult.Value, deathDateResult.Value, mainCategoryResult.Value);

                await _unitOfWork.AuthorRepository.AddAsync(author);

                await _unitOfWork.SaveChangesAsync();

                return(Result.Success(author));
            }