#1 2013-01-15 21:57:57

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Object in Interface Based Services

Hi, I'm new with your amazing framework.

How do I send Object from the server to the client in a Interface Based Services?

Offline

#2 2013-01-16 12:50:46

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

Re: Object in Interface Based Services

You can send any TPersistent or TSQLRecord content with interface based services.
See the documentation about that, and sample 14.

Offline

#3 2013-01-16 13:27:07

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Re: Object in Interface Based Services

Thank you for answering.

This is a class I'm using:

  TCostumer = class(TPersistent)
  private
    FName: string;
    published
    property Name : string read FName write FName;
  end; 

This is the interface:

  ICostumer = interface(IInvokable)
    ['{770D009F-15F4-4307-B2AD-BBAE42FE70C0}']
    function GetCostumer : TCostumer;
  end; 

When a try to run the server a get the error: 'ICostumer .GetCostumer: unexpected result type TCostumer'

I didn't found any example sending objects.

Last edited by arquivo59 (2013-01-16 14:18:19)

Offline

#4 2013-01-16 15:34:09

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

Re: Object in Interface Based Services

As stated by the documentation, you can not return a TObject as a function.
Who will be in charge of freeing the instance, in client-server mode?
There is no standard allocation scheme, in Delphi, for such parameters.

So every TObject parameter instance shall be managed by the caller, i.e. allocated before the call and released after it.
The method will just read or write the instance properties.

Offline

#5 2013-01-16 17:14:50

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Re: Object in Interface Based Services

OK, could you give a example of how I should do It?

Thanks!!

Offline

#6 2013-01-16 18:46:02

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

Re: Object in Interface Based Services

I've updated the documentation, to make it clearer.

Extracted from the latest version:

As stated above, mORMot does not allow a method function to return a class instance.

That is, you can't define such a method:

ICustomerFactory = interface(IInvokable)
    ['{770D009F-15F4-4307-B2AD-BBAE42FE70C0}']
    function NewCustomer: TCustomer;
  end;

Who will be in charge of freeing the instance, in client-server mode? There is no standard allocation scheme, in Delphi, for such parameters. So every TObject parameter instance shall be managed by the caller, i.e. allocated before the call and released after it. The method will just read or write the instance published properties, and serialize them as JSON.

That is, you can define such a method:

ICustomerFactory = interface(IInvokable)
    ['{770D009F-15F4-4307-B2AD-BBAE42FE70C0}']
    procedure NewCustomer(out aCustomer: TCustomer);
  end;

Note that here the out keyword is not indicated to mark how the memory his allocated, but to show the communication direction of the remote service, i.e. it will serialize the object at method return. Note that the caller shall instantiate an instance before call (whereas for "normal" Delphi code, it may be up to the method to instantiate the instance.

Then your client code can use it as such:

var Factory: ICustomerFactory;
    Repository: ICustomerRepository;
    Customer: TCustomer;
...
  Customer := TCustomer.Create;
  try
    Factory.NewCustomer(Customer); // get a new object instance
    Customer.FirstName := StringToUTF8(EditFirstName.Text);
    Customer.LastName := StringToUTF8(EditLastName.Text);
    NewCutomerID := Repository.Save(Customer); // persist the object
  finally
    Customer.Free; // properly manage memory
  end;

In this example, we use both Factory and Repository patterns, as proposed by the Domain Driven Design pattern.

Offline

#7 2013-01-16 19:55:46

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Re: Object in Interface Based Services

Very well, how can a send a bunch o TCostumers like 30 at once?
Can I send a TList of TCostumers?

Offline

#8 2013-01-16 22:54:54

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Re: Object in Interface Based Services

I see you use a class TInterfacedCollection.

I'm beginning with delphi, I read a lot o posts in this forum and in the documentation, but I still don't know how to use it in this situation.

Could give me a example?

Thanks!!

Offline

#9 2013-01-17 06:43:31

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

Re: Object in Interface Based Services

Just google for TCollection.
First link is http://www.atug.com/andypatterns/collections.htm and sounds pretty clear.

Then, to use TInterfacedCollection kind of parameter, see http://blog.synopse.info/post/2012/06/1 … ated-class

We are preparing a new way of adding a class option for TObjectList serialization, allowing direct serialization of such lists, instead of the more verbose TCollection pattern (see first link).

Offline

#10 2013-01-17 11:13:02

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Re: Object in Interface Based Services

Ok, thank you.

Offline

#11 2013-01-18 15:36:28

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

Re: Object in Interface Based Services

I've just added serialization and unserialization of TObjectList parameters.
See http://synopse.info/fossil/info/bf362a58d1

Should work easily for you.

Thanks to the new function ClassInstanceCreate(aClass: TClass): TObject, we may be able to handle returned class instance for functions.
Stay tuned!

Offline

#12 2013-01-18 16:35:34

arquivo59
Member
Registered: 2013-01-14
Posts: 24

Re: Object in Interface Based Services

Great!!!

There is a code:

 26063      oObjectList: begin // TList leaks memory, but TObjectList uses "ClassName":. 

Does It means that TObjectList don't leaks memory?

Offline

#13 2013-01-18 16:57:53

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

Re: Object in Interface Based Services

TList can leak memory if the objects are not explicitly freed at TList.Free.

A TObjectList created and populated from a JSON array of objects will let all its items safely destroyed at free.

See blog article http://blog.synopse.info/post/2013/01/1 … ialization

Offline

Board footer

Powered by FluxBB