如何使用 Spring Boot 在整体式应用程序中设计"delete user flow"?



我有一个整体的弹簧引导应用程序,并且我有一个该程序的用户。我想做一个端点来删除用户,但我的问题是有这么多的实体和Jpa存储库接口相关的用户。当我想要删除一个用户时,我需要在相关的删除服务中注入很多存储库接口,比如:

@Service
public class DeletionService {

private static final int S3_DELETION_KEY_LIMIT = 1000;

private final OfficeRepository officeRepository;
private final AdvertRepository advertRepository;
private final UserRepository userRepository;
private final AdvertSearchRepository advertSearchRepository;
private final AdvertPricesRepository advertPricesRepository;
private final AdvertFacilityRepository advertFacilityRepository;
private final AdvertVirtualPackagesRepository advertVirtualPackagesRepository;
private final BookingRepository bookingRepository;
private final AdvertMediasRepository advertMediasRepository;
private final MediaRepository mediaRepository;
private final AmazonS3Service amazonS3Service;
private final OfficeBuildingSecurityRepository officeBuildingSecurityRepository;
private final OfficeCyberSecurityRepository officeCyberSecurityRepository;
private final OfficeFacilityRepository officeFacilityRepository;
private final OfficeMediasRepository officeMediasRepository;
private final OfficeNotificationsRepository officeNotificationsRepository;
private final OfficePropertiesRepository officePropertiesRepository;
private final OfficeRoomsRepository officeRoomsRepository;
private final OfficeViewsRepository officeViewsRepository;
private final OwnerCompanyRepository ownerCompanyRepository;
private final PushNotificationTokenRepository pushNotificationTokenRepository;
... and so many other repositories and 

//CONSTRUCTOR etc.

程序中的所有内容都与用户相关,如果我删除用户,那么所有内容都将失效。我不确定这是正确的风格或流程,如果在monolith应用程序中有任何其他选择,我如何才能用更好的方法做到这一点?是否必须将所有相关的存储库接口注入当前服务?

注意:我没有使用任何队列服务,如Kafka, Sqs, RabbitMq等。

不管你的架构是什么,单体还是微服务——模式都是一样的。

基本概念:将大的删除操作分解为单独的步骤,例如:

public interface DeleteStep<E> {
void delete(E entity);
}
@Component
public class DeleteFiles implements DeleteStep<User> {
@Autowired
AmazonS3Service amazonS3Service
@Override
public void delete(User user) {
amazonS3Service.deleteUserFiles(user);
}
}

@Component
public class DeleteNotifications implements DeleteStep<User> {
@Autowired
OfficeNotificationsRepository officeNotificationsRepository;
@Override
public void delete(DmUser user) {
officeNotificationsRepository.deleteUserNotifications(user);
}
}

现在我们的DeletionService的主体看起来像:

@Autowired
ObjectProvider<DeleteStep<User>> userDeletionSteps;
public void deleteUser(User user) {
userDeletionSteps.forEach(s -> s.delete(user));
}

现在我们可以利用应用程序事件来改进这个基本概念,例如:

public class UserDeleteEvent {
private final User user;
public UserDeleteEvent(User user) {
this.user = user;
}
public User getUser() {
return user;
}

}

interface OfficeNotificationsRepository extends ... {
@EventListener(UserDeleteEvent.class)
default void onUserDeleted(UserDeleteEvent event) {
deleteUserNotifications(event.getUser());
}
}
class AmazonS3Service ... {
@EventListener(UserDeleteEvent.class)
void onUserDeleted(UserDeleteEvent event) {
deteteUserFiles(event.getUser());
}
}

现在我们的DeletionService变成了:

@Autowired
ApplicationEventPublisher eventPublisher;
public void deleteUser(User user) {
eventPublisher.publishEvent(new UserDeleteEvent(user));
}

此外,我们可以利用@DomainEvents而摆脱DeletionService-UserRepository可以调度应用程序事件。

相关内容

  • 没有找到相关文章

最新更新