It's your turn
You’re a new developer at a small consulting company. This company provides information about cost of living in cities around the world to international businesses. You've replaced another developer who has moved on to a new job. Previous developers mean existing code: instead of starting a project from scratch, you are inheriting an existing codebase, and picking up where the previous dev left off.
You are asked to add some new features to the code, but right off the bat, you realize that there are no unit tests whatsoever! You are a smart Java developer, and you know this will be a bit of a minefield! 💥 So, you want to start by adding some unit tests. But unfortunately, the architecture of the code does not allow it; all the logic is mixed together in the controller class. 😱
It’s not all bad news! The previous developer has left some good comments in the code that explain what is what. You want to take this code and refactor it according to three-tier architecture, and wire components to each other using dependency injection. So, you scribble out a quick to-do checklist, which reads as follows:
Move all the logic regarding initializing and searching the rental data into a repository class called RentRepository.
Move the calculation logic regarding rent calculation in the target currency into a service class RentService.
Move the logic to call the remote API, and get the conversion rate into a service called CurrencyService.
Instead of creating
newservice and repository instances, create constructors and use dependency injection annotations to wire your components to each other.
Organize classes into packages.
As it is a refactoring effort, the application should still work as it used to before changes.
You have access to the source code, which is a Spring Boot application, via this ZIP file. You'll need to download, unzip, and import it into your IDE. Now it’s time to put all that hard earned three-tier architecture, refactoring, and dependency injection knowledge to use and make life easier for you and those that follow in your footsteps! 💪
Check your work!
Check that the following elements are present:
All the logic regarding initializing and searching the rental data has been moved into a repository class called RentRepository.
The calculation logic regarding rent calculation has been moved to the target currency and into a service class RentService.
All the logic to call the remote API and get the conversion rate has been moved into a service called CurrencyService.
Constructors and dependency injection annotations have been used instead of creating
newservice and repository instances.
All classes are organized into packages.
The functionality of the original application has been maintained.