Repository module implementation with Context

Mar*_*tin 8 android repository-pattern android-architecture-components

I would like to implement Repository module to handle data operations. I have JSON file in row directory and want create concrete Repository implementation to get data from file. I'm not sure if I can use Context as attribute in the constructor or method of Repository.

e.g.

public class UserRepository {

    UserRepository() {}

    public List<User> loadUserFromFile(Context contex) {
        return parseResource(context, R.raw.users);
    }
}
Run Code Online (Sandbox Code Playgroud)

Han*_*sir 1

我认为将上下文作为属性传递没有任何坏处。如果您不喜欢这个想法,那么您可以通过一种方便的方法检索上下文:Static way to get 'Context' on Android?

  • 请不要静态检索上下文,而是[按需]检索它(/sf/answers/2727710541/) (2认同)