JasonBock / InlineMapping

Using the Compiler API for object mapping
MIT License
63 stars 11 forks source link

Add a MappingNameAttribute #13

Closed JasonBock closed 2 years ago

JasonBock commented 3 years ago

If you have this:

[MapTo(typeof(Destination))]
public class Source
{
  public int Id { get; set; }
}

public class Destination
{
  public int Identifier { get; set; }
}

You could map those after InlineMapping is done, but maybe I provide this:

public class Destination
{
  [MappingName(nameof(Source.Id)]
  public int Identifier { get; set; }
}

If this attribute exists on a property, it will be used as the mapping name.

I'm not convinced this is a good idea - in fact, I think this may be a really bad idea. Adding even more metadata on the type definitions can get to be ugly. Also, if you don't own the type, I'd have to provide this:

[assembly: MappingName(typeof(Source), typeof(Destination), nameof(Source.Id), nameof(Destination.Identifier))]
JasonBock commented 3 years ago

Also, if I do this, I'm guessing I'd need MapFromName and MapToName to allow the definition to be done on either a source or destination property.

JasonBock commented 3 years ago

I'll do this, but need to do some clarification on the design.

MithrilMan commented 2 years ago

If you want to go that route, consider having a [MappingAlias("aliasName")] where you can apply multiple aliases

JasonBock commented 2 years ago

Upon further thought, I've decided to not do this, and #12 should allow for people to do whatever customizations they want during the mapping process.