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

查看:87
本文介绍了如何使用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; }
}

我的域对象是这样的:

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使其能够从这些数组映射到Set.似乎AutoMapper正在使用数组并将其转换为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映射到Domain所需的基础结构.任何帮助,我们将不胜感激.

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天全站免登陆