Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository or DAO: read data from multiple tables

We have a class in which we have methods:

  • find: select + multiple joins in order to filter data
  • add: insert in multiple table
  • update: update in multiple table
  • delete: delete in multiple table
  • check: multiple select + multiple joins in order to check something

Is it a Repository or a DAO?

like image 428
Combo Avatar asked Nov 25 '25 12:11

Combo


1 Answers

TL;DR;
This looks more like Repository to me; personally.


There is a good part of controversy between Repository and DAO among purists. Many developers use terms DAO and Repository interchangeably. I personally do not put myself deep in that debate. I prefer to focus on my application needs instead.

Following is very rough description of some of the patterns used in Data Access Layer:

Unit Of Work:

  • Handle database connection and transactions.
  • Additionally handle cache, batch queries, tracking etc.
  • Please refer this answer.

Repository:

  • May use UnitOfWork; not always.
  • Not per table, it is per aggregate root.
  • Returns Domain Object; NOT Object State (Entity/POCO).
  • Please refer this answer. Also, this other answer for few more terms.

DAO (Data Access Objects):

  • Tightly bound to database instead of Business Logic.
  • Mostly 1:1 mapping with tables.
  • Each action is transaction.
  • Returns Object State (Entity/POCO).

CQRS (Command Query Responsibility Segregation):

  • Use a different model to write (INSERT, UPDATE, DELETE) data than the model you use to read (SELECT) data.
  • Write operations are covered under Command.
  • Read operations are covered under Query.
  • Each layer can be optimized independently for specific needs.

Query Object:

  • Accept queries as an object in parameter of method.

Data Mapper:

  • Map POCO/Entity classes with database row and vice versa.
  • ORMs (Object Relational Mappers) are based on this pattern.

Active Record:

  • Properties and Methods related to instance are in same class.
  • Mapping happens in same class.
  • Acts on single record.
like image 67
Amit Joshi Avatar answered Nov 28 '25 17:11

Amit Joshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!