Using lombok

Although DiVine removes a huge chunk of boilerplate code that would be used for your initialization processes. Your getters, setters and constructors could still take up a lot of space. It is generally recommended to use Lombok to get rid of those code overheads from the source code.

@Service
@Getter
class MinigameManager {
    private final List<Minigame> minigames = new ArrayList<>();
    
    @Inject
    private NetworkManager networkManager;
    
    // ... bunch of other fields
}
@Service
@RequiredArgsConstructor
class UserController {
    @Inject
    private final UserRepository repository;
    
    @Inject(token = "API_TOKEN")
    private final String apiToken;
    
    @AfterInitialized
    public void init() {
        repository.login(apiToken, Scope.READ | Scope.WRITE);
    }
}

The code above will generate a constructor for UserController, which will take in repository and apiToken as parameters. This will be fully compatible with DiVine.

@RequiredArgsConstructor(onConstructor_ = @ConstructWith)
class SessionManager {
    @Inject
    private final SessionCache cache;
    
    public SessionManager() {
        cache = SessionCache.newEmptyCache();
    }
}

Your code might need multiple constructors, where DiVine should use a generated one. You can still put @ConstructWith on these generated constructors.

@Service
@ToString
class DatabaseCredentials {
    private final String database, username, password;
}
@Service
public class ConnectionHandler {
    @Inject
    private @NotNull PacketCodec codec;
    
    @Inject(token = "PROTOCOL_VERSION")
    private int protocolVersion;
    
    public void sendHandshake(@NotNull Node node) {
        node.queue(codec.encode(Packet.HANDSHAKE, protocolVersion));
        node.queue(codec.encode(Packet.MESSAGE, "Hello, World"));
        node.flush();
    }
}

You may explicitly specify null assertations for your injected fields. DiVine will guarantee to always create an instance for your services.

Last updated