EntityDAC

Memory Management

In EntityDAC, all used entities are managed by the data context. It means that the data context stores every created entity or entities loaded from the database in its internal cache. Accordingly, the data context cares about automatic destroying the entities.

Attaching the entity

In order to the data context became aware about the entity and took on further management functions, the entity should be attached to the data context.

Single entity

A manually created entity can be attached either with its Attach method, or using the Attach method of the data context. An entity which is loaded from the database using the GetEntity data context method, is initially attached. When the entity is saved to the database using the data context Save method, it also automatically become attached.

An attached entity must not be destroyed manually, otherwise the exception is raised.

var
  Emp,
  Emp1,
  Emp2: TEmp;
begin
  // the entity is not attached, and it has to be destroyed manually
  Emp := TEmp.Create;
  Emp.Free;
 
  // the entity is loaded from the database and is initially attached
  // manual destroying is not needed
  Emp1 := Context.GetEntity<TEmp>(1);
 
  // the entity is loaded from the database and is attached on Save
  // manual destroying is not needed
  Emp2 := Context.CreateEntity<TEmp>;
  Emp2.Save;
end;

Entity list

In EntityDAC, any list object which is the result of the data context GetEntities method execution, is the TInterfacedObject descendant. Therefore, there is no need to care about its destroying. Also, all entities which constitute the list, are initially attached to the data context and managed by it.

var
  Emps: IEntityEnumerable<TEmp>;
begin
  // its no need to manually destroy the list instance and its items 
  Emps := Context.GetEntities<TEmp>;
end;

LINQ query

Furthermore, EntityDAC implements the LINQ syntax using special helper classes which are also inherited from TInterfacedObject, so their manual destroying is also not required.

var
  Query: ILinqQueryable;
  Emps: IEntityEnumerable<TEmp>;
begin
  // the query expression instance will be destroyed automatically
  Query := Linq.From('Emp').Where('Emp.DeptNo = 1').Select;
  
  Emps := Context.GetEntities<TEmp>(Query);
end;
© 1997-2024 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback