🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Constructor in generated code
This page was created by Hans.karlsen on 2021-10-14. Last edited by Wikiadmin on 2026-07-29.

You can define constructors for generated C# model classes, or use a static EAL factory operation when your object-creation logic must stay in the model.

Choose an object-creation pattern

Use the pattern that matches where you implement the creation logic.

Situation Use Example
You implement creation logic in C# for generated model classes. Model a constructor operation and implement it in the generated class's handwritten partial-code area. Create Class1 with a required Class2 argument.
You implement creation logic in OCL / EAL (Executable Action Language). Model a static operation that creates and returns the object. Call Class1.SimulatedConstructor('a', 'b').
You need initialization that happens after a new object is created. Use OnCreate. Set a creation time or generate an identifier.

Create a constructor for generated C#

A modeled constructor is an operation whose OperationKind is set to constructor. MDriven generates the constructor signature from the operation and its parameters.

  1. Add a normal operation to the class in MDriven Designer.
  2. Add the parameters that the caller must supply. For example, add a parameter named aClass2 of type Class2.
  3. In the operation's properties, change OperationKind to constructor.
  4. Run code generation and build the project.

For a constructor on Class1 that accepts Class2 aClass2, the generated signature is of this form:

public partial class Class1
{
  [UmlElement(Id = "c8111515-b7a5-4b9b-87f9-4a19261c8bd8")]
  public Class1(Class2 aClass2)
  {
  }
}

The UmlElement attribute connects the generated member to the modeled operation. Keep the model and generated code synchronized; see Code generation for the required generation and build workflow.

Supply the service provider for live objects

When you construct a live model object in C#, pass an IEcoServiceProvider and chain to the main constructor supplied by MDriven. This associates the new object with its Eco space.

public Class1(IEcoServiceProvider serviceProvider, Class2 aClass2) : this(serviceProvider)
{
}

For example, when creation occurs from another model object, obtain the provider from that object's AsIObject() representation:

Class1 newObject = new Class1(this.AsIObject().ServiceProvider, aClass2);

Creating an object without its Eco-space service provider can result in the Not available for Offline object error. See Not available for Offline object for the required provider access patterns.

Create objects with EAL

You cannot implement a constructor in an operation's Body property when the implementation uses EAL. Instead, model a static operation that returns the class being created. This is a factory operation: callers provide values, the operation creates the object, initializes it, and returns it.

Model the factory operation

  1. Add an operation to Class1.
  2. Set OperationKind to Static.
  3. Give the operation input parameters and set its return type to Class1.
  4. Enter the EAL expression in the operation's Body property.

For example, define the operation as:

SimulatedConstructor(input1:String; input2:String):Class1

Use this EAL body to create a Class1, assign both attributes, and return the created object:

let ret=Class1.Create in(
ret.Attribute1:=input1;
ret.Attribute2:=input2;
ret )

Call the static operation as follows:

Class1.SimulatedConstructor('a','b')

This pattern is useful when the caller must provide parameters and you want to initialize multiple attributes in one modeled operation. The operation is static and returns Class1; it does not generate a C# constructor.

Use OnCreate for creation-time initialization

OnCreate runs after a new object has been created and is the OCL counterpart to constructor-style initialization. Use it for values that do not need to come from the caller.

For example, an OnCreate body can set a timestamp and generate a GUID:

CreateTime:=DateTime.Now;
self.Guid.newGuid()

If a subclass has its own OnCreate, call self.base.OnCreate when the superclass initialization must also run. Use a static factory operation instead when initialization depends on explicit input parameters.

Troubleshooting

If code generation fails, or generated code does not match the model, follow Codegen failed and Code generation.

If you receive No suitable constructor found, check assembly-version and CodeDress assembly issues described in Fix 'No suitable constructor found' Error.

See also