Creating Objects in Scripts

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

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

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

Let’s review 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"
        }
    }.New;
 }

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 ELMA server). You can do that using InterfaceActivator – a base class for working with object interfaces.

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

Using Create() methods of entity managers

The next two objects in our 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: to create Contractor2 we use the Create() method of the base class (EntityManager<T>). This method cannot be overridden.

However, to create Contractor3 we use the method of manager for a specific entity (in our 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 handy 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 .NET platform.

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

Note, that we can create a new object of any class only if this class exists. Thus, you can only use 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, we create new objects in the script. After we call Create() method, the object will be saved in memory. To save it to database, you need to call Save() method:

newContractor2.Save();

 or

EntityManager<ContractorLegal>.Instance.Save(newContractor2);

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

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

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

Now we can see the difference between 2 ways of creating new objects. E.g. we have Order object with No. property. Suppose, that we added a unique constraint (unique constraints in MS SQL) for this property to the database. Now we 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 (we will get the 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.

Attention

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.