简体   繁体   中英

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

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<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)
        );
    }

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM