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
Building Serverless Architectures by Cagatay Gurturk.epub
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.
Linux Device Driver Development Cookbook by Rodolfo Giometti(3957)
Embedded Programming with Modern C++ Cookbook by Igor Viarheichyk(3787)
Implementing Cellular IoT Solutions for Digital Transformation by Dennis McCain(3704)
Embedded Linux Development Using Yocto Project - Third Edition by Otavio Salvador & Daiane Angolini(3554)
TinyML Cookbook by Gian Marco Iodice(3471)
Simplifying 3D Printing with OpenSCAD by Colin Dow(2861)
TinyML Cookbook by Gian Marco Iodice & Ronan Naughton(2623)
Fusion 360 for Makers by Lydia Sloan Cline(2231)
Networking A Beginner's Guide by Bruce Hallberg(2229)
Hands-On Linux for Architects by Denis Salamanca(2073)
But How Do It Know? by J. Clark Scott(2039)
Computers For Seniors For Dummies by Nancy C. Muir(2023)
Raspberry Pi and MQTT Essentials by Dhairya Parikh(1980)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1963)
9781803246888-ENHANCING DEEP LEARNING WITH BAYESIAN INFERENCE by Unknown(1918)
Hack and HHVM by Owen Yamauchi(1904)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1878)
MicroPython Projects by Jacob Beningo(1768)
Hands-On Internet of Things with MQTT by Tim Pulver(1731)
