The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

indrit (3) [Avatar] Offline
#1
Hello,

very nice book!

A simple question: in one of my applications I choose to do manual DI, I want to implement the custom scope concept and in particular I want to use the cache scope. My question is if I have done it correct, caching functionality is added into the object provider.

Is this correct? Or it is wrong because:
- the provider is doing 2 things (generate & cache) so SRP is somehow violated (probably because of the last, this implementation is wrong anyway)
- the provider have to be a no-scope provider (only generate)
- the caching logic must be within the injector; in this simple case doing manual DI the injector is actually a Factory

Is it correct to create a generic "Cache Provider" that uses a "No-scope Provider"? In this case cache-provider will handle only the caching and the no-scope provider will handle only the object generation. If I'm not wrong in this case this is the correct solution.

Please, give me a feedback. Thank you very much indeed.

@ThreadSafe
public class TokenProvider extends AbstractProvider<Token> {
private final TokenService tokenService;

@GuardedBy("this") private Token cachedToken;

public TokenProvider(@Nonnull TokenService tokenService) {
this.tokenService = tokenService;
}

public Token get() {
return cachedGet();
}

private synchronized Token cachedGet() {
if (cachedToken == null || !cachedToken.isValid()) {
cachedToken = tokenService.generate();
}

return cachedToken;
}
}