Interface Design Question
I have an interface IAuthenticatable with one method:
Task<Tokens> GetAccessTokensAsync()
that I'd like to use for two different API authentication classes. However, the two authenticated API responses vary, so I'm just wondering what I should do. Use separete interfaces for each API?9 Replies
vary how?
Without any additional context from you - to overcome the difference in the return type you could make your interface generic
This is what I ended up doing, thanks!
The JSON responses varied
Well then I'm not sure whether that is the right approach
If the interface method says that it's for getting access tokens, then it should probably return a string and the lifetime information of the token. The implementation of the interface via specific methods should handle the specific json differences
What's wrong with using the generic interface? Out of curiosity
Nothing inherently wrong with a generic interface. Just that I don't it suits this specific case.
I believe that you want an interface to abstract the specifics of your two implementations, that do essentially the same thing.
Why would you still want to have a unique interface instance for the implementation it abstracts?
I.e., why do you want
ITokenGetter<A> and ITokenGetter<B>, when you could just have ITokenGetter?
Does your project require this complexity?
For TokenGetter A, I receive a token and a an optional refresh token that appears every 15 days (B does not do this). The JSON class to deserialize into is also different from B
Okay, so from what I can see in the docs, any properties not specified in my model class will be ignored by default, so I can remove the ones I don't need. But then I still have the difference of A having a RefreshToken that may or may not be null (it isn't null on day 15), whereas B only has AccessToken
Well you could design a class that allows the refresh token to be null
Both apis would map to that class, and the rest of your app would handle the null refresh
Thank you!