一般来说,这个背景故事并不重要,只是为了解释下面的代码:
服务器处理用户和用户组。用户组能够“发现”地点——此时这些地点完全来自 Google Places API。
当前实现
目前,我有很多JpaRepository
在我的服务层中的对象,我称之为存储库。我强调“存储库”是因为在我下面提出的解决方案中,它们将被降级为 DAO。
但是,我在当前代码中不喜欢的地方以及我在这里提出问题的原因是可以在 UserGroupService
中找到的存储库数量。 .
@Service
public class UserGroupService {
private final static Logger LOGGER = LogManager.getLogger(UserGroupService.class);
@Autowired
private UserGroupRepository userGroupRepository;
@Autowired
private UserGroupPlaceRepository userGroupPlaceRepository;
@Autowired
private PlaceRepository placeRepository;
@Autowired
private GooglePlaceRepository googlePlaceRepository;
@Autowired
private GooglePlaces googlePlaces;
public UserGroupService() {
}
@Transactional
public void discoverPlaces(Long groupId) {
final UserGroup userGroup = this.userGroupRepository.findById(groupId).orElse(null);
if (userGroup == null) {
throw new EntityNotFoundException(String.format("User group with id %s not found.", groupId));
}
List<PlacesSearchResult> allPlaces = this.googlePlaces.findPlaces(
userGroup.getLatitude(),
userGroup.getLongitude(),
userGroup.getSearchRadius());
allPlaces.forEach(googlePlaceResult -> {
GooglePlace googlePlace = this.googlePlaceRepository.findByGooglePlaceId(googlePlaceResult.placeId);
if (googlePlace != null) {
return;
}
Place place = new Place();
place.setLatitude(googlePlaceResult.geometry.location.lat);
place.setLongitude(googlePlaceResult.geometry.location.lng);
place.setPlaceType(Place.PlaceType.GOOGLE_PLACE);
place.setName(googlePlaceResult.name);
place.setVicinity(googlePlaceResult.vicinity);
place = this.placeRepository.save(place);
UserGroupPlace.UserGroupPlaceId userGroupPlaceId = new UserGroupPlace.UserGroupPlaceId();
userGroupPlaceId.setUserGroup(userGroup);
userGroupPlaceId.setPlace(place);
UserGroupPlace userGroupPlace = new UserGroupPlace();
userGroupPlace.setUserGroupPlaceId(userGroupPlaceId);
this.userGroupPlaceRepository.save(userGroupPlace);
googlePlace = new GooglePlace();
googlePlace.setPlace(place);
googlePlace.setGooglePlaceId(googlePlaceResult.placeId);
this.googlePlaceRepository.save(googlePlace);
});
}
}
一个不起作用的解决方案
@Inheritance
可以使这段代码变得更简单并有可能解决这个问题。 :@Entity
@Table(name = "place")
@Inheritance(strategy InheritanceType.JOINED)
public class Place { /* .. */ }
@Entity
@Table(name = "google_place")
public class GooglePlace extends Place { /* .. */ }
但是,这不是一个选项,因为这样我就不能拥有
PlaceRepository
这只是节省了一个地方。 Hibernate does not seem to like it. .我的提议
我认为我的困惑始于 Spring 使用的名称。例如。
JpaRepository
- 我不太确定这是否真的是“正确”的名字。因为据我了解,这些对象实际上就像数据访问对象 (DAO) 一样工作。我认为它实际上应该看起来像这样:public interface PlaceDao extends JpaRepository<Place, Long> {
}
public interface GooglePlaceDao extends JpaRepository<Place, Long> {
}
@Repository
public class GooglePlaceRepository {
@Autowired
private PlaceDao placeDao;
@Autowired
private GooglePlaceDao googlePlaceDao;
public List<GooglePlace> findByGroupId(Long groupId) {
// ..
}
public void save(GooglePlace googlePlace) {
// ..
}
public void saveAll(List<GooglePlace> googlePlaces) {
// ..
}
}
@Service
public class UserGroupService {
@Autowired
private GooglePlaceRepository googlePlaceRepository;
@Autowired
private UserGroupRepository userGroupRepository;
@Transactional
public void discoverPlaces(Long groupId) {
final UserGroup userGroup = this.userGroupRepository.findById(groupId).orElse(null)
.orElseThrow(throw new EntityNotFoundException(String.format("User group with id %s not found.", groupId)));
List<PlacesSearchResult> fetched = this.googlePlaces.findPlaces(
userGroup.getLatitude(),
userGroup.getLongitude(),
userGroup.getSearchRadius());
// Either do the mapping here or let GooglePlaces return
// List<GooglePlace> instead of List<PlacesSearchResult>
List<GooglePlace> places = fetched.stream().map(googlePlaceResult -> {
GooglePlace googlePlace = this.googlePlaceRepository.findByGooglePlaceId(googlePlaceResult.placeId);
if (googlePlace != null) {
return googlePlace;
}
Place place = new Place();
place.setLatitude(googlePlaceResult.geometry.location.lat);
place.setLongitude(googlePlaceResult.geometry.location.lng);
place.setPlaceType(Place.PlaceType.GOOGLE_PLACE);
place.setName(googlePlaceResult.name);
place.setVicinity(googlePlaceResult.vicinity);
googlePlace = new GooglePlace();
googlePlace.setPlace(place);
googlePlace.setGooglePlaceId(googlePlaceResult.placeId);
return googlePlace;
}).collect(Collectors.toList());
this.googlePlaceRepository.saveAll(places);
// Add places to group..
}
}
概括
我想知道我没有看到什么。我是在与框架作斗争,还是我的数据模型没有意义,这就是我发现自己为此苦苦挣扎的原因?或者我仍然对如何使用“存储库”和“DAO”这两种模式有疑问?
将如何实现这一点?
最佳答案
我会说你的服务中有太多的存储库依赖项是正确的。就个人而言,我尽量保持 @Autowired
的数量。将依赖项降至最低,我尝试仅在一项服务中使用存储库,并通过该服务公开其更高级别的功能。在我们公司,我们称之为数据主权(德语:Datenhoheit),其目的是确保应用程序中只有一个地方可以修改这些实体。
根据我对您的代码的理解,我将介绍 PlacesService
它具有 PlaceRepository
的所有依赖项, GooglePlaceRepository
和 GooglePlaces
.如果您觉得 Service 不是正确的名称,您也可以将其称为 PlacesDao
,用 Spring 标记它@Component
注释并注入(inject)所有存储库,根据定义,这些存储库是事物的集合
@Component
public class PlacesDao {
@Autowired
private PlaceRepository placeRepository;
@Autowired
private GooglePlaceRepository googlePlaceRepository;
该服务/DAO 可以提供 API
findPlacesForGroup(userGroup)
和 createNewPlace(...)
从而使您的 for 循环更小更优雅。附带说明:您可以将前四行合并为一行。 Java 选项支持
orElseThrow()
方法:UserGroup userGroup = userGroupRepository.findById(groupId).orElseThrow(() ->
new EntityNotFoundException(String.format("User group with id %s not found.", groupId));
关于spring - 存储库与 DAO(再次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49634855/