'How to use lazy loading using DAO or other concept

I am use DAO for load object. But the object is complex and in different places of the program only certain parts of this object are needed. There is a need for lazy loading, but it is not clear where to place it.

I would like something similar to include entity framework Options that come to my mind:

  1. in the model object (when accessing a property or a specific method) but in this case, it is necessary to have a DAO inside the object this gives a dependence on a specific type of DAO
  2. in DAO (specific method or include().include() chain) but in this case it is necessary to extend the DAO interface with a strange "include" method or create many DAO combinations, for example: "docWithTab1AndTab2", "docWithTab2WithOutTab1" and etc combinatorial explosion...

if the DAO is responsible for interacting with the data source, then it turns out that it should do lazy loading, then where should the code be located?

rough example:

GenericDao<T: class> = interface
    function find(const id: string): T;
    procedure save(entity: T);
    function update(entity: T): T;
    procedure delete(entity: T);
    function findAll(): TList<T>;
  end;

TUser = class
    Id: string;
    Name: string;
    Adress: TList<TAdress>;
    Phone: TList<TPhone>;
    ... 
    etc TList<> field
end;

TUserFirebirdDao = class(TInterfacedObject, GenericDao<TUser>)
    function find(const id: string): TUser;
  end;

function TUserFirebirdDao.find(const id: string): TUser;
begin
    Result := TUser.Create();
    TFDQuery.Open('select * from users where id = :id');
    ...
    further filling properties from the dataset
    ...
end;

I get filled object, but I need more "Adress" and possibly "Phone". You don't need to download them right away. What to do?

TUser.GetPhone(): TList<TPhone>
begin
    TPhoneFirebirdDao.find()  ???
end;

or

TUserWithPhoneFirebirdDao = class
TUserWithAdressFirebirdDao = class
TUserWithPhoneAndAdressFirebirdDao = class

Or use some other approach, concept? Please answer as clear as possible. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source