开发者

ASP.NET MVC & AutoMapper (Populate View Model from Parent & Child Domain Objects)

开发者 https://www.devze.com 2023-02-08 20:33 出处:网络
I have following doimain objects: public class ComponentType { public int ComponentTypeID { get; set; } public string Component_Type { get; set; }

I have following doimain 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 will get a LIST of AffiliateComponentType from DB using NHibernate. Now I have to populate a LIST of AffiliateComponentTypeView (View Model) from LIST of AffiliateComponentType domain object. How can I achieve this using AutoMapper?

[Serializable]
public class AffiliateComponentTypeView
{
    public int ComponentTypeID { g开发者_如何学JAVAet; 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; }
}


The following mapping should do the job of flattening 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 modified your view model like this:

[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; }
}

The flattening will be performed automatically by AutoMapper using standard conventions so all you need is:

Mapper.CreateMap<AffiliateComponentType, AffiliateComponentTypeView>();

There will just be a slight problem with the Component_Type property as it clashes with AutoMapper's default naming convention so you might need to rename it.

Once you have the mapping defined you could map:

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


Somewhere in your app, you'll have a block of code that configures AutoMapper, so I'm guessing you'd have a block that looks like so:

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

Then, once you have your model back from nHibernate, you'll construct your view model like so:

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

Hope this gets you where you're headed!

0

精彩评论

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