Blog
All Blog Posts | Next Post | Previous PostMy Top 10 Aurelius Features - #8 Lazy Loading
Thursday, December 22, 2016
The ability to lazy-load an association is placed at number 8 in the list of My Top 10 Aurelius Features.Suppose you have a TContact class like the following:
type TContact = class private FId: integer; FName: string; FCountry: TCountry; public property Id: integer read FId write FId; property Name: string read FName write FName; property Country: TCountry read FCountry write FCountry; end;
// Get all contacts MyContacts := Manager.Find<TContact>.List; // Get name of country of first contact: FirstContactCountryName := MyContacts[0].Country.Name;
SELECT A.ID AS A_ID, A.NAME AS A_NAME, A.COUNTRY_ID AS A_COUNTRY_ID, B.ID AS B_ID, B.NAME AS B_NAME FROM CONTACT A LEFT JOIN COUNTRY B ON (B.ID = A.COUNTRY_ID)
The lazy-loading feature gives you that flexibility. You just reimplement your class using a special Proxy type:
type TContact = class private FId: integer; FName: string; FCountry: Proxy<TCountry>; function GetCountry: TCountry; procedure SetCountry(const Value: TCountry); public property Id: integer read FId write FId; property Name: string read FName write FName; property Country: TCountry read GetCountry write SetCountry; end; function TContact.GetCountry: TCountry; begin Result := FCountry.Value; end; procedure TContact.SetCountry(const Value: TCountry); begin FCountry.Value := Value; end;
SELECT A.ID AS A_ID, A.NAME AS A_NAME, A.COUNTRY_ID AS A_COUNTRY_ID FROM CONTACT A
SELECT A.ID AS A_ID, A.NAME AS A_NAME FROM COUNTRY A
// Get all contacts and countries at once, // regardless of lazy-load mode in mapping MyContacts := Manager.Find<TContact> .CreateAlias('Country', 'c', TFetchMode.Eager) .List; // Get name of country of first contact: FirstContactCountryName := MyContacts[0].Country.Name;
Wagner Landgraf
This blog post has received 3 comments.
2. Saturday, December 24, 2016 at 12:19:32 PM
Isn''t the answer to your question what is explained in the post/video? You can use CreateAlias with TFetchMode.Eager to "turn" the lazy mode into eager mode and have a single SQL to retrieve them all.
Wagner R. Landgraf
3. Friday, September 4, 2020 at 8:06:16 PM
Thaths perfect!
My questions about object models and ORM''s alyways are about it.
How to optimize SQL when i don''t need retrive all fields from relationship?
The lazy-loading and CreateAlias resolve.
I''m analysing TMS Aurelius and i''m loving it.
My questions about object models and ORM''s alyways are about it.
How to optimize SQL when i don''t need retrive all fields from relationship?
The lazy-loading and CreateAlias resolve.
I''m analysing TMS Aurelius and i''m loving it.
Maicon Saraiva
All Blog Posts | Next Post | Previous Post
This would result in 10 distinct SQL calls, one for each country data. Coding something like that by hand I could end up with just one SQL with some additional code and optimize performance a lot.
Is there something like that available in the Aurelius framework?
Happy XMas!
Stefan
Stefan