Android 使用上下文实现存储库模块
我想实现Android 使用上下文实现存储库模块,android,repository-pattern,android-architecture-components,Android,Repository Pattern,Android Architecture Components,我想实现Repository模块来处理数据操作。我在行目录中有一个JSON文件,希望创建具体的存储库实现来从文件中获取数据。我不确定是否可以在存储库的构造函数或方法中使用上下文作为属性 e、 g 公共类用户存储库{ UserRepository(){} 公共列表loadUserFromFile(上下文){ 返回parseResource(context,R.raw.users); } } 我认为将上下文作为属性传递没有任何害处。如果您不喜欢这个想法,那么您可以通过一个方便的方法来检索上下文:IM
Repository
模块来处理数据操作。我在行
目录中有一个JSON文件,希望创建具体的存储库
实现来从文件中获取数据。我不确定是否可以在存储库
的构造函数或方法中使用上下文
作为属性
e、 g
公共类用户存储库{
UserRepository(){}
公共列表loadUserFromFile(上下文){
返回parseResource(context,R.raw.users);
}
}
我认为将上下文作为属性传递没有任何害处。如果您不喜欢这个想法,那么您可以通过一个方便的方法来检索上下文:IMHO,您应该使用类似DI(依赖注入)的方法,为您提供类似
AppModule.class
@Module
public class AppModule {
private Context context;
public AppModule(@NonNull Context context) {
this.context = context;
}
@Singleton
@Provides
@NonNull
public Context provideContext(){
return context;
}
}
public class MyApplication extends Application {
private static AppComponent appComponent;
public static AppComponent getAppComponent() {
return appComponent;
}
@Override
public void onCreate() {
super.onCreate();
appComponent = buildComponent();
}
public AppComponent buildComponent(){
return DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
}
@Singleton
public class UserRepository {
UserRepository() {}
@Inject
public List<User> loadUserFromFile(Context contex) {
return parseResource(context, R.raw.users);
}
}
MyApplication.class
@Module
public class AppModule {
private Context context;
public AppModule(@NonNull Context context) {
this.context = context;
}
@Singleton
@Provides
@NonNull
public Context provideContext(){
return context;
}
}
public class MyApplication extends Application {
private static AppComponent appComponent;
public static AppComponent getAppComponent() {
return appComponent;
}
@Override
public void onCreate() {
super.onCreate();
appComponent = buildComponent();
}
public AppComponent buildComponent(){
return DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
}
@Singleton
public class UserRepository {
UserRepository() {}
@Inject
public List<User> loadUserFromFile(Context contex) {
return parseResource(context, R.raw.users);
}
}
UserRepository.class
@Module
public class AppModule {
private Context context;
public AppModule(@NonNull Context context) {
this.context = context;
}
@Singleton
@Provides
@NonNull
public Context provideContext(){
return context;
}
}
public class MyApplication extends Application {
private static AppComponent appComponent;
public static AppComponent getAppComponent() {
return appComponent;
}
@Override
public void onCreate() {
super.onCreate();
appComponent = buildComponent();
}
public AppComponent buildComponent(){
return DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
}
@Singleton
public class UserRepository {
UserRepository() {}
@Inject
public List<User> loadUserFromFile(Context contex) {
return parseResource(context, R.raw.users);
}
}
@Singleton
公共类用户存储库{
UserRepository(){}
@注入
公共列表loadUserFromFile(上下文){
返回parseResource(context,R.raw.users);
}
}
快乐的编码 您想使用上下文做什么?您不需要它来访问文件系统…我需要上下文来访问资源。正如我提到的,我的json文件在W raw目录中。你可以将上下文作为参数传递,没有问题。使用DI Dagger2在你的存储库中提供上下文。你是说raw目录请不要静态检索上下文,检索它上下文应该从拥有存储库的实例化模型的活动中检索,但不是静态的;它应该被检索到,也就是说它应该被传递到gree!这完全违背了保持低耦合的逻辑。最好的方法是在创建存储库对象时注入上下文。