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
};
When target property is of type string
and the source property is different than string
:
target.Property = source.Property.ToString()
This converter uses the following strategies for converting a complex type to a primitive one if such conversion is needed:
Nullable<T>
to T
.target.Addresses = source.Addresses.ConvertAll(sourceAddress => new AddressDTO
{
City = sourceAddress.City,
ZipCode = sourceAddress.ZipCode,
Street = sourceAddress.Street,
FlatNo = sourceAddress.FlatNo,
BuildingNo = sourceAddress.BuildingNo
});
This converter allows for converting string to the following types by using Parse
method:
DateTime
TimeSpan
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
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))
};
}
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()
};
}
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))
};
}
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
});
}
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();
}
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.
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"]
};
}
This converter allows for re-using already defined instance methods for type conversions. More details can be found in Existing conversion sources.
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.
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
{
}