Table of Contents

Create a data object

What are data objects ?

Data objects are custom C# objects which can be used both on the server side and on the client side.

A concrete example is when you want to use on the client side the result of a server method.
If the result of the method is a standard DotNet type, you don't need to create a data object. But if you want to use a more advanced object, you can define a custom object using data objects.

How to create a data object

To create a data object, you can click in the menu on the + button of the Data objects item under one of these folders of a module:

  • Shared folder for data objects used in both backend and frontend
  • Backend folder for data objects used only in backend code
  • Frontend folder for data objects used only in frontend code

The folder where you create the data object will determine its default scope, but you can still modify the scope later if needed.

Name

This is the name of the data object, it will be used to generate the corresponding C# class in the Application.Abstraction.DataObjects namespace.
The name of the class will be the same name as the data object.

Description

This optional parameter is the short description of what the data object will be used for. It will be displayed by the code editor intellisense.

Module

This is the module to which the data object will be associated.

Properties

Like a C# class, a data object contains properties.

Name

This is the name of the property, which can be accessed by code (MyDataObject.MyProperty).

Description

This optional parameter is the short description of what the data object property will be used for. It will be displayed by the code editor intellisense.

DotNet data type

This is the C# type used for the data object property.

Default value

This optional parameter define the default value of the data object property.

Module

This is the module to which the data object parameter will be associated.

Scope

By default, a data object is shared between the backend and frontend. However, if you want to reduce the scope of use, you can specify a scope. This scope optimizes generation to generate only the code corresponding to the scope (frontend / backend). In Neos Studio, on existing data object, use the action Change scope to specify the new scope.

  • Change from shared to frontend If the data object was used in backend, there will be generation or compilation errors.

  • Change from shared to backend If the data object was used in frontend, there may be no errors during generation/compilation. Check whether the data object is used in the frontend by searching for its name in the metadata (yaml files). Today, there's no other way. The risk is that errors may occur at runtime if the data object does not exist on the front end, even though it is being used.

How to use data objects

Data objects are translated into C# classes, you can use these classes in both client side and server side code of the cluster.

They can be accessed under the namespace Application.Abstractions.DataObjects

For the following examples, consider a data object named CopyOrderResult. It has a NewOrderId property of type int.

Server side example (server method CopyOrder) :

// ...
// some asynchronous code to create a new order and get the new OrderId in the `id` variable...
// ...
    return new CopyOrderResult() { OrderId = id };
// ...

In this example, the method will return a new CopyOrderResult data object with its OrderId property having the value of the id variable.

Client side example (UI view action CopyOrder) :

// ...
CopyOrderResult result = await ServerMethods.CopyOrder.ExecuteAsync(Item.OrderId);
// ...

In this example, we call the server method CopyOrder and the result contains the data object CopyOrderResult.
It can then be used in the UI view code.