U
U
Uncle Seryozha2016-07-28 19:01:02
ASP.NET
Uncle Seryozha, 2016-07-28 19:01:02

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
}

Unity will resolve the IApiClient and pass the current user's access token to the ApiClientImpl constructor.
Also, IApiClient will be used in the service. There you will need to run through a large number of users, check their access tokens, perform some actions with the API on behalf of different users. There can be a lot of users.
It would be possible to make the ApiClientImpl class static, but I read that resolving such a thing with IoC is not very cool. You can make a regular class, but then for each user you will need to create an instance of ApiClientImpl (and there are a lot of users). Whether it will load the server? The last option I thought about was to make some of the methods static. But I don't know if this is normal. The result will be something like this:
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); 
}

There are a lot of methods in the interface, wouldn't it be somehow redundant to write some method for the current user and static for all?
Everything seems to be simple, but for the third day I can’t figure out how to do it better. Help me!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrDywar Pichugin, 2016-07-28
@Dywar

Class Library Design Guide
- Type Design Rules --
Choosing Between Classes and Interfaces

D
Dmitry Kovalsky, 2016-07-28
@dmitryKovalskiy

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 question

Ask a Question

731 491 924 answers to any question