Dagger 2-从多个独立作用域的组件注入单个对象 [英] Dagger 2 - Injecting from multiple independently scoped components into a single object

查看:73
本文介绍了Dagger 2-从多个独立作用域的组件注入单个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是我希望拥有两个独立的范围,而这些范围实际上并没有属于父子层次结构。就我而言,我需要两种类型的范围:

The issue that I'm facing is that I want to have two independent scopes that don't really fall into a parent-child hierarchy. In my case, I want two types of scopes:

1)基于功能的范围。例如,当用户输入要素时,将创建作用域组件。当用户离开该功能时,该作用域将被破坏。

1) "Feature" based scopes. e.g., when a user enters a feature, a scoped component is created. When the user leaves that feature, that scope is destroyed.

2)基于活动的作用域(这是针对Android应用的,如果您不使用Android,则对术语感到抱歉)。创建活动时,将创建一个作用域组件。当活动被销毁时,该作用域也将销毁。

2) "Activity" based scopes (this is for an Android app, sorry about the terminology if you don't use Android). When an activity is created, a scoped component is created. When the activity is destroyed, that scope is destroyed.

子组件或组件依赖项都可以满足我的需求。这是因为功能可能在活动被销毁之前结束。同样,活动可能在功能完成之前结束。

Subcomponents nor component dependencies work for what I'm after. This is because the feature could end before the activity is destroyed. Similarly, the activity could end before the feature is finished.

我知道我可以使用提供方法代替成员注入方法,并拥有两个单独的组件,但是我希望能够简单地使用 inject 都进入一个对象。还有其他人对此有任何想法吗?

I know that I can just use provision methods instead of member injection methods and hold two separate components, but I want the simplicity of being able to just inject all my dependencies in one go into a single object. Does anyone else have any thoughts on this?

推荐答案

这是我能想到的最佳解决方案。我将与活动相关的对象耦合到应用程序中的其他状态并实现了类似的方法。

This is the best solution I can think of. I felt the same way about coupling my Activity-related objects to other states in my app and implemented something like this.

说您有三个范围:应用程序范围,活动范围,以及某些功能范围(例如,具有其个人资料信息的用户登录状态等)。 Activity和Feature范围是Application范围的子级,但是Feature和Activity是不相关的兄弟级。

Say you have three scopes: Application scope, Activity scope, and some Feature scope (eg like a User login state that has their profile info and such). The Activity and Feature scopes are children of the Application scopes, but Feature and Activity are unrelated siblings.

不要将Featured范围对象直接注入到Activity中。相反,应将包含吸气剂的桥注入功能范围内的一侧。

Do not directly inject the Featured scope objects directly into the Activity. Instead, inject a bridge that contains getters to the feature scoped side.

示例:

public interface UserManager {
    @Nullable
    User getLoggedInUser();

    @Nullable
    ShoppingCart getShoppingCart();
}

这些方法可为空,因为用户在登录时可能登录也可能未登录其他对象尝试访问它们。如果为空,则用户未登录;

These methods are nullable because the user may or may not be logged in when the other objects attempt to access them. If they are null, the user is not logged in; the feature is not activated.

此功能的实现从Feature范围方面自由地执行依赖项注入,因为它没有从Activity Scope中引用任何内容。

The implementation of this freely performs the dependency injection from the Feature scoped side since it does not reference anything from the Activity Scope.

public class UserManagerImpl implements UserManager {
   @Inject
   ShoppingCart cart;

   @Inject
   User user;

    public UserManagerImpl(MyApplication application){
        UserScopeComponent component = application.getUserComponent();
        if(component != null) {
            //only attempt injection if the component exists (user is logged in)
            component.inject(this);
        }
    }

    //put simple getters here. They will return null objects if the component didnt inject anything.
}

ApplicationModule提供此对象。它不是应用程序范围或单例;您希望每次注入时都重新初始化它,以防登录状态更改。

The ApplicationModule Provides this object. It is not an Application scope or a singleton; you want to initialize it again every time it is injected in case the login state has changed.

@Provides
//No Scope
UserManager provideUserManager(){
    return new UserManagerImpl(context);
}

然后,您可以从应用程序中的任何位置注入UserManager,吸气剂,检查输出是否不为null,然后离开。您的功能状态不再与活动状态关联,您可以自由。

From then on you can inject the UserManager from anywhere in your app, call its getter, check if the output is not null, and away you go. Your Feature state is no longer coupled to the Activity state and you can be free.

这篇关于Dagger 2-从多个独立作用域的组件注入单个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆