У меня есть следующие классы:
public class FooEntity {
public Guid Member1 { get; set; }
public Guid Member2 { get; set; }
}
public class FooDomain {
public PhotoId Member1 { get; set; }
public ExifId Member2 { get; set; }
}
public sealed class PhotoId : RequiredGuid
{
private const string Name = "PhotoId";
public PhotoId(Guid guid) : base(Name, guid)
{
}
public static implicit operator PhotoId(Guid guid)
{
return new PhotoId(guid);
}
}
public sealed class ExifId : RequiredGuid
{
private const string Name = "ExifId";
public ExifId(Guid guid) : base(Name, guid)
{
}
public static implicit operator ExifId(Guid guid)
{
return new ExifId(guid);
}
}
public class RequiredGuid {
private string _name;
private string _guid;
public RequiredGuid(string name, Guid guid)
{
if (guid == Guid.Empty)
throw new GuidShouldNotBeEmptyException($"The '{name}' field is required");
_name = name;
_guid = guid;
}
public static implicit operator Guid(RequiredGuid field)
{
return _guid;
}
}
и в профиле:
CreateMap<FooEntity, FooDomain>().ReverseMap();
CreateMap<RequiredGuid, Guid>().ConvertUsing(src => src); //calls implicit operator
Тогда я бы хотел отменить раскрытие моих свойств RequiredGuid
CreateMap<Guid, RequiredGuid>().ConvertUsing<GuidConverter>();
Конвертер:
public class GuidConverter : ITypeConverter<Guid, RequiredGuid>
{
public RequiredGuid Convert(Guid source, RequiredGuid destination, ResolutionContext context)
{
var propertyNameBeingMapped = ???;
return new RequiredGuid(propertyNameBeingMapped, source);
}
}
Когда я конвертирую FooDomain => FooEntity, у меня есть RequiredGuid => Guid: хорошо.
Когда я конвертирую FooEntity => FooDomain, Guid => RequiredGuid, я могуне получается имя свойства.
Я думаю, что мне следует использовать ResolutionContext, но как?
Спасибо!