Table of Contents

What is server code and when to use it?

Server code is a term referencing all the elements defined in Neos Studio that include C# business code that will be executed on the backend:

  • Entity validation rule
  • Entity event rule
  • Entity view validation rule
  • Entity view event rule
  • Server method

When you want your code to be executed at key moments in the framework workflow (entry value validation, before saving, when data is loaded, ...) then you should consider using validation or event rules. For other use cases, you might want to use the more flexible server methods that can do pretty much all you need them to do and be called from the application via a button or even from another application via an API.

How to define server code?

Server code can be defined in multiple views of Neos Studio, depending on the type of element the code will be attached to. The presentation should be similar except for some specific properties.

When creating server code, a popup will appear. It will vary depending on the type of element the code is linked to but some properties are still common :

  • Name : name of the element, used to name the corresponding C# class that will contain the business code
  • Business assembly : business assembly that will contain the C# class representing the element. The chosen assembly must belong to the element module or one of its children.
  • Show Application layer assemblies [only for entity validation rule and entity event rule] : the source element of your code is in the "Domain" layer. This layer is the core of your business and we recommend placing all the code associated with the elements of this layer there. Placing your code in an "Application" layer assembly is only justified if your code uses elements from this layer (entity views, data objects, server methods, notifications...). See more about domain and application layers.
  • Asynchronous execution [only for validation rules] : this option must be checked when you execute asynchronous code. Otherwise, unchecking this option makes the code more readable by preventing the method from returning a "Task" object. If necessary, the signature of the generated method can always be changed afterwards.

Once those information have been entered and validated, you can either write the C# code directly in the Neos Studio code editor or in Visual Studio (after the element has been saved once). After that, the Open the file in visual studio button will be available. The code is automatically synchronized between Visual Studio and the Neos Studio code editor.

Note that a button allows you to display the code editor window in full screen .

What are dependencies and how to define them?

Dependencies are the services injected into the constructor of the C# class. They will allow you to access repositories of entities or entity views, to manipulate the unit of work, ... You can also define and use your own services. You can display and edit them by clicking on this button . An assistant guides you when adding a new dependency. Neos Studio automatically synchronizes dependencies with the generated C# class, you can add, modify or delete them either in this view or directly in the constructor of the C# file in Visual Studio.

How to test your code?

A unit test will automatically be generated when creating a new element. You can display it with the dedicated button . However, the default implementation is just an example and must be replaced by a real test. We invite you to consult the articles covering tests to discover how to implement them.

Specific properties

Depending on the type of element you are editing, there are a number of specific properties. They are accessible either via this button or via this button . Here are the articles detailing them :

How to?