Blog Options
Archive
<< April 2024 >>-
Wednesday 24
- Expanding the StellarDS.io tooling -
Monday 22
- Easy Guide to Building a Pexels Gallery App with TMS WEB Core -
Wednesday 17
- New free component for sophisticated file uploads with TMS WEB Core -
Tuesday 16
- Closing the gap with TMS FNC UI Pack new additions -
Thursday 11
- Unveiling the Latest Enhancements in TMS VCL UI Pack -
Friday 5
- TMS components and Delphi and C++Builder 12.1 -
Wednesday 3
- Customizing the login page of your application using TMS Sphinx
- A Python library for using StellarDS.io effortlessly
- Introducing: How it Works with Holger - Building a Pexels gallery app with TMS WEB Core video series
- TMS FNC Cloud Pack with StellarDS.io backend: unparalleled productivity in VCL & FMX
Authors
- Bernard Roussely (1)
- Wagner Landgraf (82)
- Roman Yankovsky (2)
- Bart Holvoet (27)
- Aaron Decramer (18)
- Pieter Scheldeman (99)
- Nancy Lescouhier (32)
- Adrian Gallero (33)
- Bruno Fierens (404)
- Marcos Douglas B. Santos (5)
- Wagner R. Landgraf (1)
- Bradley Velghe (16)
- Bernard (2)
- Andrew Simard (86)
- Holger Flick (15)
- Gjalt Vanhouwaert (30)
- Tunde Keller (22)
- Masiha Zemarai (119)
Blog
All Blog Posts | Next Post | Previous PostMy Top 10 Aurelius Features - #8 Lazy Loading
Bookmarks: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
Bookmarks:
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