How to map one-to-one association

What is the correct way to map an one-to-one association?

For example: association where each Person can have 0 or 1 Address associated and an each Address must be associated with only one Person.

Classes:

TAddress = class
strict private
  FCity: String;
  FStreetAddress: String;
  FZipCode: String;
public
  property StreetAddress: String read FStreetAddress write FStreetAddress;
  property City: String read FCity write FCity;
  property: ZipCode: String FZipCode write FZipCode;
end;

TPerson = class
strict private
  FId: Integer;
  FName: String;
  FAddress: TAddress;
public
  property Id: Integer read FId write FId; 
  property Name: String read FName write FName;
  property Address: TAddress read FAddress write FAddress;
end;


Regards

You should always map a one-to-many, and then you can add wrappers to make your object behave as 1-1 at OOP-level:



TPerson = class
private
  [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll)]
  FAddresses: Proxy<TList<TAddress>>;
public
  property Address: TAddress read GetAddress write SetAddress;
end;


function TPerson.GetAddress: TAddress;
begin
  if FAddresses.Value.Count > 0 then
    Result := FAddresses.Value[0]
  else
    Result := nil;
end;


procedure TPerson.SetAddress(Value: TAddress);
begin
  if FAddresses.Value.Count = 0 then
    FAddresses.Add(Value)
  else
    FAddresses[0] := Value;
end;

  

Wagner R. Landgraf2019-01-28 12:18:17