title: How to write a better data access layer with Realm
body: <p>I've been using Realm in a few small projects and I quite like it. I'm hoping to move on to using it in bigger projects and I'm looking for better structure my data access layer.</p>
  
  <p>I came across this similar <a href="https://stackoverflow.com/q/33592572/1077789">question</a> and tried to build up on the information I found there. The approach discussed there is the DAO pattern so I gave a shot at that.</p>
  
  <p>This is my model class.</p>
  
  <pre><code>class Chat: Object {
      dynamic var id: String = ""
      dynamic var createdAt: Date = Date()
      dynamic var creatorId: String = ""
      dynamic var title: String?
      let chatMessages = List&lt;ChatMessage&gt;()
  
      override static func primaryKey() -&gt; String? {
          return "id"
      }
  
      convenience init(fromJSON json: JSON) {
          self.init()
          // ...
      }
  }
  </code></pre>
  
  <p>Then I created a <code>ChatDAOProtocol</code> to hold all the convenience helper methods.</p>
  
  <pre><code>protocol ChatDAOProtocol {
      func addMessage(_ message: ChatMessage)
      func getChatThumbnail() -&gt; UIImage
      func getParticipants(includingMe: Bool) -&gt; [Participant]?
      static func getChat(fromId id: String) -&gt; Chat?
      static func getChat(fromCreatorId id: String) -&gt; Chat?
  }
  </code></pre>
  
  <p>Lastly I created another class called <code>ChatHelper</code> that implemented all those protocol methods.</p>
  
  <pre><code>class ChatHelper: ChatDAOProtocol {
      func addMessage(_ message: ChatMessage) {
  
      }
  
      func getChatThumbnail() -&gt; UIImage {
          return UIImage()
      }
  
      func getParticipants(includingMe: Bool) -&gt; [Participant]? {
          return nil
      }
  
      static func getChat(fromId id: String) -&gt; Chat? {
          return nil
      }
  
      static func getChat(fromCreatorId id: String) -&gt; Chat? {
          return nil
      }
  
  }
  </code></pre>
  
  <p>This already seems better than sprinkling all the database related code all over the VCs and stuff. But I still have some doubts.</p>
  
  <p>For example, say if I need to get all the participants of a chat, now I have to call the method on the <code>ChatHelper</code> class. And if I want to get simply the chat title, I call the <code>title</code> property of the <code>Chat</code> object itself. Doesn't seem like a very unified interface. Should I include getters and setters for all the properties in the helper as well. So the <code>Chat</code> object is never directly called (except for maybe creating an instance).</p>
  
  <p>Or</p>
  
  <p>Should I make the <code>Chat</code> object itself conform to the <code>ChatDAOProtocol</code> protocol? So all the convenience methods as well as the properties are directly accessible from the <code>Chat</code> object straight up?</p>
  
  <p>Or is there a better way than both of these?</p>