I would like to know what is the best practice to use the ObjectManager, should I use a single manager in every application?, in order to avoid transaction problems and memory leaks, as well as just an idbconnection instance?
A Manager lifecycle should be as short as possible.
In VCL, for example, I use a single TAureliusManager in a form and make the ObjManager somehow accesible from all dependent forms and/or units. I often have ObjManager as a parameter for some 2nd level forms/functions/procedures, and create/destroy a local instance when passed nil (when there's no need for using a parent manager).
All of these ever pointing to a single AureliusConnection.
I'm not sure if it's the best practice, but at least I've never had transaction problems.
I'm thinking of using this same approach in all my controllers and forms classes, passing the manager created in the Connection's parent class to these others. I believe it is the best approach, even to avoid having objects not destroyed because of several managers
Injecting the same manager into forms that share same objects is a good idea.
But as @Ferrari_Julio said, try to destroy the managers as soon as possible, it's not recommended to keep the managers created holding entity objects just because.
i have this code:
constructor TDatabaseUtilsController.Create(AIDBConn: IDBConnection);
function TDatabaseUtilsController.Query(AFields: array of string;
AValues: array of Variant; isAOwner : Boolean): T;
if not isAOwner then
FUtilManager.OwnsObjects := False;
Criteria := FUtilManager.Find;
for I := 0 to Length( AFields ) -1 do
Filter := Linq [AFields[I] ] = AValues[I];
Results := Criteria.List; if Results.Count > 0 then Result := Results.First else Result := nil; except on E: Exception do begin raise Exception.Create('Erro ao executar Query: ' + sLineBreak + E.Message ); end; end;
FUtilManager.OwnsObjects := True;
the code below use to facilitate the querys in my application, where whenever I need the return object of the Quuery function to stay alive I pass the parameter isAwoner = False, will I have problems that way?
in the MVC model what would be the best approach?
create each separate manager in your controllers by passing the same connection and destroying them after using the class?
If you know what you are doing you will not have problems. But I personally prefer to just keep the manager (in this case, the controller) alive for the time I need the objects returned by it.
That is a very broad question that doesn't have a definitive answer, unfortunately. Even the "MVC model" is debatable, specially in Delphi.