Building Serverless Architectures by Cagatay Gurturk

Building Serverless Architectures by Cagatay Gurturk

Author:Cagatay Gurturk
Language: eng
Format: mobi, epub
Tags: COM011000 - COMPUTERS / Systems Architecture / General, COM051230 - COMPUTERS / Software Development & Engineering / General, COM051000 - COMPUTERS / Programming / General
Publisher: Praful Palekar
Published: 2017-07-18T07:25:51+00:00


Service dependencies

Often, the services we declare have other dependencies. For example, our user service needs a repository service to access the persistence layer. How do we declare these nested dependencies and get the Guice construct to the dependency graph for us?

We can leverage JSR-330 annotations again to declare dependencies for other dependencies. As always, let's look at this in action. Let's start with creating the UserRepository interface in the com.serverlessbook.services.user.repository package:

$ mkdir -p services-user/src/main/java/com/

serverlessbook/services/user/repository

$ touch services-user/src/main/java/com/serverlessbook/

services/user/repository/UserRepository.java

Inside the interface, let's declare the method we need at this stage:

package com.serverlessbook.services.user.repository; import com.serverlessbook.services.user.domain.User; import java.util.Optional; public interface UserRepository { Optional<User> getUserByToken(String token); }

Then, let's create a nonfunctional implementation of this class with the name UserRepositoryDynamoDB:

package com.serverlessbook.services.user.repository; import com.serverlessbook.services.user.domain.User; import java.util.Optional; public class UserRepositoryDynamoDB implements UserRepository { @Override public Optional<User> getUserByToken(String token) { return Optional.empty(); } }

This class always returns an empty User object because at this stage, we do not need the real implementation. We will be implementing the real data layer in the next chapter.

Now we can add UserRepositoryDynamoDB as a dependency to the UserServiceImpl class. Let's add a private field to the UserServiceImpl class and create a constructor for it:

private final UserRepository userRepository; public UserServiceImpl(UserRepository userRepository) { this.userRepository = userRepository; Objects.requireNonNull(userRepository); }

At this step, executing tests again will end up failing because Guice cannot create UserServiceImpl without a zero-argument constructor in this configuration. Actually, the Guice error explains the problem and provides a clue about the solution:

Could not find a suitable constructor in com.serverlessbook.services.user.UserServiceImpl. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.

Then, we must use two steps to complete the puzzle here:

Add the @Inject annotation to the constructor so that Guice will understand that it should construct the object using this constructor, injecting the required parameters to it.

Declare UserRepositoryDynamoDB as the implementation of UserRepository.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.