Type converters

Free converters

Object To Object

target.MainAddress = new AddressDTO
{
    City = source.MainAddress.City,
    ZipCode = source.MainAddress.ZipCode,
    Street = source.MainAddress.Street,
    FlatNo = source.MainAddress.FlatNo,
    BuildingNo = source.MainAddress.Building_No
};

Object To String

When target property is of type string and the source property is different than string:

target.Property = source.Property.ToString()

Object To Simple Value

This converter uses the following strategies for converting a complex type to a primitive one if such conversion is needed:

  • Map Nullable<T> to T.
  • Add explicit casting if exists.
  • Object unwrapping - if the source contains a single property of a single method that result type matches the target then this property or method is called to satisfy type conversion.

Collection To Collection

target.Addresses = source.Addresses.ConvertAll(sourceAddress => new AddressDTO
    {
        City = sourceAddress.City,
        ZipCode = sourceAddress.ZipCode,
        Street = sourceAddress.Street,
        FlatNo = sourceAddress.FlatNo,
        BuildingNo = sourceAddress.BuildingNo
    });

Premium converters

⭐ String Parsing Converter

This converter allows for converting string to the following types by using Parse method:

  • all primitives
  • DateTime
  • TimeSpan
  • enum types

⭐ Enum To Enum Converter

AuthenticationKindAPI Map(AuthenticationKind source)
{
    return source switch
    {
        AuthenticationKind.Password => AuthenticationKindAPI.Password,
        AuthenticationKind.Active_Directory => AuthenticationKindAPI.ActiveDirectory,
        AuthenticationKind.Social_Media => AuthenticationKindAPI.SocialMedia,
        _ => throw new InvalidEnumArgumentException(nameof(source), (int)source, typeof(AuthenticationKind))
    };
}

More details about options related to this converter can be found in enum-mappings

⭐ Enum To string Converter

This converter generates high performance enum to string conversion with switch statement/expression:

private string Map(AuthenticationKind source)
{
    return source switch
    {
        AuthenticationKind.Password => nameof(AuthenticationKind.Password),
        AuthenticationKind.Active_Directory => nameof(AuthenticationKind.Active_Directory),
        AuthenticationKind.Social_Media => nameof(AuthenticationKind.Social_Media),
        _ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(source), (int)source, typeof(AuthenticationKind))
    };
}

⭐ Object To Dictionary

Converting to Dictionary<string, object>:

public static Dictionary<string, object> Map(AddressDTO source)
{
    return new Dictionary<string, object>()
    {
        [nameof(AddressDTO.City)] = source.City,
        [nameof(AddressDTO.ZipCode)] = source.ZipCode,
        [nameof(AddressDTO.Street)] = source.Street,
        [nameof(AddressDTO.FlatNo)] = source.FlatNo,
        [nameof(AddressDTO.BuildingNo)] = source.BuildingNo
    };
}

Converting to Dictionary<string, string>:

public static Dictionary<string, string> Map(AddressDTO source)
{
    return new Dictionary<string, string>()
    {
        [nameof(AddressDTO.City)] = source.City,
        [nameof(AddressDTO.ZipCode)] = source.ZipCode,
        [nameof(AddressDTO.Street)] = source.Street,
        [nameof(AddressDTO.FlatNo)] = source.FlatNo.ToString(),
        [nameof(AddressDTO.BuildingNo)] = source.BuildingNo.ToString()
    };
}

⭐ Dictionary To Object

For Dictionary<string,string> source:

public static AddressDTO Map(Dictionary<string,string> source)
{
    return new AddressDTO
    {
        City = source.GetValueOrDefault(nameof(AddressDTO.City)),
        ZipCode = source.GetValueOrDefault(nameof(AddressDTO.ZipCode)),
        Street = source.GetValueOrDefault(nameof(AddressDTO.Street)),
        FlatNo = int.Parse(source.GetValueOrDefault(nameof(AddressDTO.FlatNo))),
        BuildingNo = int.Parse(source.GetValueOrDefault(nameof(AddressDTO.BuildingNo)))
    };
}

For Dictionary<string, object> source:

public static AddressDTO Map(Dictionary<string, object> source)
{
    return new AddressDTO
    {
        City = (string)source.GetValueOrDefault(nameof(AddressDTO.City)),
        ZipCode = (string)source.GetValueOrDefault(nameof(AddressDTO.ZipCode)),
        Street = (string)source.GetValueOrDefault(nameof(AddressDTO.Street)),
        FlatNo = (int)source.GetValueOrDefault(nameof(AddressDTO.FlatNo)),
        BuildingNo = (int)source.GetValueOrDefault(nameof(AddressDTO.BuildingNo))
    };
}

⭐ Dictionary To Dictionary

public Dictionary<string, AddressDTO> Map(Dictionary<string, AddressEntity> source)
{
    return source.ToDictionary(kv => kv.Key, kv => new AddressDTO
    {
        City = kv.Value.City,
        ZipCode = kv.Value.ZipCode,
        Street = kv.Value.Street,
        FlatNo = kv.Value.FlatNo,
        BuildingNo = kv.Value.BuildingNo
    });
}

⭐ Dictionary To Collection

public List<AddressDTO> Map(Dictionary<string, AddressEntity> source)
{
    return source.Values.Select(sourceValue => new AddressDTO
    {
        City = sourceValue.City,
        ZipCode = sourceValue.ZipCode,
        Street = sourceValue.Street,
        FlatNo = sourceValue.FlatNo,
        BuildingNo = sourceValue.BuildingNo
    }).ToList();
}

⭐ Collection To Dictionary

public static UserDTO Map(UserEntity entity)
{
    return new UserDTO
    {
        Id = entity.Id,
        FirstName = entity.FirstName,
        LastName = entity.LastName,
        Addresses = entity.Addresses.ToDictionary(entityAddress => throw new NotImplementedException(), entityAddress => new AddressDTO
        {
            ZipCode = entityAddress.ZipCode,
            Street = entityAddress.Street,
            FlatNo = entityAddress.FlatNo,
            BuildingNo = entityAddress.BuildingNo
        })
    };
}

As there is no deterministic way to figure out what should be used as the key function, MappingGenerator leaves that decision up to the developer and generates throw new NotImplementedException() expression as a placeholder.

⭐ DataReader To Object

public UserDTO Map(IDataReader reader)
{
    return new UserDTO
    {
        Id = Convert.ToInt32(reader["Id"]),
        Version = Convert.ToInt32(reader["Version"]),
        UnitId = reader["UnitId"] is DBNull ? default(int?) : Convert.ToInt32(reader["UnitId"]),
        Login = Convert.ToString(reader["Login"]),
        FirstName = Convert.ToString(reader["FirstName"]),
        LastName = Convert.ToString(reader["LastName"]),
        Age = Convert.ToInt32(reader["Age"]),
        Authentication = (AuthenticationKindAPI)reader["Authentication"]
    };
}

⭐ Existing Instance Converters

This converter allows for re-using already defined instance methods for type conversions. More details can be found in Existing conversion sources.

⭐ Existing Static Converters

This converter allows for re-using already defined static methods (regular and extension methods) for type conversions. More details can be found in Existing conversion sources.

⭐ Unwrapping converter

If source type doesn’t match the target, but have a single property that matches the target type, then this property is used for mapping:

public class TestMapper
{
    public static UserDTO Map(OtherUserDTO data)
    {
        return new UserDTO
        {
            Address = data.Address.Value
        };
    }
}

public class UserDTO
{
    public AddressDTO Address {get; set;}
}
    
public class OtherUserDTO
{
    public AddressWrapperDTO Address {get; set;}
}
    
public class AddressWrapperDTO
{
    public AddressDTO Value {get; set;}
}

public class AddressDTO
{
}