AutoPopulate script able to set property that's not a reference type?

I’m enjoying the simplicity of autoCRUD and wondering if there is a way to use the AutoPopulate attribute to set a property from two properties in the request DTO.

Specifically, the request DTO includes Latitude and Longitude properties, but the db insert is using a custom converter to the postgres GIS type geometry. So my entity DTO is using the .net Point type from the NTS extensions for GIS.

What would be nice is to be able to use the AutoPopulate to do a new Point(new Position(Latitude,Longitude)) to set the property.

I know it’s a long shot.

I’ve currently implemented a custom service to handle it but you know…

1 Like

You can access the Request DTO in your AutoPopulate #Script Expresssion with Request.Dto.

#Script has a lot of .NET integration capabilities in Scripting .NET which shows how to create instances and call constructors however I’d recommend instead adding your own Script Methods in your AppHost.Configure(), e.g:

public class MyScriptMethods : ScriptMethods
{
    public Point toPosition(IRequest req) => ...;
}

//...
ScriptContext.ScriptMethods.Add(new MyScriptMethods());

Then have your AutoPopulate expression call your C# method instead passing in Request for the IRequest or Request.Dto for the Request DTO.

[AutoPopulate(nameof(MyDto.MyProp),  Eval = "toPosition(Request)")]

Again, thanks. The documentation is usually great if I can find the right spot. Now that I see the pieces here it makes much more sense. :+1:

1 Like

Worked perfectly! For interest

public class GisScriptMethods: ScriptMethods
{
    public Point? toPosition(IRequest request)
    {
        if (request.Dto is not IHaveGisRequestModel r) return null;
    
        if (r.Latitude.HasValue && r.Longitude.HasValue)
            return new Point(new Position(r.Latitude.Value, r.Longitude.Value, r.Altitude));

        return null;
    }    
}

and

[AutoPopulate(nameof(IHaveGisDatabaseModel.GisLocation), Eval = "toPosition(Request)")]
1 Like