Creating Objects in Scripts

Creating objects in scripts is a frequent operation, and you need to understand its specifics well.  

There are two ways to create an object in a script:

  1. Using special initialization methods
  2. Directly through an object constructor

Let’s take a look at each option and clarify how objects should be created in scripts.

Using special initialization methods

Below you can see an example of using such methods:

public void CreateNewObects (Context context)
    //Using InterfaceActivator class
    var newContractor = EleWise.ELMA.Model.Services.InterfaceActivator.Create<ContractorLegal>();
    newContractor.Name = "Contractor 1";
    // Using the Create manager method 
    var newContractor2 = EntityManager<ContractorLegal>.Create();
    newContractor2.Name = " Contractor 2";
    // Using the Create manager method
    var newContractor3 = EntityManager<ContractorLegal>.Instance.Create();
    newContractor3.Name = " Contractor 3";
    //Using the InstanceOf syntax assistant class
    var newContragent4 = new EleWise.ELMA.Services.InstanceOf<ContractorLegal>()
        New = {
            Name = " Contractor 4"

Let’s take a closer look at this methods.

Using InterfaceActivator class

When you create a new module and need to work with objects, you can only work with their interfaces (there are no classes yet, they will be compiled later when you start the ELMA server). You can do that by using InterfaceActivator – a base class for working with object interfaces.

When you call the Create method, the object class (ContractorLegal in our case) is created at runtime and saved in the application’s memory.

Using Create() methods of entity managers

The next two objects in this example are created with the Create method from the entity manager:

// Using the Create() method of base entity manager 
    var newContractor2 = EntityManager<ContractorLegal>.Create();
    newContractor2.Name = " Contractor 2";
    // Using the Create() method of entity manager
    var newContractor3 = EntityManager<ContractorLegal>.Instance.Create();
    newContractor3.Name = " Contractor 3";

The difference between these two methods is the following: to create Contractor2, the Create() method of the base class (EntityManager<T>) is used. This method cannot be overridden.

However, to create Contractor3 you use the method of the manager for a specific entity (in this case – the manager for ContractorLegal). If necessary, we can override the methods of this manager. To learn how to do that, see Data model structure and entity managers article.

Using InstanceOf Class

This method is rather useful for module developers. It recalls the Create method from the InterfaceActivator class. This class makes it easier for developers to write code in certain places. 

Using Object Constructor

This method is traditionally used to create objects in the .NET platform.

public void CreateObjects(Context context)
    var newContractor = new ContractorLegal();
    newContractor.Name = "New Contractor";

Note, that you can create a new object of any class only if this class exists. Thus, you can only use the new modifier after the classes are compiled (after the ELMA server is started). E.g. when creating scripts in ELMA Designer.

What is the difference between these 2 ways?

In both cases, you create new objects in a script. After you call the Create() method, the object will be saved in the memory. To save it to the database, you need to call the Save() method:




In both cases, the Save() method of an entity manager will be called.

 Here is what happens when you call Save() method for a new object:

  1. The object is inserted in the table of the database to get new ID;
  2. This ID is assigned to the Id property of the object;
  3. If a script is executed without exceptions, the database transaction is committed.

Now you can see the difference between the 2 ways of creating new objects. E.g. you have an Order object with the No. property. Suppose, that you added a unique constraint (unique constraints in MS SQL) for this property to the database. Now you need to add additional checks to the Create() method of the entity manager to ensure that this property will have a unique value for each new object (you will get a database exception otherwise).

Now if you create a new Order using the new modifier, the system will not check if the No. property is unique. Thus, you will get the database exception when trying to save the object.


Always use the Create manager method

To avoid the above-mentioned issue, we recommend that you always create new objects using Create() methods of entity managers.