You can fix the Not available for Offline object error in CodeDress or framework code by creating each model object with the service provider from an object that already belongs to the correct Eco space.
What the error means
An object of a model class must belong to an Eco space. The Eco space provides the services that MDriven uses to manage the object. If your code creates a model object without supplying that context, the object is not available as an offline object and this error is raised.
This commonly happens when you use a parameterless constructor in custom C# code:
Class1 newObject = new Class1();
Create the object with an IEcoServiceProvider from an existing model object instead.
Create the object in the current Eco space
Follow these steps where the error occurs:
- Find the statement that creates the model object, such as
new Class1(). - Identify an existing model object in the same operation. In an instance method on a model class, that object is normally
this. - Pass that object's service provider to the constructor.
- Run the operation again and confirm that the new object can participate in the same Eco space as the object that created it.
For example, in code on Class1:
Class1 newObject = new Class1(this.AsIObject().ServiceProvider);
this.AsIObject().ServiceProvider is the important part of this expression. All model-class objects expose AsIObject(), and the resulting IObject provides the IEcoServiceProvider through its ServiceProvider property.
Use the ServiceProvider shortcut when available
The Eco.ObjectRepresentation namespace provides a this.ServiceProvider() extension-method shortcut. When that namespace is available, the same creation can be written as:
Class1 newObject = new Class1(this.ServiceProvider());
Both examples obtain the service provider from this. Use the explicit AsIObject().ServiceProvider form when you want to make the source of the Eco-space context clear, or use ServiceProvider() when the extension method is in scope.
Choose the correct source object
Use a source object that is already part of the Eco space in which the new object belongs. For example, when an operation on an existing Order creates a Line, obtain the service provider from that Order:
Line newLine = new Line(this.AsIObject().ServiceProvider);
Do not create the new model object with a parameterless constructor when the object must be managed in the current Eco space.
Related guidance
This error concerns object creation in an Eco space. It is different from an object that has been marked for deletion but remains accessible until save; see Documentation:Object is already deleted. For the general OCL meaning of objects, see Documentation:Objects.
