Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private Sets #385

Closed
luylucas10 opened this issue Nov 3, 2021 · 1 comment
Closed

Private Sets #385

luylucas10 opened this issue Nov 3, 2021 · 1 comment

Comments

@luylucas10
Copy link

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?

@lipharDYT
Copy link

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

andrerav added a commit that referenced this issue Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants