Sign in Go Pro

Realm & Android Architecture Components

How to Apply the DAO Pattern to Realm


Up next



The Data Access Object (DAO) pattern helps encapsulate your data access logic into a single layer. This isolates your persistence implementation details from the rest of your application making it easier to adapt and test.

This lesson will teach you how to apply the DAO pattern with Realm. It's only that Course Notes section, at the end, that is new.

Summary of Content (what you will learn):

  • What the DAO pattern is and what it isn't
  • The benefits of using the DAO pattern with Realm
  • How to apply it within your Realm application
  • Controlling the lifecycle of the Realm Instance within your DAO
  • How to manage transactions and within your DAOs

Additional Materials

Course Notes


Why didn't you create a instance of Realm inside your DAO class?

Passing a Realm instance into the DAO during construction gives you the greatest flexibility in how you use the DAO.

Particularly in a transaction which spans multiple DAOs or even multiple updates, inserts or deletes in a single DAO, as in this example from the lesson,, having the ability to pass in the realm instance during DAO construction allows all of our updates/inserts/deletes to happen in the same transaction, instead of multiple separate transactions.


Lessons in Realm & Android Architecture Components