mdalowcode

Does ObjectReef supports Data Transfer Objects (DTO)?


I'm wondering how to create something like a Data Transfer Object in ObjectReef. When I call an operation from the API I can use parameters that are either simple types or references to existing objects.

context App::foo(s: string, n: integer, d: date)
post:
   -- some stuff here

Is there a method for a more concise way of passing arguments to operation via API?


Solution

  • You can use a JsonObject object as a DTO and just read its properties like of any other object.

    context App::foo(argsDTO: JsonObject)
    post:
        self.Name = argsDTO.name
        self.Age = argsDTO.age
    

    and from client application like ie. js app just pass a Json as argument

    await fetch('http://localhost:1996/json/v001/app/foo',
            {
                method:'POST',
                headers: {
                    'Content-Type':'application/json',
                    'Accept':'application/json'},
                body: JSON.stringify(
                    {
                        argsDTO:{
                            name: 'Alice',
                            age: 24
                        }
                    })
            });