简体   繁体   English

如何使用Models实现简单的REST和ContentProvider架构?

[英]How to implement a simple REST and ContentProvider architecture using Models?

I'm currently developing an android app that interacts with a RESTful webservice. 我目前正在开发一个与RESTful Web服务交互的Android应用程序。 The client is able to perform full CRUD on the webservice. 客户端能够在Web服务上执行完整的CRUD。 After searching for best practices I watched the Google I/O 2010 keynote about REST clients which is cited in almost all articles. 在搜索最佳实践后,我观看了几乎所有文章中引用的关于REST客户端Google I / O 2010主题演讲

To use as much of the android platform as possible, I decided to go with Option B using a ContentProvider and a SyncAdapter . 为了尽可能多地使用Android平台,我决定使用ContentProviderSyncAdapter来使用Option B. This provided me with the built-in account system, content observers and periodic syncs when internet is availble. 这为我提供了内置的帐户系统,内容观察者和互联网可用时的定期同步。

Because one very important feature of our App is offline editing and availability (it is used in environments with bad reception) we want to keep as much relevant data locally as possible. 因为我们的应用程序的一个非常重要的功能是离线编辑和可用性(它用于接收不良的环境),我们希望尽可能在本地保留相关数据。

The client communicates with the server using a RESTful API in Json , the data from the server is deserialized using Gson on Models (POJO's, Plain old java objects). 客户端使用Json中RESTful API与服务器通信,使用Gson on Models (POJO,Plain old java对象)对来自服务器的数据进行反序列化。

To keep the code clear and easy to read I tried building my own object mapper instead of using a Cursor directly. 为了保持代码清晰易读,我尝试构建自己的对象映射器而不是直接使用Cursor This mapper provides default CRUD-operations and maps Cursors to Models when reading data and Models to ContentValues when writing data. 此映射器提供默认的CRUD操作,并在读取数据时将游标映射到模型 ,在写入数据时将模型映射到ContentValues

However, this architecture feels very bloated. 但是,这种架构感觉非常臃肿。

  • First of all it is not possible to get reliable information about the current SyncState (to provide feedback to the user). 首先,不可能获得关于当前SyncState可靠信息(以向用户提供反馈)。 (Now "hacked" using this SO answer ) (现在使用这个SO答案 “被黑了”)

  • The second problem is that for each resource I will need: A model, A mapper, A table definition and ContentProvider URIs . 第二个问题是我需要的每个资源: 模型,映射器,表定义和ContentProvider URI Which is a lot of code to manage for just one resource. 这只是一个资源管理的代码很多。

  • The third problem is that requiring Models through my mapper I blocked myself from using a CursorLoader in the Activities. 第三个问题是通过我的映射器要求模型我阻止自己在活动中使用CursorLoader

Bottom line 底线

I'm looking for a maintainable and lightweight way of having offline content and synchronisation with a RESTful webservice using Json . 我正在寻找一种可维护轻量级的方法来使用Json进行离线内容和与RESTful Web服务的同步 Also I would like to be able to use Models in my code because user.getName() is a lot more developer friendly than cursor.getString(cursor.getColumnIndex(UserDataSource.COLUMN_NAME)); 此外,我希望能够在我的代码中使用模型 ,因为user.getName()cursor.getString(cursor.getColumnIndex(UserDataSource.COLUMN_NAME));更加开发人员友好cursor.getString(cursor.getColumnIndex(UserDataSource.COLUMN_NAME)); (Which is currently hidden in my Mapper class). (目前隐藏在我的Mapper类中)。

A good example on mapping would be Dapper combined with Dapper Extensions written for .NET but similar to my approach, however my approach required all columns and fields to be defined in many different files (see above). 关于映射的一个很好的例子是Dapper结合为.NET编写的Dapper Extensions ,但与我的方法类似,但是我的方法需要在许多不同的文件中定义所有列和字段(见上文)。

Also, I'm considering to drop the ContentProvider from my code because it feels very bloated and obsolete for such a simple task. 此外,我正在考虑从我的代码中删除ContentProvider ,因为它对于这么简单的任务感觉非常臃肿和过时。

Have you considered using an ORM? 您考虑过使用ORM吗? I've had success with a combination of OrmLite and Jackson in the past. 过去,我在OrmLiteJackson的合作中取得了成功。 You will have to write your own service for synchronization, but OrmLite takes care of the heavy lifting when it comes to your data models. 您必须编写自己的服务以进行同步,但OrmLite负责处理数据模型时的繁重工作。

Although you won't get some of the niceties that the ContentProvider + SyncAdapter combination provides (still no CursorLoader when you're using POJOs), this setup may ease the burden of a complex schema and/or lots of types. 虽然您不会获得ContentProvider + SyncAdapter组合提供的一些细节(当您使用POJO时仍然没有CursorLoader),但此设置可以减轻复杂模式和/或许多类型的负担。

Here's an example of a table definition from OrmLite's homepage: 以下是OrmLite主页的表定义示例:

@DatabaseTable(tableName = "accounts")
public class Account {
    @DatabaseField(id = true)
    private String name;

    @DatabaseField(canBeNull = false)
    private String password;   
}

As you can see, the model + mapping + table definition comes together very quickly and painlessly. 正如您所看到的,模型+映射+表定义非常快速且无痛地汇集在一起​​。

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

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