You are not logged in.
In my service I would like to have a method to create a new measurement type (TPhoMessart is a measurement type) in the database and to return an ID of the new record.
The following code works awesome:
function TPhoMessartService.CreateNewOne(const AName: RawUTF8): TID;
var
NewMessart: TPhoMessart;
begin
NewMessart := TPhoMessart.Create;
try
NewMessart.Name := AName;
Result := Factory.Rest.Add(NewMessart, True);
finally
NewMessart.Free;
end;
end;
But the documentation says that I have to use the methods ORMAdd and Commit. So, I want to follow the instruction and tried it out as follows:
function TPhoMessartService.CreateNewOne(const AName: RawUTF8): TID;
var
NewMessart: TPhoMessart;
begin
NewMessart := TPhoMessart.Create;
try
NewMessart.Name := AName;
// Result := Factory.Rest.Add(NewMessart, True);
ORMAdd(NewMessart);
Commit;
finally
NewMessart.Free;
end;
end;
If the code above is executed I am getting an exception "TSQLDBFireDACConnection on MySQL/[databasename] should be connected" during the executing of Commit. The exception is raised in the following method:
procedure TSQLDBConnection.CheckConnection;
begin
if self=nil then
raise ESQLDBException.Create('TSQLDBConnection not created');
if not Connected then
raise ESQLDBException.CreateUTF8('% on %/% should be connected',
[self,Properties.ServerName,Properties.DataBaseName]);
end;
Question 1: What am I doing wrong? Because if I access the database through the Factory.Rest.Add(...) a MySQL connection is there and everything is fine.
Question 2: How do I get the ID of the new record after Commit?
Question 3: Is it a right way how I follow the DDD approach in my code?
Thank you for your help in advance!
Offline
1. How do you call the method? From which thread?
2. See fBatchResults[] array.
3. In pure DDD you should not need to worry about the IDs, but identify your aggregate via some real data from the model (logon, account number, social insurance number...). The ID is an implementation detail.
Offline
1. How do you call the method? From which thread?
I don't call this method directly if you mean CheckConnection, I call only the commit (see the code above). And by calling the method the exception is raised in the method CheckConnection
3. In pure DDD you should not need to worry about the IDs, but identify your aggregate via some real data from the model (logon, account number, social insurance number...). The ID is an implementation detail.
In most cases the real data is identified via some unique things like logon or account number, but sometimes the primary keys from the databases are used as a unique identifier. This is a case if there is no real identifier, but we need one and we have to create some artificial one. And I think it's ok to use a primary key. What do you think? What is your experience?
Offline