使用Service和使用repository之间有什么区别? Spring Boot

huangapple go评论83阅读模式
英文:

What's the difference between using a Service compared to using a repository? Spring Boot

问题

以下是翻译好的部分:

示例 1:

使用 EntityRepositoryService 创建一个 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 userServiceGroupService groupService 被用来将值存储在 CRUD 数据库中。

示例 2:

以下是我自己的一个示例,其中我使用了 @Autowired Repository

https://github.com/DanielMartensson/OpenSourceLogger/tree/master/src/main/java/se/danielmartensson/views

这里: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

https://github.com/DanielMartensson/OpenSourceLogger/tree/master/src/main/java/se/danielmartensson/views

Here: https://github.com/DanielMartensson/OpenSourceLogger/blob/master/src/main/java/se/danielmartensson/views/MySQLView.java

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&lt;User&gt;() {
@Override
public Collection&lt;User&gt; 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 = &quot;simple&quot;, layout = MainLayout.class)
public class SimpleCrudView extends VerticalLayout {
public SimpleCrudView(UserService userService, GroupService groupService) {
// crud instance
GridCrud&lt;User&gt; crud = new GridCrud&lt;&gt;(User.class);
// grid configuration
crud.getGrid().setColumns(&quot;name&quot;, &quot;birthDate&quot;, &quot;maritalStatus&quot;, &quot;email&quot;, &quot;phoneNumber&quot;, &quot;active&quot;);
crud.getGrid().setColumnReorderingAllowed(true);
// form configuration
crud.getCrudFormFactory().setUseBeanValidation(true);
crud.getCrudFormFactory().setVisibleProperties(
&quot;name&quot;, &quot;birthDate&quot;, &quot;email&quot;, &quot;salary&quot;, &quot;phoneNumber&quot;, &quot;maritalStatus&quot;, &quot;groups&quot;, &quot;active&quot;, &quot;mainGroup&quot;);
crud.getCrudFormFactory().setVisibleProperties(
CrudOperation.ADD,
&quot;name&quot;, &quot;birthDate&quot;, &quot;email&quot;, &quot;salary&quot;, &quot;phoneNumber&quot;, &quot;maritalStatus&quot;, &quot;groups&quot;, &quot;active&quot;, &quot;mainGroup&quot;,
&quot;password&quot;);
crud.getCrudFormFactory().setFieldProvider(&quot;mainGroup&quot;,
new ComboBoxProvider&lt;&gt;(groupService.findAll()));
crud.getCrudFormFactory().setFieldProvider(&quot;groups&quot;,
new CheckBoxGroupProvider&lt;&gt;(groupService.findAll()));
crud.getCrudFormFactory().setFieldProvider(&quot;groups&quot;,
new CheckBoxGroupProvider&lt;&gt;(&quot;Groups&quot;, groupService.findAll(), Group::getName));
crud.getCrudFormFactory().setFieldProvider(&quot;mainGroup&quot;,
new ComboBoxProvider&lt;&gt;(&quot;Main Group&quot;, groupService.findAll(), new TextRenderer&lt;&gt;(Group::getName), Group::getName));
// layout configuration
setSizeFull();
add(crud);
crud.setFindAllOperationVisible(false);
// logic configuration
crud.setOperations(
() -&gt; userService.findAll(),
user -&gt; userService.save(user),
user -&gt; userService.save(user),
user -&gt; 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.

huangapple
  • 本文由 发表于 2020年9月22日 17:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64006544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定