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
- This course uses the open source https://github.com/cmelchior/realmfieldnameshelper project to generate constants for field names.
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, DatabaseInitTransaction.java, 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.