lundi 10 février 2020

Best way for sharind data between services

I have two services. First:

public class UserServiceFirst {

    public Map<String, String> saveUsers(String searchPattern) {
        Map<String, String> userIds = new HashMap<String, String>();
        List<User> users = findUsers(searchPattern);
        for (User user : users) {
            String oldUserId = user.getId();
            user.setId(generateNewUserId());
            userIds.put(oldUserId, user.getId());
        }
        return userIds;
    }

    private List<User> findUsers(String searchPattern) {
        return null;//some logic
    }

    private String generateNewUserId() {
        return null;//some logic
    }
}

and Second:

public class UserServiceSecond {

public void saveUsers(UserIdsHolder userIdsHolder, String searchPattern) {
    List<User> users = findUsers(searchPattern);
    Map<String, String> userIds = new HashMap<String, String>();
    for (User user : users) {
        String oldUserId = user.getId();
        user.setId(generateNewUserId());
        userIdsHolder.put(oldUserId, user.getId());
    }
}

private List<User> findUsers(String searchPattern) {
    return null;//some logic
}

private String generateNewUserId() {
    return null;//some logic
}

}

I don't understand which way best. In the first service, I have method which saves users and returns Map with userIds - old and new. I need this userIds in the next service. In the second service I pass holder. This holder holds userIds. And I pass this holder to the next service.

It is an example of using:

public class Main {
    public static void main(String[] args) {

        List<Book> books = getBook();

        //-----------------FIRST WAY--------------------------------
        UserServiceFirst userServiceFirst = new UserServiceFirst();
        Map<String, String> userIds = userServiceFirst.saveUsers("text");

        for (Book book : books) {
            if (userIds.containsKey(book.getUserId())){
                book.setUserId(userIds.get(book.getUserId()));
            }
            saveBook(book);
        }
        //-----------------FIRST WAY--------------------------------
        //**********************************************************
        //-----------------SECOND WAY--------------------------------
        UserServiceSecond userServiceSecond = new UserServiceSecond();
        UserIdsHolder userIdsHolder = new UserIdsHolder();
        userServiceSecond.saveUsers(userIdsHolder, "text");

        for (Book book : books) {
            if (userIdsHolder.containsOldUserId(book.getUserId())){
                book.setUserId(userIdsHolder.getNewUserId(book.getUserId()));
            }
            saveBook(book);
        }
        //-----------------SECOND WAY--------------------------------

    }

    private static void saveBook(Book book) {
        //logic
    }

    private static List<Book> getBook() {
        return null;//logic
    }
}

I need to process some data in one service and pass results to the next service. But sometimes 5 service needs to results from 2 and 3 services. Now I create a holder with 5 maps and hold data on each map. And pass this holder between services. I think it is bad practice and I want rewrite it.

Aucun commentaire:

Enregistrer un commentaire