I have spent a couple of days trying to track this down without any luck.
I have a table of addresses. Each row has 4 address fields. I want to map them to another object that has one field that is a single string made up of the 4 fields, but (there's always a but), if one of the fields contains a null or an empty string I want to ignore it.
eg The address table contains :- Address1 = House Number Address2 = Street Address3 = Address4 = Town
New object would contain the string :- House Number, Street, Town.
As requested this is where I am at :-
The AutoMapper config file
public static class AutoMapperConfig
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new AddressSearchList_ToResponse_Profile());
});
}
}
The Profile Definition :
public class AddressSearchList_ToResponse_Profile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<Address, AddressSearchResponseDto>()
.ConvertUsing<ConvertAddressToSearchList>();
Mapper.AssertConfigurationIsValid();
//This is as far as I get - what am I missing
}
}
And finally the conversion routine (admittedly not the slickest code ever):
public class ConvertAddressToSearchList : ITypeConverter<Address, AddressSearchResponseDto>
{
public AddressSearchResponseDto Convert(ResolutionContext context)
{
string newAddress = string.Empty;
Address oldAddress = (Address)context.SourceValue;
int addressId = oldAddress.Id;
newAddress = oldAddress.AddressLine1;
if (!String.IsNullOrEmpty(oldAddress.AddressLine2))
{
newAddress += ", " + oldAddress.AddressLine2;
}
if (!String.IsNullOrEmpty(oldAddress.AddressLine3))
{
newAddress += ", " + oldAddress.AddressLine3;
}
if (!String.IsNullOrEmpty(oldAddress.AddressLine4))
{
newAddress += ", " + oldAddress.AddressLine4;
}
if (!String.IsNullOrEmpty(oldAddress.County))
{
newAddress += ", " + oldAddress.County;
}
if (!String.IsNullOrEmpty(oldAddress.Postcode))
{
newAddress += ". " + oldAddress.Postcode;
}
AddressSearchResponseDto searchAddress = new AddressSearchResponseDto { Id = addressId, Address = newAddress };
return searchAddress;
}
Thanks
Steve
Maybe, it's just me, but (there's always a but), what is the question?
I think it's already converted, you just missing the code to actually mapping the source entity and display the result to monitor, like this:
AddressSearchResponseDto result = Mapper.Map<Address,AddressSearchResponseDto>(source);
Console.WriteLine(result.newAddress);
To fix this change the Profile Definition to :
public class AddressSearchList_ToResponse_Profile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<Address, AddressSearchResponseDto>()
.ConvertUsing<ConvertAddressToSearchList>();
}
}
Then in the code that calls the automapper gubbins you need to do this :
var query = from a in addressRepository.GetAll() select a;
IList<AddressSearchResponseDto> result = Mapper.Map<IList<Address>, IList<AddressSearchResponseDto>>(query.ToList());
result now holds the correct data.
Thanks you very much for your help guys.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.