英文:
What's the difference between using a Service compared to using a repository? Spring Boot
问题
以下是翻译好的部分:
示例 1:
使用 Entity
、Repository
和 Service
创建一个 CRUD 数据库。
https://github.com/alejandro-du/crudui/tree/master/demo/src/main/java/org/vaadin/crudui/demo
代码的使用可以在这里看到:
https://github.com/alejandro-du/crudui/blob/master/demo/src/main/java/org/vaadin/crudui/demo/ui/view/SimpleCrudView.java
在这里我们可以看到 UserService userService
和 GroupService groupService
被用来将值存储在 CRUD 数据库中。
示例 2:
以下是我自己的一个示例,其中我使用了 @Autowired Repository
。
这里:https://github.com/DanielMartensson/OpenSourceLogger/blob/master/src/main/java/se/danielmartensson/views/MySQLView.java
问题:
使用 Service 对象
和使用 @Autowired Repository
之间有什么区别?
示例:
如果我在这里使用 @Autowired Repository backend
,我可以在这里向 CRUD 数据库写入。
crud.setCrudListener(new CrudListener<User>() {
@Override
public Collection<User> findAll() {
return backend.findAllUsers();
}
@Override
public User add(User user) {
return backend.add(user);
}
@Override
public User update(User user) {
return backend.update(user);
}
@Override
public void delete(User user) {
backend.remove(user);
}
});
如果我像下面这样使用 Service 对象,我也可以向 CRUD 数据库写入。
@Route(value = "simple", layout = MainLayout.class)
public class SimpleCrudView extends VerticalLayout {
public SimpleCrudView(UserService userService, GroupService groupService) {
// CRUD 实例
GridCrud<User> crud = new GridCrud<>(User.class);
// 表格配置
crud.getGrid().setColumns("name", "birthDate", "maritalStatus", "email", "phoneNumber", "active");
crud.getGrid().setColumnReorderingAllowed(true);
// 表单配置
crud.getCrudFormFactory().setUseBeanValidation(true);
crud.getCrudFormFactory().setVisibleProperties(
"name", "birthDate", "email", "salary", "phoneNumber", "maritalStatus", "groups", "active", "mainGroup");
crud.getCrudFormFactory().setVisibleProperties(
CrudOperation.ADD,
"name", "birthDate", "email", "salary", "phoneNumber", "maritalStatus", "groups", "active", "mainGroup", "password");
crud.getCrudFormFactory().setFieldProvider("mainGroup", new ComboBoxProvider<>(groupService.findAll()));
crud.getCrudFormFactory().setFieldProvider("groups", new CheckBoxGroupProvider<>(groupService.findAll()));
crud.getCrudFormFactory().setFieldProvider("groups", new CheckBoxGroupProvider<>("Groups", groupService.findAll(), Group::getName));
crud.getCrudFormFactory().setFieldProvider("mainGroup", new ComboBoxProvider<>("Main Group", groupService.findAll(), new TextRenderer<>(Group::getName), Group::getName));
// 布局配置
setSizeFull();
add(crud);
crud.setFindAllOperationVisible(false);
// 逻辑配置
crud.setOperations(
() -> userService.findAll(),
user -> userService.save(user),
user -> userService.save(user),
user -> userService.delete(user)
);
}
}
英文:
Here is two examples.
Example 1:
Create a CRUD database with Entity
, Repository
and then Service
.
https://github.com/alejandro-du/crudui/tree/master/demo/src/main/java/org/vaadin/crudui/demo
And the use of the code can be shown here:
https://github.com/alejandro-du/crudui/blob/master/demo/src/main/java/org/vaadin/crudui/demo/ui/view/SimpleCrudView.java
Here we can see that UserService userService
and GroupService groupService
are being used for storing values in the CRUD database.
Example 2:
Here is an example by me where I'm using @Autowired Repository
Question:
What is the difference between using a Service Object
rather than using @Autowired Repository
Example:
If I using @Autowired Repository backend
here, I can write to the CRUD database here.
crud.setCrudListener(new CrudListener<User>() {
@Override
public Collection<User> findAll() {
return backend.findAllUsers();
}
@Override
public User add(User user) {
return backend.add(user);
}
@Override
public User update(User user) {
return backend.update(user);
}
@Override
public void delete(User user) {
backend.remove(user);
}
});
And if I'm using Service object like this one. I can also write to the CRUD database.
@Route(value = "simple", layout = MainLayout.class)
public class SimpleCrudView extends VerticalLayout {
public SimpleCrudView(UserService userService, GroupService groupService) {
// crud instance
GridCrud<User> crud = new GridCrud<>(User.class);
// grid configuration
crud.getGrid().setColumns("name", "birthDate", "maritalStatus", "email", "phoneNumber", "active");
crud.getGrid().setColumnReorderingAllowed(true);
// form configuration
crud.getCrudFormFactory().setUseBeanValidation(true);
crud.getCrudFormFactory().setVisibleProperties(
"name", "birthDate", "email", "salary", "phoneNumber", "maritalStatus", "groups", "active", "mainGroup");
crud.getCrudFormFactory().setVisibleProperties(
CrudOperation.ADD,
"name", "birthDate", "email", "salary", "phoneNumber", "maritalStatus", "groups", "active", "mainGroup",
"password");
crud.getCrudFormFactory().setFieldProvider("mainGroup",
new ComboBoxProvider<>(groupService.findAll()));
crud.getCrudFormFactory().setFieldProvider("groups",
new CheckBoxGroupProvider<>(groupService.findAll()));
crud.getCrudFormFactory().setFieldProvider("groups",
new CheckBoxGroupProvider<>("Groups", groupService.findAll(), Group::getName));
crud.getCrudFormFactory().setFieldProvider("mainGroup",
new ComboBoxProvider<>("Main Group", groupService.findAll(), new TextRenderer<>(Group::getName), Group::getName));
// layout configuration
setSizeFull();
add(crud);
crud.setFindAllOperationVisible(false);
// logic configuration
crud.setOperations(
() -> userService.findAll(),
user -> userService.save(user),
user -> userService.save(user),
user -> userService.delete(user)
);
}
}
答案1
得分: 1
在我看来,当您在服务层工作时,可以注入多个存储库,但如果您使用存储库,那么只有一个实体对象。
基本上只对一个实体对象执行CRUD操作,服务并没有太多优势。但当您从在业务逻辑上存在某种连接的2个实体中进行操作时,服务就变得更有意义。
英文:
IMO, when you work on the service layer, you can inject multi repository, but if you using repository so there's only 1 entity object.
basically just CRUD on 1 entity object, service does not take much advantage. but when you working from 2 entities which have some connection in business logic then service makes more sense.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论