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

[MapperIgnore] on properties of classes #585

Closed
mutzl opened this issue Jul 21, 2023 · 5 comments · Fixed by #1143
Closed

[MapperIgnore] on properties of classes #585

mutzl opened this issue Jul 21, 2023 · 5 comments · Fixed by #1143
Labels
enhancement New feature or request

Comments

@mutzl
Copy link

mutzl commented Jul 21, 2023

A common usecase is, that entities have properties that never need to be mapped to any DTO.
Some of these metadata properties might even be defined in a base class.
For example:

public abstract class BaseEntity
{
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; } = default!;
    public DateTime? UpdatedDate { get; set; }
    public string? UpdatedBy { get; set; }
}

public class MyEntity : BaseEntity 
{
    public string Foo { get; set; }
}

public class MyDto 
{
    public string Foo { get; set; }
}

So the mapper configuration is always pretty verbose:

    [MapperIgnoreTarget(nameof(BaseEntity.CreatedDate))]
    [MapperIgnoreTarget(nameof(BaseEntity.CreatedBy))]
    [MapperIgnoreTarget(nameof(BaseEntity.UpdatedDate))]
    [MapperIgnoreTarget(nameof(BaseEntity.UpdatedBy))]
    public static partial MyEntity ToEntity(this MyDto dto);

    [MapperIgnoreSource(nameof(BaseEntity.CreatedDate))]
    [MapperIgnoreSource(nameof(BaseEntity.CreatedBy))]
    [MapperIgnoreSource(nameof(BaseEntity.UpdatedDate))]
    [MapperIgnoreSource(nameof(BaseEntity.UpdatedBy))]
    public static partial MyDto ToDto(this MyEntity entity);

A MapperIgnoreAttribute - similar to the well known [JsonIgnore] - directly on the property would make code way cleaner.

public abstract class BaseEntity
{
    [MapperIgnore] public DateTime CreatedDate { get; set; }
    [MapperIgnore] public string CreatedBy { get; set; } = default!;
    [MapperIgnore] public DateTime? UpdatedDate { get; set; }
    [MapperIgnore] public string? UpdatedBy { get; set; }
}
@TimothyMakkison
Copy link
Collaborator

Would #513 and #201 help?

@mutzl
Copy link
Author

mutzl commented Jul 21, 2023

Would #513 and #201 help?

#201 would help, I suppose.

The [MapperIgnore] looks way more intuitive to me, though.
(at least for the use case I described above - for this kind of metadata, I know upfront, that it's never gonna be mapped).

BTW: Und vielen Dank für diese großartige Idee und Umsetzung von mapperly!

@TimothyMakkison
Copy link
Collaborator

Good point with 513. If #201 supports adding ignores then it will be perfect for you.

Failing that it would be pretty easy to modify the IgnoreObsolete code to support [MapperIgnore] 👍

@latonz
Copy link
Contributor

latonz commented Jul 26, 2023

MapperIgnoreAttribute is currently used to ignore target properties for backward compatibility (is marked as obsolete). I like the idea of using MapperIgnoreAttribute on properties to ignore them. However, this needs a breaking change in Mapperly or would need to allow it on properties without a constructor parameter...

The idea of #201 is to allow MapperAttribute configurations on an assembly level which wouldn't work for the use case mentioned here.

@latonz latonz added the enhancement New feature or request label Jul 26, 2023
@latonz
Copy link
Contributor

latonz commented Aug 2, 2023

#611 removes the MapperIgnoreAttribute from the API, it may in a later release be reused to ignore properties/fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants