Exists<t> method to Objectmanager

Having exists method in object manager might help developer a little bit.
Sometimes it's enough to check that record with key exists, not the content.
Instead of writing

  lpos := om.Find<Tpointofsale>(aPosId);
  if not assigned(lpos) then
    raise EXDataHttpException.Create(404,'Point of sale not found');

on could write

  if not om.exists<tPointOfSale>(aPosid) then
      raise EXDataHttpException.Create(404,'Point of sale not found');

benefit of this is that later version could produce sql like select 1 from pointofsale where id='00001' instead of
select all fields from pointofsale where ...
with small number of fields that isn't an issue but if you have many fields where is some performance loss.

But you can use projections to speed up:

if om.Find<Tpointofsale>.Select(Linq['Id']).Where(Linq.IdEq(aPosId)) = nil then
  raise EXDataHttpException.Create(404,'Point of sale not found');

And then, of course, you can wrap it in a function, or even a helper class for TObjectManager.

I know that, but there is small downside of it.
If i write exists method using that helper like that, there is huge risks some one uses it and later during same manager life time tries to access properties of same entity - which aren't loaded.

You can then use a different manager. Just create a manager, execute the method, and destroy it. Creating the manager is very lightweight.

Ah, of course, that makes sense.

1 Like