Convert-DbValue to enum using projections

Is there a way to convert projected enum-values to its enumeration?

I've found TMappingExplorer.DbValueToEnum, that would do this for me, but it's private...

this is the find I'm using:

[Enumeration(TEnumMappingType.emString, 'one,two')]
TEntityEnum = (teOne, teTwo);

TEntity = class
private
  FEnum: TEntityEnum;
public
  property Enum: TEntityEnum read FEnum write FEnum;
end;

// shortened Find:
hResult := ObjectManager.Find<TEntity>.
  Select(TProjections.ProjectionList.Add(EntDic.Entity.Enum)).
  Take(1).UniqueValue;

 hResult.Values[0] = 'one' //or two, but I'd like to use teOne or teTwo...

It is private indeed, but you can workaround it using a hack:

type
  TInternalConverter = class(TLegacyDBValueConverter)
  public
    procedure DBToValue(const DBValue: Variant; var Value: TValue); reintroduce;
  end;

{ TInternalConverter }

procedure TInternalConverter.DBToValue(const DBValue: Variant; var Value: TValue);
begin
  inherited DBToValue(DBValue, Value);
end;

{ some code}

procedure TForm2.Button1Click(Sender: TObject);
var
  Conv: TLegacyDBValueConverter;
  Value: TValue;
  Enum: TEntityEnum;
begin
  Conv := Manager.Explorer.FindEntityTypeFromClass(TEntity).AllColumns.Find('Enum').Wrapper as TLegacyDBValueConverter;
  TInternalConverter(Conv).DBToValue('two', Value);
  Enum := Value.AsType<TEntityEnum>;
end;

This hack works, but could you just make TMappingExplorer.DbValueToEnum public?
Would be easier ;)

1 Like

For now we are rather avoiding publishing too many methods in the explorer. We have a major refactor in sight.

Ok, maybe that's why there is no update since february?

No, there is no relation.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.