简体   繁体   English

我如何干掉 Android 改造调用?

[英]How I can DRY Android retrofit calls?

I have several repository classes in my code.我的代码中有几个存储库类。

For example, this is UserRepository:例如,这是 UserRepository:

public class UserRepository {

public static String TAG = "UserRepository";

ApiService mApiService;

SharedPreferences mPrefs;
Context mContext;

RemoteDataSource<User> mRemoteDataSource;


public UserRepository() {
    mApiService = new RetrofitClient().getApiService();
    mContext = App.getAppContext();
    mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mRemoteDataSource = new RemoteDataSource<>();
}

public RemoteDataSource getRemoteDataSource() {
    mRemoteDataSource.setIsLoading();
    Call<ApiResponse> userCall = mApiService.getUserInfo(mPrefs.getString(User.TOKEN_NAME, null));
    userCall.enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
            mRemoteDataSource.setIsLoaded();
            mRemoteDataSource.setData(response.body().getUser());
            mRemoteDataSource.setStatus(response.body().getStatus());
            mRemoteDataSource.setMessage(response.body().getMessage());
        }

        @Override
        public void onFailure(Call<ApiResponse> call, Throwable t) {
            Log.e(TAG, t.getMessage());
            mRemoteDataSource.setFailed(t.getMessage());
        }
    });

return mRemoteDataSource;
}

}

And this is BonusRepository:这是 BonusRepository:

public class BonusRepository {

public static String TAG = "BonusRepository";

ApiService mApiService;

SharedPreferences mPrefs;
Context mContext;

LiveData<Bonus> mBonus;
String mId;
RemoteDataSource<Bonus> mRemoteDataSource;

public BonusRepository(String id) {
    mId = id;
    mApiService = new RetrofitClient().getApiService();
    mContext = App.getAppContext();
    mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mRemoteDataSource = new RemoteDataSource<>();
}

public RemoteDataSource getRemoteDataSource() {
    mRemoteDataSource.setIsLoading();
    Call<ApiResponse> bonusCall = mApiService.getBonus(mPrefs.getString(User.TOKEN_NAME, null), mId);
    bonusCall.enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
            mRemoteDataSource.setIsLoaded();
            mRemoteDataSource.setData(response.body().getBonus());
            mRemoteDataSource.setStatus(response.body().getStatus());
            mRemoteDataSource.setMessage(response.body().getMessage());
        }

        @Override
        public void onFailure(Call<ApiResponse> call, Throwable t) {
            Log.e(TAG, t.getMessage());
            mRemoteDataSource.setFailed(t.getMessage());
        }
    });
    return mRemoteDataSource;
}
}

getRemoteDataSource methods in both classes are equal, except两个类中的getRemoteDataSource方法是相同的,除了

Call<ApiResponse> userCall = mApiService.getUserInfo(mPrefs.getString(User.TOKEN_NAME, null));

and mRemoteDataSource.setData(response.body().getUser());mRemoteDataSource.setData(response.body().getUser()); in UserRepository在用户库中

differs with:不同之处在于:

Call<ApiResponse> bonusCall = ApiService.getBonus(mPrefs.getString(User.TOKEN_NAME, null), mId);

and mRemoteDataSource.setData(response.body().getBonus());mRemoteDataSource.setData(response.body().getBonus()); in BonusRepository.在 BonusRepository 中。

In other repositories I have similar duplicate code.在其他存储库中,我有类似的重复代码。

I want to remove this duplications, but doesn't find any good solution.我想删除这些重复项,但没有找到任何好的解决方案。

What is the best way to DRY my code?干燥我的代码的最佳方法是什么?

Create an abstract class parent to all your repositories' ones, and implement the getRemoteDataSource() method, calling a new abstract method, which will be the only specific one, in each specific implementation.为所有存储库创建一个抽象类父级,并实现getRemoteDataSource()方法,调用一个新的抽象方法,这将是每个特定实现中唯一的特定方法。

For instance:例如:

public class AbstractRepository {

protected abstract Call<ApiResponse> performCall();

public RemoteDataSource getRemoteDataSource() {
    mRemoteDataSource.setIsLoading();
    Call<ApiResponse> userCall = performCall();
    userCall.enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
            mRemoteDataSource.setIsLoaded();
            mRemoteDataSource.setData(response.body().getUser());
            mRemoteDataSource.setStatus(response.body().getStatus());
            mRemoteDataSource.setMessage(response.body().getMessage());
        }

        @Override
        public void onFailure(Call<ApiResponse> call, Throwable t) {
            Log.e(TAG, t.getMessage());
            mRemoteDataSource.setFailed(t.getMessage());
        }
    });

return mRemoteDataSource;
}

}

And then you can perform something like:然后您可以执行以下操作:

public class UserRepository extends AbstractRepository {

public static String TAG = "UserRepository";

ApiService mApiService;

SharedPreferences mPrefs;
Context mContext;

RemoteDataSource<User> mRemoteDataSource;

public UserRepository() {
    mApiService = new RetrofitClient().getApiService();
    mContext = App.getAppContext();
    mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mRemoteDataSource = new RemoteDataSource<>();
}

protected Call<ApiResponse> performCall() {
    return mApiService.getUserInfo(mPrefs.getString(User.TOKEN_NAME, null));
}

} }

I let you adapt to your needs, but this is the best solution.我让你适应你的需求,但这是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM