Skip to content

Private Sets #385

Closed
Closed

Description

@luylucas10

Hi.

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; }
}

I wrote this configuration:

     TypeAdapterConfig<CargoModelo, Cargo>.NewConfig().MapToConstructor(true);

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.

How can I change this behavior?

Activity

  1. lipharDYT commented on Nov 26, 2021

    @lipharDYT

    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);
        }
    }
    
  2. added a commit that references this issue on Jun 24, 2022
    b0a4648
  3. added a commit that references this issue on Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions