這是我將通常具有主張域和持久對象的分離。
從持久性的角度來看,您的Person
實體並不在乎這個禁止列表。從持久性的角度來看,它絕對沒有具體目的,因此我認爲它不屬於Person
實體中的任何地方。
從您的域名的角度來看,您基本上正在努力滿足爲您的域名提供方法的要求,要求Person
,「嘿,你被禁止了嗎?」
@Service
public class PersonBanService {
@Cacheable
@Transactional(readOnly = true)
public List<Integer> getBanList() {
return personBanRepository.findAll();
}
}
@Service
public class PersonDomainService {
@Transactional(readOnly = true)
public List<Person> getPersons() {
List<Integer> banList = personBanService.getBanList();
List<PersonEntity> people = personRepository.findAll();
return people.stream()
.map(p -> new Person(p, banList.contains(p.getId())))
.collect(Collectors.toList());
}
}
基本上,PersonDomainService
充當這兩個關注的介體。它會根據域方法調用爲您提取一次禁止列表,然後將其與PersonEntity
實例一起傳遞給域包裝類Person
,該類包裝內部使用該列表。
這裏的好處在於,PersonBanService
可能會選擇緩存返回的結果,從而允許在羣集或多個請求調用中共享該列表,而不必使用任何可用的緩存框架敲擊數據庫。
現在你Person
域對象
public Person(PersonEntity entity, boolean banned) {
this.person = entity;
this.banned = banned;
}
public boolean isBanned() {
return banned;
}
裏面它是安全的假設,這個緩存列表是什麼,你的應用程序保存在一個靜態的註冊表全部到應用程序或者是一些東西,也許是網絡攔截負荷一次並且您重用該特定請求的緩存列表? – Naros
@Naros現在List被存儲在配置數據庫表中。 – slash3584