#1 2015-06-18 19:19:37

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

DDD ORMAdd, Commit and how to retrieve the inserted ID

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

#2 2015-06-18 21:35:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,230
Website

Re: DDD ORMAdd, Commit and how to retrieve the inserted ID

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

#3 2015-06-19 04:27:09

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: DDD ORMAdd, Commit and how to retrieve the inserted ID

ab wrote:

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

ab wrote:

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

Board footer

Powered by FluxBB