mdriven

Derived attribute with AsTajson call in the custom service


I use .net5 and .netStandard from nuget. The structure of the solution is simple - class library with ecomodel and webapi project. I've registers custom service in the EcoSpace1.cs as it was advised by eco gurus long, long time ago.

public interface IMyService
{
    int Class1Count();
}

public class MyServiceClass : IMyService
{
    private IEcoServiceProvider ServiceProvider { get; set; }

    public MyServiceClass(IEcoServiceProvider serviceProvider)
    {
        ServiceProvider = serviceProvider;
    }
    public int Class1Count()
    {
        var v = ServiceProvider.GetEcoService<IOclService>().Evaluate("Class1.allInstances->size");
        return (int)v.AsObject;
    }
}

The rule for a webapi project's controller- inherit from the Controller class - due to the Swashbuckle.AspNetCore (swagger) usage. Swagger shows errors if I inherit a controller from the ModelDrivenControllerBase

But the code above is OK, the GET works perfect:

public class MySecondEcoServiceController : Controller
{
    [HttpGet]
    public int Get()
    {

        using (EcoSpace1 es=new EcoSpace1())
        {
            es.Active = true;
            int r = es.MyService.Class1Count();
            return r;
        }
    }

}

It works till I tried to add another method into the IMyService and to get json. I tried AsTajson by adding derived attribute to the Class1 with DerivationOcl.

Class1.allInstances->first.AsTaJson( 'SampleViewModel',  false )

or

self.AsTaJson( 'SampleViewModel',  false )

In the MyServiceClass the implementation of the Get is:

Class1 v = (Class1)ServiceProvider.GetEcoService<IOclService>().Evaluate("Class1.allInstances->first").AsObject;
json = v.Attribute2;

If I tried GET this value - exception:

Eco.FrameworkImpl.Ocl.EBoldOCLError: 'bughuntinfo internaleval:84  es seems fine.Object reference not set to an instance of an object.'

What is the proper way (or EcoService?) to get values returned by TaJson?


Solution

  • Not sure but please check that you have initiated use of ViewModels once for your EcoSpace type:

    ViewModelDefinitionsInApplication.Init(_es);