将依赖注入与 Roboguice 结合使用?
我正在开发一个 Android 项目,我想知道有关构建 Android 应用程序的良好架构的任何建议。
我想使用 Roboguice 使用依赖注入,并且我一直在阅读有关 MVVM 模式或 MVC 模式的内容 (Android MVVM 设计模式示例)。
我还知道 roboguice 有一个非常酷的基于上下文的事件的引发和处理功能,由于代码是解耦的,因此可以非常容易测试。
关于工作设计模式有什么建议吗?您使用过或开发过的可测试且可扩展的架构?
I'm working on an Android project and I would like to know any recommendations about what's a good architecture to build an android application.
I want to use dependency injection using Roboguice and I've been reading about MVVM pattern or MVC pattern (Android MVVM Design Pattern Examples).
Also I know that roboguice have a pretty cool Context-Based Event's raising and handling feature that could be very testable as the code is decoupled.
Any recommendations on a working design pattern? a testable and scalable architecture you have worked with or developed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Android 平台提供了一组通用的设计模式,并且与 Web 应用程序相比,您获得的硬件资源有限,因此通常最好坚持在生产代码中直接使用这些模式。还有其他框架可以“包装”基础平台;如果您有特定目的(或者可能是为了原型设计/实验),这些都是值得研究的,但为了获得最佳级别的支持,您通常最好坚持使用标准组件。
在开发 UI 解决方案时,这是一个很好的资源: http://www.androidpatterns.com/
特别针对 DI:有一个 < a href="http://www.springsource.org/spring-android">Android 的 Spring 框架,我已经使用过它,它看起来很有前途。您已经提到 Roboguice 作为另一种替代方案。然而,为了避免性能和库开销,我仍然发现最简单的方法是编写一个简单的基于反射的类,在我自己的代码中注册和注入依赖项。类似于这种方法,除了我通常将注入代码移动到单独的单例和引用中从那里开始。
根据我的经验,大多数第三方产品目前还不够成熟,无法依赖,而且除了基础平台提供的功能之外,并没有真正为您提供太多功能。然而,它们在不断进步,所以一定要时不时地尝试一些大牌的产品。
The Android platform provides a common set of design patterns, and with the limited hardware resources you get compared to Web-apps it is still often best to stick with using these directly in production code. There are other frameworks that sort of "wrap" the base platform; these are worth looking into if you have a specific purpose (or perhaps for prototyping/experimenting), but for the best level of support you are generally best sticking with the standard components.
This is a great resource when working on UI solutions: http://www.androidpatterns.com/
Specifically for DI: There is a Spring framework for Android, I've had a play with it and it looks quite promising. You've already mentioned Roboguice as another alternative to this. However, to avoid performance and library overhead, I still find the easiest approach is to write a simple reflection-based class that registers and injects dependencies within my own code. Similar to this approach, except I usually move the injection code into a separate singleton and reference it from there.
In my experience most of the third-party offerings are not yet mature enough to rely on right now, and don't really give you much on top of what the base platform provides. They are constantly progressing, however, so be sure to experiment with the big names from time-to-time.