在完全投入领域事件之前,我们先完善Management领域限界上下文:

    1. package com.lugew.domaindrivendesignwithspringboot.management;
    2. import com.lugew.domaindrivendesignwithspringboot.sharedkernel.Money;
    3. import lombok.Getter;
    4. import lombok.Setter;
    5. import javax.persistence.*;
    6. @Entity
    7. @Getter
    8. @Setter
    9. public class HeadOfficeDto {
    10. @Id
    11. @GeneratedValue
    12. private long id;
    13. private float balance;
    14. private int oneCentCount;
    15. private int tenCentCount;
    16. private int quarterCount;
    17. private int oneDollarCount;
    18. private int fiveDollarCount;
    19. private int twentyDollarCount;
    20. @Transient
    21. private float amount;
    22. @PostLoad
    23. public void setAmount() {
    24. amount = oneCentCount * 0.01f + tenCentCount * 0.10f + quarterCount * 0.25f
    25. + oneDollarCount * 1f
    26. + fiveDollarCount * 5f + twentyDollarCount * 20f;
    27. }
    28. public HeadOffice convertToHeadOffice() {
    29. HeadOffice headOffice = new HeadOffice();
    30. headOffice.setId(id);
    31. headOffice.setBalance(balance);
    32. headOffice.setCash(new Money(oneCentCount, tenCentCount, quarterCount,
    33. oneDollarCount, fiveDollarCount, twentyDollarCount));
    34. return headOffice;
    35. }
    36. }

    重启应用,在浏览器查看http://localhost:23333/h2-console
    image.png
    接下来为HeadOffice添加变更手续费方法:

    1. package com.lugew.domaindrivendesignwithspringboot.management;
    2. import com.lugew.domaindrivendesignwithspringboot.common.AggregateRoot;
    3. import com.lugew.domaindrivendesignwithspringboot.sharedkernel.Money;
    4. import lombok.Getter;
    5. import lombok.Setter;
    6. import static com.lugew.domaindrivendesignwithspringboot.sharedkernel.Money.None;
    7. @Getter
    8. @Setter
    9. public class HeadOffice extends AggregateRoot {
    10. private float balance;
    11. private Money cash = None;
    12. public void changeBalance(float delta) {
    13. balance += delta;
    14. }
    15. public HeadOfficeDto convertToHeadOfficeDto() {
    16. HeadOfficeDto headOfficeDto = new HeadOfficeDto();
    17. headOfficeDto.setId(id);
    18. headOfficeDto.setBalance(balance);
    19. headOfficeDto.setOneCentCount(cash.getOneCentCount());
    20. headOfficeDto.setTenCentCount(cash.getTenCentCount());
    21. headOfficeDto.setQuarterCount(cash.getQuarterCount());
    22. headOfficeDto.setOneDollarCount(cash.getOneDollarCount());
    23. headOfficeDto.setFiveDollarCount(cash.getFiveDollarCount());
    24. headOfficeDto.setTwentyDollarCount(cash.getTwentyDollarCount());
    25. return headOfficeDto;
    26. }
    27. }

    再添加资源库:

    1. package com.lugew.domaindrivendesignwithspringboot.management;
    2. import org.springframework.data.repository.CrudRepository;
    3. public interface HeadOfficeRepository extends CrudRepository<HeadOfficeDto, Long> {
    4. }

    最后根据需求创建一个HeadOffice,我们直接提供获取实体的单例:

    1. package com.lugew.domaindrivendesignwithspringboot.management;
    2. import lombok.RequiredArgsConstructor;
    3. import org.springframework.stereotype.Component;
    4. @Component
    5. @RequiredArgsConstructor
    6. public class HeadOfficeInstance {
    7. private final HeadOfficeRepository headOfficeRepository;
    8. private static final long HeadOfficeId = 1;
    9. private HeadOfficeDto headOfficeDto;
    10. public HeadOfficeDto getInstance() {
    11. return headOfficeRepository.findById(HeadOfficeId).orElse(null);
    12. }
    13. }

    还有一个问题,这个类属于洋葱架构的哪个位置?它看起来和资源库相似,所以属于资源库相同的层级,此层中的类是不能被内层应用的,只能被相同或上层引用。这个类有别与资源库,因为它维持了一个实例对象,所以不能由资源库来行使。
    准备工作已完成,我们将ATM和HeadOffice两者联系起来。