Designing a DAO Model for an Existing Project

Most of the DAO examples I've seen consist of simple queries containing only one table.

I am working on refactoring a non-DAO project that has many SQL queries where multiple tables are used. My question is, what's the best way to design a model for a DAO? ... In the examples below, I could create an object that covers each specific request. However, I'm not sure if this is good practice or not. for example is it better to have one object representing each db table?

CustomerPaymentDAO to cover this request:

select
    a.username,
    p.creation_date,
    p.amount,
    c.card_type
from
    account      a,
    payment      p,
    payment_type t,
    payment_card c
where
...

      

CustomerPurchaseDAO to cover this request:

select
    a.username,
    i.name,
    i.cost,
    c.name,
    v.value
from
    account      a,
    item         i,
    category     c,
    voucher      v
where
...

      

+3


source to share


1 answer


Generally speaking, there are two options:

  • Create an entity corresponding to each table and specify the required relationships (many-to-many, many-to-one, one-to-one).

  • In the database, create a view for each query and create entities based on the view (in your example, two views + two entities).



The second case is suitable for read-only objects. If you need to create / update / delete objects, you need to create an object corresponding to each individual table.

+1


source







All Articles