ASP.NET MVC 与AutoMapper(从父域对象和子域对象填充视图模型)

发布于 2024-10-16 01:02:29 字数 1077 浏览 1 评论 0原文

我有以下 doimain 对象:

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

我将使用 NHibernate 从数据库获取 AffiliateComponentType 的列表。现在,我必须从 AffiliateComponentType 域对象的列表中填充 AffiliateComponentTypeView(视图模型)的列表。如何使用 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; }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-10-23 01:02:29

以下映射应该完成展平模型的工作:

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)
    );

如果您像这样修改您的视图模型:

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

AutoMapper 将使用标准约定自动执行扁平化,因此您所需要的是:

Mapper.CreateMap<AffiliateComponentType, AffiliateComponentTypeView>();

Component_Type 属性只会有一个小问题,因为它与 AutoMapper 的默认命名约定冲突所以你可能需要重命名它。

定义映射后,您可以映射:

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

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);
醉生梦死 2024-10-23 01:02:29

在你的应用程序中的某个地方,你将有一个配置 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);

希望这能让您到达目的地!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文