You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't know if it's a feature or a bug, but I have an issue that I don't understand why it's happening.
I've an Entity with public getters, private setters and a constructor with parameters:
public class Cargo : Entidade
{
//[...] removed for simplification
public Cargo(int id, string nome, int? cargoChefeId = null) : base(id)
{
_subordinados = new List<Cargo>();
ValidarNome(nome);
ValidarCargoChefe(cargoChefeId);
Nome = nome.Trim().ToUpper();
CargoChefeId = cargoChefeId;
}
public int? CargoChefeId { get; private set; }
public string Nome { get; private set; }
public virtual Cargo CargoChefe { get; private set; }
//[...] removed for simplification
}
And my View Model:
public class CargoModelo
{
public int Id { get; set; }
[Display(Name = "Cargo Chefe")]
public int? CargoChefeId { get; set; }
[Required]
[MinLength(5)]
[MaxLength(100)]
public string Nome { get; set; }
public CargoModelo CargoChefe { get; set; }
}
It works, but, I don't know why, after mapping to constructor, It uses entity private setters, losing the validation and data manipulation done in the ctor.
I am encountering the same issue. My application has an always-valid domain model and I rely heavily on encapsulation, constructors, and methods. I do not expose setters (nor do I intend to), so it is an even worse situation than the above because the generated code does not compile.
My entity looks like this (I have removed members irrelevant to this issue):
public Reference(ReferenceName name, string description, ReferenceSettings referenceSettings)
{
Settings = Guard.Against.Null(referenceSettings, nameof(referenceSettings));
Name = Guard.Against.Default(name, nameof(name));
Description = Guard.Against.InvalidReferenceDescription(description, nameof(description));
}
public ReferenceSettings Settings { get; private set; } = null!;
public ReferenceName Name { get; private set; }
public string Description { get; private set; } = null!;
Input DTO I want to map from (note: this is a custom DTO, not an autogenerated one):
public class ReferenceInput
{
public string Name { get; init; } = null!;
public string Description { get; init; } = null!;
public ReferenceSettingsInput ReferenceSettings { get; init; } = null!;
}
The interface I am using for mapper generation:
[Mapper]
public interface IReferenceMapper
{
Reference MapFromInput(ReferenceInput input);
}
The code generated by Mapster.Tool
public partial class ReferenceMapper : IReferenceMapper
{
public Reference MapFromInput(ReferenceInput p1)
{
return p1 == null ? null : new Reference(p1.Name == null ? default(ReferenceName) : (ReferenceName)p1.Name, p1.Description, funcMain1(p1.ReferenceSettings))
{
Name = p1.Name == null ? default(ReferenceName) : (ReferenceName)p1.Name, //<---- THIS LINE SHOULDN'T EXIST
Description = p1.Description //<---- THIS LINE SHOULDN'T EXIST
};
}
private ReferenceSettings funcMain1(ReferenceSettingsInput p2) {...}
[...]
}
The configuration:
public class ReferenceRegister : IRegister
{
public void Register(TypeAdapterConfig config)
{
TypeAdapterConfig.GlobalSettings.Default.EnableNonPublicMembers(false);
config.NewConfig<ReferenceInput, Reference>()
.MapToConstructor(true);
}
}
Activity
lipharDYT commentedon Nov 26, 2021
I am encountering the same issue. My application has an always-valid domain model and I rely heavily on encapsulation, constructors, and methods. I do not expose setters (nor do I intend to), so it is an even worse situation than the above because the generated code does not compile.
My entity looks like this (I have removed members irrelevant to this issue):
Input DTO I want to map from (note: this is a custom DTO, not an autogenerated one):
The interface I am using for mapper generation:
The code generated by Mapster.Tool
The configuration:
fixes MapsterMapper#385
Merge pull request #449 from JMPSequeira/Issue#385