简体   繁体   English

哪个版本的RoboGuice可与Android SDK 8配合使用?

[英]What version of RoboGuice works with Android SDK 8?

That's probably part one of my question. 这可能是我的问题之一。

Basically I'm struggling with the actual injection for version 1.1.2. 基本上,我正在为版本1.1.2的实际注入而苦苦挣扎。 I've read the couple of pages on the site, and I feel I'm missing something. 我已经阅读了网站上的几页,并且觉得自己缺少一些东西。

Basically I've done the RoboApplication extension. 基本上,我已经完成了RoboApplication扩展。 I've overridden the addApplicationModules method. 我已经重写了addApplicationModules方法。 I've even made a module. 我什至制作了一个模块。

My module looks like this: 我的模块如下所示:

    public class DataRepository extends AbstractAndroidModule
    {

        @Override
        protected void configure() {
             /*
              * This tells Guice that whenever it sees a dependency on a TransactionLog,
              * it should satisfy the dependency using a DatabaseTransactionLog.
              */
            bind(IDataBaseAdapter.class).to(DataBaseAdapter.class);
        }

    }

In my adapter I have this: 在我的适配器中,我有:

    public class DataBaseAdapter implements IDataBaseAdapter
    {
        private DataBaseHelper _dbHelper;

        private SQLiteDatabase _db;

        @Inject 
        protected static Provider<Context> contextProvider;

        public DataBaseAdapter()
        {
            _dbHelper = new DataBaseHelper(contextProvider.get());
        }
    }

If I don't do there, where is the opportune place for the chunk of code to reside... where I associate injectors? 如果我不在那里,那段代码驻留的合适位置在哪里……我与喷油嘴相关联?

Finally... my Application has an injection of it like so: 最后...我的应用程序注入了它,如下所示:

    public class MyApplication extends RoboApplication 
    {
        public MyApplication()
        {
            super();
        }

        public MyApplication(Context context)
        {
            super();
            attachBaseContext(context);
        }

        @Override
        protected void addApplicationModules(List<Module> modules)
        {
            modules.add(new DataRepository());
        }

        @Inject
        private IDataBaseAdapter adapter;

        public IDataBaseAdapter getAdapter()
        {
            return adapter;
        }

        public void setAdapter(IDataBaseAdapter value)
        {
            adapter = value;
        }
            ...
    }

I'm trying to use the Inject attribute as shown. 我正在尝试使用如图所示的Inject属性。 For example: 例如:

    @Inject 
    private IDataProvider provider; 

A couple of reasons why I'm lost is that I come from a .NET and Flash/ActionScript background plus I've only used StructureMap instead of Ninject (in the .NET world), which I've heard Guice is designed with some of the ideas of Ninject in mind. 我迷路的几个原因是,我来自.NET和Flash / ActionScript背景,另外,我只用过StructureMap而不是Ninject(在.NET世界中),我听说Guice是为某些情况而设计的Ninject的想法。 Could someone help me figure out this small piece? 有人可以帮我弄清楚这小块吗?

I'd really like to focus on using 1.1.2 instead of jumping to 2.x of RoboGuice... especially since it is still in beta, so I hope you all don't mind. 我真的很想专注于使用1.1.2而不是跳转到RoboGuice的2.x ...尤其是因为它仍处于beta版本,所以我希望你们都不要介意。

Thanks again, Kelly 再次感谢,凯利

Android is quite different from standalone / hosted java application. Android与独立/托管的Java应用程序完全不同。 You do not have main() , but you have certain activity units, which are managed by android framework (activities, services , broadcast receivers) 您没有main(),但是您拥有某些活动单元,这些活动单元由android框架(活动,服务,广播接收器)管理

DI is a technique which allows you to eliminate booler plate code by wiring together parts in good object oriented way. DI是一种技术,通过以面向对象的良好方式将零件连接在一起,您可以消除布尔板代码。

As your unit of work is mostly activity, you shall do wiring / creation of your collaborating objects in onCreate() method , and there are dedicated onResume() and onPause() methods (see actviity lifecycle) 由于您的工作单元主要是活动,因此您应该在onCreate()方法中进行布线/创建协作对象,并且有专用的onResume()和onPause()方法(请参见活动生命周期)

Rule of thumb is, does this thing needs to be restarted every time activity loses it focus? 经验法则是,每当活动失去焦点时,是否需要重新启动该程序? If yes, initialize / destroy it in inResume() / onPause(), otherwise - in onCreate() 如果是,请在inResume()/ onPause()中初始化/销毁它,否则-在onCreate()中

And if you like to share objects withing entire application ( running in same JVM ) , it is OK to use singleton pattern in android. 而且,如果您希望与整个应用程序(在同一JVM中运行)共享对象,则可以在android中使用单例模式。 So you may just have singleton injector factory , and cosult it from everywhere: 因此,您可能只拥有Singleton注射器工厂,并从各处寻求帮助:

 InjectorFactory.getInstance(<context if necessary?>).getInstance(whatever you need);

OK, I've figured out what was needed, but I'm not quite sure why after seeing all the information floating out there. 好的,我已经弄清楚了需要什么,但是在看到所有信息浮出水面之后,我不确定为什么。

I basically made this change, and now my test passes. 我基本上做了这个更改,现在我的测试通过了。

public class DataBaseAdapter implements IDataBaseAdapter
{
    private DataBaseHelper _dbHelper;

    private SQLiteDatabase _db;

    @Inject
    public DataBaseAdapter(Provider<Context> contextProvider)
    {
        _dbHelper = new DataBaseHelper(contextProvider.get());
    }
}

While I like using constructors as the tool for injecting, I wonder why it had to work this way, considering that examples I have seen are some kind of reflection class injection. 虽然我喜欢使用构造函数作为注入工具,但考虑到我所看到的示例是某种反射类注入,因此我想知道为什么它必须以这种方式工作。

Anyway, that's this part. 无论如何,这就是这一部分。 Hopefully someone else will find this useful. 希望其他人会发现这很有用。

Cheers, Kelly 干杯,凯利

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

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