Persistent Model Framework for Android

A few months ago, a fellow student of mine came back from SAP in South Africa and told me about his new Android project: Androrm, which is a persistent relational data mapper especially for Android. Since I really hate writing ContentProviders and SQL queries, I gave it a try – and as luck would have it, I’m now using it in two of my projects!

Let me just give you a quick overview on what Androrm is about and how easy it is to define your data model with it. Basically it works like this:

1. Defining models

Let’s take two simple models: We have books and authors. All you have to do is extending the Model class and defining your fields:

Note: The source code was taken from the Android documentation and modified by me.

public class Book extends Model {
     
    // fields visibility is set to protected,
    // so the Model class can access it
    protected CharField mTitle;
     
    // zero-argument constructor, that can be called 
    // by androrm, to gather field information
    public Book() {
        super();
         
        // set up field with maxLength parameter
        mTitle = new CharField(80);
    }

    public String getTitle() {
        return mTitle.get();
    }
     
}

public class Author extends Model {
     
    protected CharField mName;
     
    public Author() {
        super();
         
        mName = new CharField(80);
    }

    public String getName() {
         return mName.get();
    }

    public void setName(String name) {
         mName.set(name);
    }
     
}

As you can see, there is a DataField type for each basic data type in java. Use them to define your model’s fields. They have set() and get() methods so you can easily set and get your values.

2. Creating a database

Once your models are defined, call this code in your onCreate() method of your app’s start activity to initialize the Androrm database and register your models:

List<Class<? extends Model>> models = new ArrayList<Class<? extends Model>>(); 
models.add(Author.class);                                                     
models.add(Book.class);                                                     
                                                                               
DatabaseAdapter.setDatabaseName("a_database_name");                                      
DatabaseAdapter adapter = new DatabaseAdapter(getApplicationContext());        
adapter.setModels(models);                                                     

In a next step, we want our books to be connected to their authors:

3. Creating relations

For relations, there is a special DataField called ForeignKeyField. It lets you extend your models with new classes, if basic data types are not enough. You just need a few lines of code:

public class Book extends Model {
     
    // fields visibility is set to protected,
    // so the Model class can access it
    protected CharField mTitle;
     
    // Link the Author model to the Book model.
    protected ForeignKeyField<Author> mAuthor;
     
    // zero-argument constructor, that can be called 
    // by androrm, to gather field information
    public Book() {
        super();
         
        // set up field with maxLength parameter
        mTitle = new CharField(80);
         
        // initialize the foreign key relation
        mAuthor = new ForeignKeyField<Author>(Author.class);
    }

    public String getTitle() {
        return mTitle.get();
    }

   public void setAuthor(Author author) {
        mAuthor.set(author);
   }
     
}

4. Insert your data

This is the part which amazed me the most when I first saw it. You just create your objects – business as usual – and then the magic happens:

// In your activity

Author author = new Author();
author.setName("My Author");
author.save(this);

Book book = new Book();
book.setName("Book name");
book.setAuthor(author);
book.save(this);

That’s it! You just create an instance of your class, do whatever you like and call save() to persist it. No SQL, no complicated ContentProviders. Of course, you can do a lot more with Androrm like querying your data and defining more complex relationships between your models. This was just a quick overview – for more details, please have a look at the documentation.

Androrm is still in an early stage, but – as far as I can tell – it works pretty stable! It is open source and hosted on GitHub, so feel free to contribute or file bugs!
If you find any related projects, please comment on this post!

That’s it for now, thanks for reading!

About these ads

7 thoughts on “Persistent Model Framework for Android

  1. I’m coming to Android from iOS, so I’m kind of spoiled with Core Data on that platform. I’ve been messing around with Androrm for a few days now, and I am really pleased with its performance thusfar.
    I am building a Core Data-like abstraction layer that builds the object map for you from an XML file (if you’re familiar with Propel in PHP, it’s the same thing). The Core Data behavior comes in by handling the saturation of objects on demand, and allowing for fetch requests (e.g. stored procedures)
    Im using Androrm as the backbone. I had to make a few tweaks to the source for thread safety, but left everything else untouched.

  2. Do you know how Androrm handles altering a model? I was playing with it and it looks like when I add a new field to my model it breaks when retrieving any existing data.

    I was hoping that the orm would be smart enough to migrate my existing data to the new model structure.

    The only other orm that I’ve ever used was the Entity Framework(crap!), but it was fancy enough to handle alters gracefully.

  3. Hi. I would like to point out that the above code contains errors. For example the getName method of the Authors class returns void instead of String.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s