DelegateDecompiler icon indicating copy to clipboard operation
DelegateDecompiler copied to clipboard

Nested decompilation

Open StijnClaessens opened this issue 5 months ago • 1 comments

I use DelegateDecompiler together with EF core. I map entities to models. I have model base classes and subclasses (simple example below). Each model contains a mapping method that will be used to project the entity on the model (I don't use AutoMapper). Those mapping methods are decorated with the [Decompile] attribute. In the subclass mapping method, I want to use the base class mapping method. The setup below compiles but gives a runtime error: expression must be writeable (Parameter 'left').

Is this way of working (nested [Decompile] methods) possible with the library 0.34.2?

public class EquipmentBaseModel
{
    public int EquipmentId { get; set; }
    public string? Name { get; set; }
    public short? EquipmentTypeId { get; set; }
    public string? Imonumber { get; set; }

    // MAPPINGS
    [Decompile]
    public static EquipmentBaseModel FromEntity(EquipmentEntity entity)
    {
        EquipmentBaseModel model = new EquipmentBaseModel();
        model.EquipmentId = entity.EquipmentId;
        model.Name = entity.Name;
        model.Imonumber = entity.Imonumber;
        model.EquipmentTypeId = entity.EquipmentTypeId;
        return model;
    }
}

public class EquipmentTestModel : EquipmentBaseModel
{
    public bool? Operational { get; set; }
    public string? ConstructionYear { get; set; }

    // MAPPINGS
    [Decompile]
    public new static EquipmentTestModel FromEntity(EquipmentEntity entity)
    {
        EquipmentTestModel model = (EquipmentTestModel)EquipmentBaseModel.FromEntity(entity);
        model.Operational = entity.Operational;
        model.ConstructionYear = entity.ConstructionYear;
        return model;
    }
}

public async Task<Result<IReadOnlyList<EquipmentModel>>> GetEquipmentAsync()
{
List<EquipmentTestModel> data = await dbContext.Equipment.AsNoTracking()
                                                         .Include(x => x.EquipmentType)
                                                         .Select(x => EquipmentTestModel.FromEntity(x))
                                                         .ToListAsync();
}

StijnClaessens avatar Sep 12 '25 14:09 StijnClaessens

Hi, my first guess would be that your method is decompiled as a block expression which (by experience) does not fare well with EF.

Instead I would try something more 'atomic' like this :

 [Decompile]
    public static EquipmentBaseModel FromEntity(EquipmentEntity entity)
    {
        return new EquipmentBaseModel() {
            EquipmentId = entity.EquipmentId,
            Name = entity.Name,
            Imonumber = entity.Imonumber,
            EquipmentTypeId = entity.EquipmentTypeId,
        };
    }

magicmoux avatar Oct 30 '25 11:10 magicmoux