Automapper better way of mapping property with custom logic - c#

I have a simple class called Supplier which has contacts and contact can have addresses. right now we are using only 1 contact and 1 address (line1 to line4) in that contact. In future we might use multiple contacts having multiple addresses. below is my class
public class SupplierDto
{
public string Name { get; set; }
public string Alias { get; set; }
public int? SupplierTypeId { get; set; }
public int? WebclicsManufacturerId { get; set; }
public string SAPCode { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Line4 { get; set; }
public int CountryId { get; set; }
public string PostalCode { get; set; }
public string ContactName { get; set; }
public string ContactEmail { get; set; }
public string ContactTelephone { get; set; }
public string ContactJobTitle { get; set; }
}
My Supplier class is a bit complex, so I am trying to map from Supplier to DTO and below is my mapping.
CreateMap<Supplier, SupplierDto>()
.ForMember(dest => dest.Line1, options => options.MapFrom(source => source.SupplierContacts.First().Contact.EntityAddresses.First().Address.Line1))
.ForMember(dest => dest.Line2, options => options.MapFrom(source => source.SupplierContacts.First().Contact.EntityAddresses.First().Address.Line2))
.ForMember(dest => dest.Line3, options => options.MapFrom(source => source.SupplierContacts.First().Contact.EntityAddresses.First().Address.Line3))
.ForMember(dest => dest.Line4, options => options.MapFrom(source => source.SupplierContacts.First().Contact.EntityAddresses.First().Address.Line4))
.ForMember(dest => dest.CountryId, options => options.MapFrom(source => source.SupplierContacts.First().Contact.EntityAddresses.First().Address.CountryId))
.ForMember(dest => dest.PostalCode, options => options.MapFrom(source => source.SupplierContacts.First().Contact.EntityAddresses.First().Address.PostalCode))
.ForMember(dest => dest.ContactName, options => options.MapFrom(source => source.SupplierContacts.First().Contact.Name))
.ForMember(dest => dest.ContactEmail, options => options.MapFrom(source => source.SupplierContacts.First().Contact.Email))
.ForMember(dest => dest.ContactTelephone, options => options.MapFrom(source => source.SupplierContacts.First().Contact.Telephone))
.ForMember(dest => dest.ContactJobTitle, options => options.MapFrom(source => source.SupplierContacts.First().Contact.JobTitle));
As you can see I have custom logic for each column. Now problem is if there is no contact/address, code breaks because I am using
First()
from LINQ, is there a better way to check if contacts exists then do mapping and if it has addresses then proceed with address mapping?

Just create an ContactDto and add it to the SupplierDto, then move all contact relevant properties to the ContactDto.
public class ContactDto
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Line4 { get; set; }
public int CountryId { get; set; }
public string PostalCode { get; set; }
public string ContactName { get; set; }
public string ContactEmail { get; set; }
public string ContactTelephone { get; set; }
public string ContactJobTitle { get; set; }
}
public class SupplierDto
{
public string Name { get; set; }
public string Alias { get; set; }
public int? SupplierTypeId { get; set; }
public int? WebclicsManufacturerId { get; set; }
public string SAPCode { get; set; }
public ContactDto Contact { get; set; }
}
Then the mapping should look something like this
CreateMap<Supplier, SupplierDto>()
.ForMember(dest => dest.Contact, options => options.MapFrom(source => source.SupplierContacts.FirstOrDefault()));
If Contact is null then AutoMapper didn't try to map it. If you want in the Future support multiple Contacts on the Supplier, then just change the ContactDto Contact Property to a List<ContactDto> and remove the .FirstOrDefault() in the Mapping.

Related

AutoMapper map a collection Property value from its Parent property

I have two models, Receipt.cs and ReceiptProduct.cs. What I want to achieve is to map the ICollection ReceiptProducts fields like PurchaseOrderId and ReceiptId from its parent Receipt.
Receipt.cs
public class Receipt
{
public Guid Id { get; set; }
public string Reference { get; set; }
public string PurchaseOrderId { get; set; }
public virtual ICollection<ReceiptProduct> ReceiptProducts { get; set; }
}
ReceiptProduct.cs
public class ReceiptProduct
{
public Guid Id { get; set; }
public string ReceiptId { get; set; }
public string PurchaseOrderId { get; set; }
public string ProductName { get; set; }
public string ProductId { get; set; }
public string Note { get; set; }
}
ReceiptProducts.ReceiptId <= Receipt.Id
ReceiptProducts.PurchaseOrderId <= Receipt.PurchaseOrderId
I tried the below code. But I got the error
CreateMap<DataEntities.Receipt, BusinessEntities.Receipt>()
.ForMember(dest => dest.ReceiptProducts.Select(x=>x.ReceiptId), automapper => automapper.MapFrom(src => src.Id));
Error : AutoMapper.AutoMapperConfigurationException: Custom configuration for members is only supported for top-level individual members on a type.
So how to map that collection property values.
try this.
public class ReceiptProduct
{
public Guid Id { get; set; }
public string ReceiptId { get; set; }
public string PurchaseOrderId { get; set; }
public string ProductName { get; set; }
public string ProductId { get; set; }
public string Note { get; set; }
**public Receipt Receipt { get; set; }**
}
Mapping
CreateMap<DataEntities.ReceiptProduct, BusinessEntities.Receipt>()
.ForMember(dest => x=>x.ReceiptId, opts => opts.MapFrom(src => src.Receipt.Id))
.ForMember(dest => x=>x.PurchaseOrderId , opts => opts.MapFrom(src => src.Receipt.PurchaseOrderId))
.ForMember(dest => x=>x.Reference , opts => opts.MapFrom(src => src.Receipt.Reference ));

How to select from a list and map the another object using Automapper

From the nested Entity list we want to select an object and map it in the Dto object, but when we use the Automapper to filter for object which deleteDate is equal null and map it, for each column Automapper filter again on db. how to filter one time and map selected to Dto object
We need to store all the information in our database so use DeleteDate column with default value null. so we use one to many relation to store history of our data.
public class User
{
public Guid UserId { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<CustomerDetail> CustomerDetails { get; set; }
}
public class CustomerDetail
{
public Guid CustomerDetailId { get; set; }
public float MaximumAmountPerTransaction { get; set; }
public float MaximumAmountPerDay { get; set; }
public bool IsConfirm { get; set; }
public Guid UserId { get; set; }
public virtual DateTimeOffset CreateDate { get; set; }
public virtual DateTimeOffset? DeleteDate { get; set; }
}
public class CustomerResponse
{
[Key]
public Guid CustomerId{ get; set; }
public string MobileNumber{ get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsConfirm { get; set; }
public float? MaximumAmountPerTransaction { get; set; }
public float? MaximumAmountPerDay { get; set; }
}
public class CustomerDomainProfile : Profile
{
public UserDomainProfile()
{
CreateMap<User, CustomerResponse>()
.ForMember(dest => dest.CustomerId,
opt => opt.MapFrom(src => src.UserId))
.ForMember(dest => dest.Country,
opt => opt.MapFrom(src => src.Country.CommonName))
.ForMember(dest => dest.IsConfirm,
opt => opt.MapFrom(src => src.CustomerDetails.FirstOrDefault(cd => cd.DeleteDate == null).IsConfirm))
.ForMember(dest => dest.MaximumAmountPerTransaction,
opt => opt.MapFrom(src => src.CustomerDetails.FirstOrDefault(cd => cd.DeleteDate == null).MaximumAmountPerTransaction))
.ForMember(dest => dest.MaximumAmountPerDay,
opt => opt.MapFrom(src => src.CustomerDetails.FirstOrDefault(cd => cd.DeleteDate == null).MaximumAmountPerDay))
}
}
I expect a query to the database and select the CustomerDetails with DeleteDate is null and map it, but the type of our define, for each column trying to query to database because
I expect that querying the database and selecting CustomerDetails with DeleteDate is null and its map, but our definition, for each column, is trying to query the database because it can not recognize that they are from a same table. How can we identify them that have fallen from a row to reduce the query? is any other solution or idea?

how to map Outer and Inner Source to same object - auto mapper

I have this structure:
public class UserEntity
{
public long Id { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public ComplexObject TimeZone { get; set; }
}
public class UserQueryResult
{
public UserEntity User { get; set; }
public ObjectId AccountId { get; set; }
}
public class UserDto
{
public long Id { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public string TimeZoneId { get; set; }
public string AccountId { get; set; }
}
I have UserEntity to UserDto mapping like this:
CreateMap<UserEntity, UserDto>()
.ForMember(dest => dest.TimeZoneId, opt => opt.ResolveUsing<UserTimeZoneResolver>())
.ForMember(dest => dest.AccountId, opt => opt.Ignore());
As you can see, there's a resolver from UserEntity to UserDto. Now, if I want to map UserQueryResult to UserDto, how could I reuse the above mapper without the need to map all properties manually?
CreateMap<UserQueryResult, UserDto>()
.ForMember(dest => dest.AccountId, opt => opt.MapFrom(src => src.AccountId.ToString()))
.// use the UserEntity to UserDto map?

AutoMapper Update UseDestinationValue Not Working as expected on Virtual Properties

Struggling to get AutoMapper (6.1.1) to work in this scenario of attempting to update an existing Vendor and associated Vendor Contacts.
I've tried using .ignore() and .UseDestinationValues() on the related entities, both to no avail.
Here's what happens to the destination values after the map:
existingStratusVendor.Id = 0 (should be value of existing)
existingStratusVendor.VendorContacts.Id = 0 (should be value of existing)
existingStratusVendor.Items = null, but had 1 related entity prior to mapping, same with all other related virtual properties. (this happens for all other virtual properties that I've marked as .UseDestinationValues() as well)
What am I doing wrong or am I misunderstanding how this is supposed to work?
POCOs
public partial class Vendor
{
public Vendor()
{
this.Items = new HashSet<Item>();
this.Items1 = new HashSet<Item>();
this.VendorContacts = new HashSet<VendorContact>();
this.POHeaders = new HashSet<POHeader>();
this.ReceiptHeaders = new HashSet<ReceiptHeader>();
this.ItemPriceCostRules = new HashSet<ItemPriceCostRule>();
}
public int Id { get; set; }
public int CompanyId { get; set; }
public string VendorName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Notes { get; set; }
public int CreatedById { get; set; }
public System.DateTime CreatedOn { get; set; }
public int ModifiedById { get; set; }
public System.DateTime ModifiedOn { get; set; }
public string FinancialsId { get; set; }
public int LeadTimeDays { get; set; }
public int SafetyStockDays { get; set; }
public virtual ICollection<Item> Items { get; set; }
public virtual ICollection<Item> Items1 { get; set; }
public virtual ICollection<VendorContact> VendorContacts { get; set; }
public virtual ICollection<POHeader> POHeaders { get; set; }
public virtual Company Company { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual UserProfile UserProfile1 { get; set; }
public virtual ICollection<ReceiptHeader> ReceiptHeaders { get; set; }
public virtual ICollection<ItemPriceCostRule> ItemPriceCostRules { get; set; }
}
public partial class VendorContact
{
public int Id { get; set; }
public int VendorId { get; set; }
public string ContactName { get; set; }
public string EmailAddress { get; set; }
public string OfficePhone { get; set; }
public string CellPhone { get; set; }
public int CreatedById { get; set; }
public System.DateTime CreatedOn { get; set; }
public int ModifiedById { get; set; }
public System.DateTime ModifiedOn { get; set; }
public bool PurchasingContact { get; set; }
public virtual Vendor Vendor { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual UserProfile UserProfile1 { get; set; }
}
Maps
CreateMap<Vendor, Vendor>()
.ForMember(dest => dest.Id, option => option.UseDestinationValue())
.ForMember(dest => dest.Company, option => option.UseDestinationValue())
.ForMember(dest => dest.POHeaders, option => option.UseDestinationValue())
.ForMember(dest => dest.ReceiptHeaders, option => option.UseDestinationValue())
.ForMember(dest => dest.Items, option => option.UseDestinationValue())
.ForMember(dest => dest.Items1, option => option.UseDestinationValue())
.ForMember(dest => dest.ItemPriceCostRules, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile1, option => option.UseDestinationValue())
;
CreateMap<VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.UseDestinationValue())
.ForMember(dest => dest.VendorId, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile1, option => option.UseDestinationValue())
Code
public ActionConfirmation<int> ImportFromFinancials(Vendor financialsModifiedVendor, int intUserId)
{
Vendor vendorToUpdate;
var existingStratusVendor = _vendorRepository
.SearchFor(a => a.CompanyId == intCompanyId && a.FinancialsId == financialsModifiedVendor.FinancialsId).FirstOrDefault();
if (existingStratusVendor == null) //add a new vendor
{
vendorToUpdate = financialsModifiedVendor;
}
else
{
Mapper.Map(financialsModifiedVendor, existingStratusVendor);
vendorToUpdate = existingStratusVendor;
}
//Save Vendor
var baseAppServ = new BaseAppServ<Vendor>(_repository);
var vendorUpdateResult = baseAppServ.SaveOrUpdate(vendorToUpdate, intUserId);
if (!vendorUpdateResult.WasSuccessful) return vendorUpdateResult;
...
}
both entities has the same name, it looks like you are missing a namespace
CreateMap<Other.Namespace.VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.UseDestinationValue())
.ForMember(dest => dest.VendorId, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile1, option => option.UseDestinationValue())

AutoMapper - How to Map to three level deep

I'm trying to user AutoMapper to flatten a Entity with relation to another Entity which has relation to third Entity to view model
How to map these three entities into one?
Source:
public class Address
{
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
public int CityId { get; set; }
public virtual City City { get; set; }
}
public class City
{
public int CityId { get; set; }
public string CityName { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
Destination:
Public Class AddressViewModel
{
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
public int CityId { get; set; }
public string CityName { get; set; }
public int CountryId { get; set; }
public string CountryName { get; set}
}
A couple of ways (at least). If you name your viewmodel fields differently it can happen by convention:
Public Class AddressViewModel
{
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
public int CityCityId { get; set; }
[DisplayName("City Name")]
public string CityCityName { get; set; }
public int CityCountryCountryId { get; set; }
[DisplayName("Country Name")]
public string CityCountryCountryName { get; set}
}
If that's too ugly, you can do it in CreateMap:
Mapper.CreateMap<Address, AddressViewModel>()
.ForMember(dest => dest.CityId, opts => opts.MapFrom(src => src.City.CityId))
.ForMember(dest => dest.CityName, opts => opts.MapFrom(src => src.City.CityName))
.ForMember(dest => dest.CountryId, opts => opts.MapFrom(src => src.City.Country.CountryId))
.ForMember(dest => dest.CountryName, opts => opts.MapFrom(src => src.City.Country.CountryName));
http://automapper.codeplex.com/wikipage?title=Flattening&referringTitle=Home
Example to use Include() to query data in deeper levels:
public AddressViewModel GetAddressById(int id)
{
var result = applicationDbContext.Address
.Include(o=>o.City)
.Include(o=>o.City.Country)
.FirstOrDefault(x=>x.AddressId == id);
return mapper.Map<AddressViewModel>(result);
}

Categories

Resources