Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created January 30, 2018 10:11
Show Gist options
  • Select an option

  • Save lbargaoanu/31d8236589f29f2e530ada5dbeccdaf6 to your computer and use it in GitHub Desktop.

Select an option

Save lbargaoanu/31d8236589f29f2e530ada5dbeccdaf6 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<IBaseUserDto, IBaseUserEntity>()
.ForMember(d=>d.Name, opt => opt.Ignore());
cfg.CreateMap(typeof(BaseUserDto<>), typeof(BaseUserEntity<>));
cfg.CreateMap(typeof(ConcreteUserDto), typeof(ConcreteUserEntity))
.IncludeBase(typeof(IBaseUserDto), typeof(IBaseUserEntity));
});
var user = new ConcreteUserDto
{
Id = "my-id",
Name = "my-User"
};
var userEntity = Mapper.Map<ConcreteUserEntity>(user).Dump();
}
public interface IBaseUserDto
{
string Name { get; set; }
}
public abstract class BaseUserDto<TIdType> : IBaseUserDto
{
public TIdType Id { get; set; }
public string Name { get; set; }
}
public class ConcreteUserDto : BaseUserDto<string>
{
}
public interface IBaseUserEntity
{
string Name { get; set; }
}
public abstract class BaseUserEntity<TIdType> : IBaseUserEntity
{
public TIdType Id { get; set; }
public string Name { get; set; }
}
public class ConcreteUserEntity : BaseUserEntity<string>
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment