如何解决具有自身引用的模型类中的dagger-2循环依赖注入问题?

时间:2019-04-18 13:35:38

标签: android dependency-injection dagger-2 dagger

我在项目中使用Dagger 2,效果很好,但是现在我更改了其中一个模型以引用自身(具有相同类型的父字段),此后我们无法编译该项目并报告

> error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public interface AppComponent {

我正在使用github项目来模拟示例

可以使用下面的项目https://github.com/kioko/android-liveData-viewModel/issues/4

重现该问题。

我所做的就是为模型添加一个自引用。

我知道dagger-2不允许循环注入,但是在这里它是模型类,并且不能在任何地方直接注入,而是在注入它的服务类中引用它。

我不确定我是否错过了任何可能愚蠢的事情,但这已经占用了我很多时间,而我仍然处在开始的位置,无法取得任何进展。

任何快速指针都应该有很大帮助。

        primaryKeys = {"id"})
@TypeConverters(TmdbTypeConverters.class)
public class Movie {
    @SerializedName("id")
    @Expose
    @NonNull
    public final int id;
    @SerializedName(value = "poster_path")
    public String posterUrl;
    public Double rating;
    @SerializedName(value = "release_date")
    public String releaseYear;
    public String title;
    public Boolean adult;
    public String overview;
    @SerializedName(value = "original_title")
    public String originalTitle;
    @SerializedName(value = "original_language")
    public String originalLanguage;
    @SerializedName(value = "backdrop_path")
    public String backdropPath;
    public Double popularity;
    @SerializedName(value = "vote_count")
    public Integer voteCount;
    public Boolean video;
    @SerializedName(value = "vote_average")
    public Double voteAverage;
    @SerializedName(value = "genre_ids")
    public List<Integer> genreIds;

    @SerializedName(value = "parent")
    public Movie parent;

    public Movie(int id, String posterUrl, Double rating, String releaseYear, String title, Boolean adult,
                 String overview, String originalTitle, String originalLanguage, String backdropPath,
                 Double popularity, Integer voteCount, Boolean video, Double voteAverage, List<Integer> genreIds, Movie parent) {
        this.id = id;
        this.posterUrl = posterUrl;
        this.rating = rating;
        this.releaseYear = releaseYear;
        this.title = title;
        this.adult = adult;
        this.overview = overview;
        this.originalLanguage = originalLanguage;
        this.originalTitle = originalTitle;
        this.backdropPath = backdropPath;
        this.popularity = popularity;
        this.voteCount = voteCount;
        this.video = video;
        this.voteAverage = voteAverage;
        this.genreIds = genreIds;
        this.parent = parent;
    }
}
public interface TmdbService {

    @GET("movie/top_rated?")
    LiveData<ApiResponse<MovieResult>> getTopRatedMovies();

    @GET("movie/popular?")
    LiveData<ApiResponse<MovieResult>> getPopularMovies();

    @GET("movie/latest")
    LiveData<ApiResponse<MovieResult>> getLatestMovies();

    @GET("discover/movie?sort_by=popularity.desc")
    LiveData<ApiResponse<MovieResult>> discoverPopularMovies();

    @GET("search/movie?")
    LiveData<ApiResponse<MovieResult>> searchMovies(@Query("query") String query);

    @GET("movie/{movie_id}")
    LiveData<ApiResponse<Movie>> getMovieById(@Path("movie_id") int movieId);

    @GET("movie/{movie_id}/similar")
    LiveData<ApiResponse<Movie>> getSimilarMovies(@Path("movie_id") int movieId);

    @GET("genre/movie/list")
    LiveData<ApiResponse<GenreResponse>> getGenres();

    @GET("movie/{movie_id}/videos")
    LiveData<ApiResponse<VideoResult>> getMovieVideos(@Path("movie_id") int movieId);
}

0 个答案:

没有答案