ASP.NET MVC and AutoMapper (a model for representing content from parent and child domain objects)

I have the following obimain objects:

public class ComponentType
{
    public int ComponentTypeID { get; set; }
    public string Component_Type { get; set; }
    public string ComponentDesc { get; set; }
}

public class AffiliateComponentType
{
    public int AffiliateComponentID { get; set; }
    public int AffiliateID { get; set; }
    public ComponentType ComponentType { get; set; }
    public bool MandatoryComponent { get; set; }
    public bool CanBeBookedStandalone { get; set; }
    public int PreferenceOrder { get; set; }
}

I get an AffiliateComponentType LIST from the database using NHibernate. Now I need to fill out the LIST of AffiliateComponentTypeView (View Model) from the LIST of the AffiliateComponentType domain object. How can I achieve this with AutoMapper?

[Serializable]
public class AffiliateComponentTypeView
{
    public int ComponentTypeID { get; set; }
    public string Component_Type { get; set; }
    public string ComponentDesc { get; set; }
    public bool MandatoryComponent { get; set; }
    public bool CanBeBookedStandalone { get; set; }
    public int PreferenceOrder { get; set; }
}
+3
source share
2 answers

The following display should do the work of aligning your model :

Mapper
    .CreateMap<AffiliateComponentType, AffiliateComponentTypeView>()
    .ForMember(
        dest => dest.ComponentTypeID,
        opt => opt.MapFrom(src => src.ComponentType.ComponentTypeID)
    )
    .ForMember(
        dest => dest.Component_Type,
        opt => opt.MapFrom(src => src.ComponentType.Component_Type)
    )
    .ForMember(
        dest => dest.ComponentDesc,
        opt => opt.MapFrom(src => src.ComponentType.ComponentDesc)
    );

and if you change your view model as follows:

[Serializable]
public class AffiliateComponentTypeView
{
    public int ComponentTypeComponentTypeID { get; set; }
    public string ComponentTypeComponent_Type { get; set; }
    public string ComponentTypeComponentDesc { get; set; }
    public bool MandatoryComponent { get; set; }
    public bool CanBeBookedStandalone { get; set; }
    public int PreferenceOrder { get; set; }
}

Smoothing will be performed automatically with AutoMapper using standard conventions, so you only need to:

Mapper.CreateMap<AffiliateComponentType, AffiliateComponentTypeView>();

Component_Type, AutoMapper, .

, :

IEnumerable<AffiliateComponentType> source = ...
IEnumerable<AffiliateComponentTypeView> dest = Mapper.Map<IEnumerable<AffiliateComponentType>, IEnumerable<AffiliateComponentTypeView>>(source);
+2

- , AutoMapper, , , :

Mapper.CreateMap<ComponentType, AffiliateComponentTypeView>();
Mapper.CreateMap<AffiliateComponentType, AffiliateComponentTypeView>();

, nHibernate, :

var model = Session.Load<AffiliateComponentType>(id);
var viewModel = Mapper.Map<AffiliateComponentType, 
    AffiliateComponentTypeView>(model);
if (model.ComponentType != null)
    Mapper.Map(model.ComponentType, viewModel);

, , !

+1

Source: https://habr.com/ru/post/1790496/


All Articles