Answer the question
In order to leave comments, you need to log in
What is the best way to implement interface and class?
Hello.
In my application, I need to work with the API of various services. There is only one interface for working with these APIs. It defines methods for checking an access token, getting certain entities.
What is the problem: the interface will be used in the application and in the same service. The application will need to call some API methods on behalf of a specific user. For example:
IApiClient apiClient; //резолвится с помощью Unity
if(apiClient.IsCurrentUserTokenValid()){
var entities = apiClient.getEntities();
//code
}
public interface IApiClient{
// токен текущего пользователя
string AccessToken{get;set;}
// проверяет валидность токена для текущего пользователя
bool IsCurrentUserTokenValid();
// [static] проверяет валидность токена
bool IsUserTokenValid(string token);
// получает сущности текущего пользователя
IEnumerable<Entity> GetCurrentUserEntities(EntityParams params);
// [static] получает сущности пользователя
IEnumerable<Entity> GetUserEntities(string token, EntityParams params);
}
Answer the question
In order to leave comments, you need to log in
Class Library Design Guide
- Type Design Rules --
Choosing Between Classes and Interfaces
I didn't understand anything to be honest. In my opinion token validation is an authentication issue, GetCurrentUserEntities is something similar to getting data. Don't you think that you are mixing several logical constructs in one interface? If you have a lot of methods in the interface - most likely you are doing something wrong. Why do you need a static class? to save resources? you can configure Unity so that the object of the interface you need implements the singleton pattern. Well, or implement it yourself. There will be an object in a single copy and it will perfectly resolve and mock.
Why are you passing the token to the methods? for authorization? Will you check the validity of the token on each method call? Here is the first result of a simple query in Yandex.
As a result, in my opinion, your problem is not in the interface, but in your head. You have little idea of the architecture of what you are doing and are mixing everything together.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question