#1 2020-05-10 13:04:31

larand54
Member
Registered: 2018-12-25
Posts: 104

ORMAdd does not store the linked classes

Having problem finding out how I should handle INSERT of objects that contains linked classes.
ORMAdd does not seems to handle that.
I came up with an own solution but don't really like it.

The main TSQLRecord looks like this:

     TSQLRecordCustomer = class(TSQLRecord)
  protected
    fContact: TSQLRecordPersonContactable;    <<<--- The linked class
  published
    property Contact: TSQLRecordPersonContactable read fContact write fContact;

   
The repositorys add-function:   

function TCustomerRepository.Add(const aAggregate: TCustomer): TCQRSResult;
var
  rest: TSQLRest;
  SQLpc: TSQLRecordPersonContactable;
  pc: TPersonContactable;
  SQLcus: TSQLRecordCustomer;
begin
  result := cqrsInternalError;
  rest := GetRest(self);
  SQLpc := TSQLRecordPersonContactable.Create;
  SQLcus := TSQLRecordCustomer.Create;
  CustomerToRecord(aAggregate,SQLcus,SQLpc); <<<---- Copies all fields from TCustomer into SQLcus and SQLpc
  SQLcus.Contact := TSQLRecordPersonContactable(rest.add(SQLpc, true));
  rest.Add(SQLcus, true);
  result := cqrsSuccess;

This works but I don't know how to handle different errors here and the "CustomerToRecord" function is heavy to type.

What I want is that this should be enough.

  Result := ORMAdd(aAggregate);

But this leaves the "Contact"-field equal to zero.

I'm afraid that it isn't possible using ORM this way but hopes for a simpler and better solution than mine.

Last edited by larand54 (2020-05-11 06:14:45)


Delphi-11, WIN10

Offline

#2 2020-05-10 13:12:57

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

Re: ORMAdd does not store the linked classes

You are making your code from the wrong way round.
Confusing TSQLRecord and the ORM aggregate class.

The DDD ORM won't start from a TSQLRecord, but from an aggregate class. Then it would create nested classes into TSQLRecord fields.
For instance, with a nested Name: TPersonName property in TPerson, TPerson.Name.First will be persisted as TSQLPerson.Name_First field - with no nested TSQLRecord.
It will flatten the TPerson class into a TSQLPerson, and create a SQL column for each TPerson nested field. On MongoDB, it will just use a nested object.
Please read the doc and samples again.

Offline

#3 2020-05-10 17:08:50

larand54
Member
Registered: 2018-12-25
Posts: 104

Re: ORMAdd does not store the linked classes

Ok, from the beginning I did just that but I did not want a complete flat table, I want to split them in One Customer table and one PersonContactable table.
In this simple example it isn't needeed but I want to know how to behave when I really need to split.
In one case I can think of is when you have several contacts for the customer. They are specified by what "role" that contact have. Different customers has different roles One may have only one another has 3 and others may have 6 etc. In that case it feels inconvenient to have a flat table for all this even if its possible.
One contact can possess several roles, the customer employs a new person to take over another contacts role etc.


21_customer-role-contact.png

Last edited by larand54 (2020-05-11 10:32:02)


Delphi-11, WIN10

Offline

#4 2020-05-11 15:15:22

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

Re: ORMAdd does not store the linked classes

I am not sure you got the notion of "Aggregate" as defined by DDD.
You are still reasoning in terms of relational databases.

Offline

Board footer

Powered by FluxBB