如何使用 AutoMapper 简单地将 NHibernate ISet 映射到 IList [英] How to simply map an NHibernate ISet to IList using AutoMapper

查看:21
本文介绍了如何使用 AutoMapper 简单地将 NHibernate ISet 映射到 IList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AutoMapper 从 DTO 映射到我的域.

I'm trying to use AutoMapper to map from DTO's to my Domain.

我的 DTO 可能如下所示:

My DTO's might look like this:

public class MyDTO
{
    public string Name { get; set; }
    public bool OtherProperty { get; set; }

    public ChildDTO[] Children { get; set;}
}

public class ChildDTO
{
    public string OtherName { get; set; }
}

My Domain 对象如下:

My Domain objects like this:

public class MyDomain
{
    public string Name { get; set; }
    public bool OtherProperty { get; set; }
    public ISet<ChildDomain> Children { get; set; }
}

public class ChildDomain
{
    public string OtherName { get; set; }
}

我将如何设置 AutoMapper 以便能够从这些 Array 映射到 Set.似乎 AutoMapper 正在获取 Array 并将它们转换为 IList,然后在转换为 ISet 时失败.

How would I setup AutoMapper to be able to map from these Array's to Set's. It seems like AutoMapper is taking the Array's and converting them into IList's then failing on conversion to ISet.

这是个例外

Unable to cast object of type 'System.Collections.Generic.List`1[DataTranser.ChildDTO]' to type 'Iesi.Collections.Generic.ISet`1[Domain.ChildDomain]'.

我希望找到一种简单的通用方法来做到这一点,这样我就可以最大限度地减少从 DTO 映射到域所需的基础设施.非常感谢任何帮助.

I'm hoping to find a simple generic way to do this so that I can minimize the infrastructure needed to map from DTO's to Domain. Any help is greatly appreciated.



更新:
那么我将如何对 MyDomain -> ChildDomain 进行建模,而不会以贫血的域模型结束?我知道如果 MyDomain 或 ChildDomain 中没有业务逻辑,域模型目前是贫乏的,但我们的目标是在我们前进时添加业务逻辑.我只是想确保我的视图模型可以转换为域模型并持久化.



UPDATE:
So then how would I model MyDomain -> ChildDomain without ending up with an anemic domain model? I understand that without business logic in MyDomain or ChildDomain the domain model is currently anemic, but the goal was to add business logic in as we move forward. I just want to ensure that my View Model can be translated into the domain model and persisted.

对于这种情况,从视图和域之间的简单映射转变为稍后添加业务规则,您有什么建议?

What would you suggest for this scenario, moving from a simple mapping between view and domain and later adding in business rules?

再次感谢您的帮助.

推荐答案

如果你的持久层很简单,使用 UseDestinationValue() 会告诉 AutoMapper 不要替换底层集合:

If your persistence layer is simple, using UseDestinationValue() will tell AutoMapper to not replace the underlying collection:

ForMember(dest => dest.Children, opt => opt.UseDestinationValue())

ForMember(dest => dest.Children, opt => opt.UseDestinationValue())

但是,如果它不简单,我们只是手动更新到域中.更新域模型的逻辑通常会变得更加复杂.进行反向映射会对域模型的形状施加限制,而这可能是您不想要的.

However, if it's not simple, we just do the updating manually back into the domain. The logic generally gets more complex to update the domain model. Doing reverse mapping puts constraints on the shape of your domain model, which you might not want.

这篇关于如何使用 AutoMapper 简单地将 NHibernate ISet 映射到 IList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆