ASP.NET MVC 与AutoMapper(从父域对象和子域对象填充视图模型)
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下映射应该完成展平模型的工作:
如果您像这样修改您的视图模型:
AutoMapper 将使用标准约定自动执行扁平化,因此您所需要的是:
Component_Type
属性只会有一个小问题,因为它与 AutoMapper 的默认命名约定冲突所以你可能需要重命名它。定义映射后,您可以映射:
The following mapping should do the job of flattening your model:
and if you modified your view model like this:
The flattening will be performed automatically by AutoMapper using standard conventions so all you need is:
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:
在你的应用程序中的某个地方,你将有一个配置 AutoMapper 的代码块,所以我猜你会有一个如下所示的代码块:
然后,一旦你从 nHibernate 返回模型,你将构建你的视图像这样的模型:
希望这能让您到达目的地!
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:
Then, once you have your model back from nHibernate, you'll construct your view model like so:
Hope this gets you where you're headed!