Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data dao pattern

I'm starting developing for ios, and now i'm studying core-data. One thing was not clear for me, when i was studying a lot of people was managing core-data entitys on the controller. For me this isn't MVC, since core-data is from Model layer.

So i think will be nice to implement core-data using DAO pattern, but before i wanna know if there is any core-data pattern or if there's some cons implementing DAO using core-data?

like image 533
Guilherme Torres Castro Avatar asked Feb 03 '13 22:02

Guilherme Torres Castro


People also ask

What is a DAO pattern?

The Data Access Object (DAO) pattern is a structural pattern that allows us to isolate the application/business layer from the persistence layer (usually a relational database but could be any other persistence mechanism) using an abstract API.

Is DAO a design pattern?

DAO stands for Data Access Object. DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.

What is the difference between DAO and Repository patterns?

DAO is an abstraction of data persistence. However, a repository is an abstraction of a collection of objects. DAO is a lower-level concept, closer to the storage systems. However, Repository is a higher-level concept, closer to the Domain objects.


1 Answers

It is indeed correct to avoid implementing data look-up methods in the controller. This way the philosophy of the MVC design pattern is adhered to: the controller should just be calling high-level "glue" code and therefore acting as a document that describes how the view is interacting with the model.

With regards to persistent objects, there are two main approaches to this:

  • Use the ActiveRecord pattern
  • Use the Data Access Object pattern.

A Data Access Object (DAO) is an interface dedicated to the persistence of a model/domain object to a data-source.

The ActiveRecord pattern puts the persistence methods on the model object itself, whereas the DAO defines a discrete interface. The advantage of the DAO pattern is:

  • Its easy to define another style of persistence, eg moving from a Database to cloud, without changing the interface and thus effecting other classes.

  • The persistence concerns are modularized away from the main model object concerns.

The advantage of the ActiveRecord pattern is simplicity.

ActiveRecord for CoreData

At the present time the ActiveRecord pattern seems to be a lot more popular among Objective-C developers. The following project provides ActiveRecord for CoreData: https://github.com/magicalpanda/MagicalRecord

DAO for CoreData

I'm not familiar with a widely used library that provides the DAO pattern for CoreData. However, it could be quite easily applied without the assitance of a library:

  • Define all your data methods for a particular entity - findByName, save, delete, etc on a protocol.
  • Implement the protocol by calling the appropriate CoreData methods.

NB: The example project for the Typhoon framework will soon include some examples of applying the DAO pattern with CoreData.

like image 126
Jasper Blues Avatar answered Oct 04 '22 17:10

Jasper Blues