AutoMapper最佳实践 - 我应该问的DAO的信息,从DTO履行域对象映射? [英] AutoMapper best practices - Should I be asking the DAO for information to fulfill mapping from DTO to domain object?

查看:186
本文介绍了AutoMapper最佳实践 - 我应该问的DAO的信息,从DTO履行域对象映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/// <summary>
///     Initialize the AutoMapper mappings for the solution.
///     http://automapper.codeplex.com/
/// </summary>
public static void CreateAutoMapperMaps()
{
    IDaoFactory daoFactory = DependencyResolver.Current.GetService<IDaoFactory>();

    Mapper.CreateMap<Error, ErrorDto>()
            .ReverseMap();

    IPlaylistDao playlistDao = daoFactory.GetPlaylistDao();
    IUserDao userDao = daoFactory.GetUserDao();

    Mapper.CreateMap<Playlist, PlaylistDto>();
    Mapper.CreateMap<PlaylistDto, Playlist>()
            .ForMember(playlist => playlist.User, opt => opt.MapFrom(playlistDto => userDao.Get(playlistDto.UserId)));

    Mapper.CreateMap<PlaylistItem, PlaylistItemDto>();
    Mapper.CreateMap<PlaylistItemDto, PlaylistItem>()
            .ForMember(playlistItem => playlistItem.Playlist,
                        opt => opt.MapFrom(playlistItemDto => playlistDao.Get(playlistItemDto.PlaylistId)));

    Mapper.CreateMap<ShareCode, ShareCodeDto>().ReverseMap();

    Mapper.CreateMap<User, UserDto>().ReverseMap();
    Mapper.CreateMap<Video, VideoDto>().ReverseMap();

    Mapper.AssertConfigurationIsValid();
}

朋友告诉我AutoMapper依赖DAO以实现从DTO到域的映射。

A friend is telling me that it is bad practice for AutoMapper to rely on DAOs to fulfill mappings from DTO to Domain.

我不明白为什么这是不好的做法,也不知道如何有效地工作在我的域对象空引用。

I don't understand why this is bad practice nor do I understand how it would be possible to effectively work on my domain object with null references.

任何人都可以解释?感谢

Can anyone explain? Thanks

推荐答案

这主要是我根据我的经验和阅读。其他人可能不同意。如果您持有与其他层级的严格网域实体分隔,那么使用AutoMapper 填充您的域实体(DE)将被视为不良。严格的DE设计通常不会暴露其可设置形式的属性。您的实体将嫉妒地控制如何设置数据,仔细验证审查输入,然后才允许成为实体的一部分。这通常只有公开提供的方法。我觉得这是一个关键系统和非常复杂的业务逻辑的有用模式。

This is mostly my opinion based on my experience and reading. Others might disagree. If you hold to a strict domain entity separation from other tiers, then using AutoMapper to populate your domain entity (DE) would be considered bad. Strict DE design generally will not expose its properties in settable form. Your entity will jealously control how the data is set with careful validation that vets the input before permitting it to become part of the entity. Often this takes the form of only making methods available publicly. I feel this is a useful mode for critical systems and very complex business logic.

我的问题与上述模型是在许多情况下,它是overkill,您的域数据的副本太多。您真的要将从数据源加载的DTO映射到DE,然后将您的DE中的另一个DTO映射到您的视图?痛苦,更多的缺陷空间。

My problem with the above model is that in many situations, it is overkill and will result in way too many copies of your domain data. Do you really want to map a DTO loaded from your datasource to your DE, then map another DTO from your DE to your view? Painful, with more room for bugs.

对于更小,更简单的系统,我认为使用一个好的ORM解决方案使你的DE映射到数据存储更有意义,使用洋葱架构管理依赖关系,并将您的DE映射到视图模型。这是AutoMapper非常有帮助的地方。

For smaller, simpler systems, I think it makes more sense to make your DE map to the data store with a good ORM solution, use an Onion Architecture to manage dependencies, and map your DE to view models as appropriate. This is where AutoMapper is very helpful.

这篇关于AutoMapper最佳实践 - 我应该问的DAO的信息,从DTO履行域对象映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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