#1 2016-04-05 10:23:26

willo
Member
From: Cape Town, South Africa
Registered: 2014-11-15
Posts: 67
Website

DTO implementing interfaces

Hi AB,

We're sitting with a situation where we'd like out DTO's to implement certain interfaces. I realize that we should not inherited from TSynPersistent, in these cases, but where do you suggest we inherit from then?

Offline

#2 2016-04-05 12:22:03

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

Re: DTO implementing interfaces

DTOs are value objects.
They are transmitted by representation.
So interfaces variable are not suitable for such parameters.

But if you use interface parameters as callbacks, you can execute the interface implementation on the server side from the fake client interface generated by mORMot's SOA.
See http://synopse.info/files/html/Synopse% … l#TITL_149

Offline

#3 2016-04-05 12:30:32

willo
Member
From: Cape Town, South Africa
Registered: 2014-11-15
Posts: 67
Website

Re: DTO implementing interfaces

Hi AB,

Sure - but we have a function to populate a drop down, for instance. It would be great to pass the array of DTO's directly to it and by using an interface it could extract the correct description to use.

Offline

#4 2016-04-05 12:38:05

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

Re: DTO implementing interfaces

You may use an interface-base callback here.
But it would be IMHO over-complicated in such cases.

Just return the DTOs, e.g. the TStringList as expected by the UI, then let the client code update the UI with the returned values.
Otherwise, you may start to couple your service with the UI too much...

Offline

#5 2016-04-05 13:00:19

willo
Member
From: Cape Town, South Africa
Registered: 2014-11-15
Posts: 67
Website

Re: DTO implementing interfaces

Let me explain it like this:

type
  IDescribable = interface
     function getID : integer;
     function getDescription : RawUTF8;
  end;

  TPerson = class(TSynPersistent, IDescribable)
  private
     fID : integer;
     LastName: RawUTF8;
     FirstName: RawUTF8;
  protected
     function getID : integer;
     function getDescription : RawUTF8;
  published
     property ID : integer read fID write fID;
     property FirstName : RawUTF8 read fFirstName write fFirstName;
     property LastName : RawUTF8 read fLastName write fLastName;
  end;

Then on my client side I can have a library function:

procedure populateList( aStrings :  TStrings; aData : array of IDescribable )
var I : integer;
begin
  for I := low( aData ) to high(aData) do begin
    aStrings.AddObject( aData[i].getDescription, aData[i] );
  end;
end;

and call it like this:

var
  UserNames : TPersonDynArray;
begin
  SomeServiceINterface.GetLookupValues( aSomeLookups );
  populateList( cbUserDropDown.Items, aSomeLookups );
..

Please note - this is just from the top of my head to trying and explain what I'm trying to do.

Offline

#6 2016-04-05 14:38:55

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

Re: DTO implementing interfaces

An array of IDescribable won't work.
But a single IDescribable (if defined as IInvokable with a GUID) could be passed from the client side to the server side.

Honestly, I do not see any benefit of such a pattern.
Just define a getList method returning a TStringList, which would be applied on the client side to whatever "array of IDescribable" interfaces you expect.
It would avoid a lot of back/forth communication between the server and the client, and make your SOA system much more stable.

Offline

Board footer

Powered by FluxBB