开发者

Automapper Question

I have four entities Organization and Addr开发者_开发技巧ess ..for the sake of brevity I have omitted all but the properties in question in my entities and viewmodels

public class Organization : VersionedAuditableEntityWithTypedId<Guid>, IAggregateRoot
{
    //Organization related properties are in here

    /// <summary>
    /// Organization Address
    /// </summary>
    [NotNull]
    public virtual Address Address { get; set; }
}

public class Address : AuditableEntityWithTypedId<Guid>
{
   // Address related properties are in here

    /// <summary>
    /// Region/State
    /// </summary>
    [NotNull]
    public virtual Region Region { get; set; }
}

public class Country : EntityWithTypedId<Guid>
{
    /// <summary>
    /// Name of country
    /// </summary>
    [DomainSignature, Length(Max = 50), NotNullNotEmpty, SelectListItemDisplay, SelectListItemValue]
    public virtual string Name { get; set; }
}

public class Region : EntityWithTypedId<Guid>
{
    /// <summary>
    /// Name of region
    /// </summary>
    [Length(Max = 50), NotNullNotEmpty, SelectListItemDisplay,SelectListItemValue]
    public  virtual string Name { get; set; }

    /// <summary>
    /// Country
    /// </summary>
    [NotNull]
    [SuppressMessage("Microsoft.Naming", "CA1721", Justification = "Common naming for type descriptions")]
    public virtual Country Country { get; set; }
}

I have two view models

  1. EditOrganizationViewModel
  2. EditAddressViewModel

Within my EditOrganizationViewModel I have the following property

public class EditOrganizationViewModel
{

 /// <summary>
 /// Organization address
 /// </summary>
 public EditAddressViewModel Address { get; set; }

}

My EditAddressViewModel has a list of countries

public class EditAddressViewModel

{

/// <summary>
/// List of countries
/// </summary>
public IEnumerable<SelectListItem> Countries { get; set; }

}

My countries property does not map to anything in my address entity but is needed to display a list of countries and it popluated in my service layer.

I perform mapping once in my application as so...

Once for the EditAddressViewModel...

Mapper.CreateMap<Address, EditAddressViewModel>()
.ForMember(dest => dest.LabelForAddress1, opt => opt.Ignore())
.ForMember(dest => dest.LabelForAddress2, opt => opt.Ignore())
.ForMember(dest => dest.LabelForAddress3, opt => opt.Ignore())
.ForMember(dest => dest.LabelForAddressType, opt => opt.Ignore())
.ForMember(dest => dest.LabelForCity, opt => opt.Ignore())
.ForMember(dest => dest.LabelForCountry, opt => opt.Ignore())
.ForMember(dest => dest.LabelForPostcode, opt => opt.Ignore())
.ForMember(dest => dest.LabelForRegion, opt => opt.Ignore())
.ForMember(dest => dest.Types, opt => opt.Ignore())
.ForMember(dest => dest.Countries, opt => opt.Ignore())
.ForMember(dest => dest.Regions, opt => opt.Ignore())
.ForMember(dest => dest.Country, opt => opt.MapFrom(src => src.Region.Country.Id))
.ForMember(dest => dest.Region, opt => opt.MapFrom(src => src.Region.Id))
.ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type.Id))
Mapper.CreateMap<EditAddressViewModel, Address>();

and once for the EditOrganizationViewModel..

Mapper.CreateMap<Organization, EditOrganizationViewModel>()
.ForMember(dest => dest.LabelForOrganizationName, opt => opt.Ignore())
.ForMember(dest => dest.LabelForTeamSport, opt => opt.Ignore())
.AfterMap(
(organization, viewModel) =>
MapListItemToViewModel(organization.Phones, viewModel.Phone,
p => p.Type.Name == Resources.BusinessPhone))
.AfterMap(
(organization, viewModel) =>
MapListItemToViewModel(organization.Phones, viewModel.Fax,
p => p.Type.Name == Resources.FaxPhone)) 
.AfterMap(
(organization, viewModel) =>
MapListItemToViewModel(organization.EmailAddresses, viewModel.EmailAddress,
p => p.Type.Name == Resources.PrimaryEmailAddress));

I build my view model as below

public EditOrganizationViewModel CreateOrganizationFormViewModel()
{
        // Create the organization view model
        var viewModel = new EditOrganizationViewModel();

        // Load the organization and bind to view model
        var organization = _organizationRepository.FindAll().FirstOrDefault();

        if (organization == null) organization = new Organization();

         // Business Address
        viewModel.Address =
            AddressManagementService.Instance.CreateEditAddressViewModel(Resources.BusinessAddress);

   **At this point my countries list is populated**

   // Map the organization to the view model
      Mapper.Map(organization, viewModel);

  **My countries property is now null after Mapper.Map is called**
}

The problem arises when I call Mapper.Map(organization, viewModel). The countries list is now null. I am not sure how setup my mapping configurations to preserve my list of coutries. I have tried using a valueresolver but it only gets called if the address of the organization is not null. I would hate to have to really flatten my organizationviewmodel and include the address information. Any help would be greatly appreciated.

if the or


either of 2 things should work...

  1. for that field in the mapping, instead of Ignore() try UseDestinationValue()
  2. populate the country list after the map is executed (reverse the order of the last 2 lines of your function)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜