AutoMapper和Entity Framework POCO具有明确加载 [英] AutoMapper and Entity Framework POCO with explicit loading

查看:200
本文介绍了AutoMapper和Entity Framework POCO具有明确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要我显式地加载在我的POCO的关系,但因为我切换到AutoMapper我是这么认为的,在服务层我的code看起来比较清爽,我可以指示它pre-负荷的关系对我来说:

I explicitly load relationships on my POCO when I need to, but since I switched to AutoMapper I thought I could instruct it to pre-load relationships for me so that my code in service layer looks cleaner:

Mapper.CreateMap<Issue, IssueEditModel>().BeforeMap((i, m) => 
        LoadProperties<Issue>(() => 
            { return kernel.GetService<IIssuesRepository>(); },
            i, new Expression<Func<Issue, object>>[]
            {
                e => e.RelationshipA,
                e => e.RelationshipB
            }
        )
);

该LoadProperties方法查找使用ObjectContext中使用的方法LoadProperty和DependencyResolver上市加载关系信息库。现在,我的服务可以轻松地映射EF POCO查看模型的单一Mapper.Map电话。

The LoadProperties method looks up the repository using DependencyResolver and loads listed relationships using ObjectContext's LoadProperty method. Now my services can easily map EF POCO to view model with a single Mapper.Map call.

有没有人试过这种?什么是潜在的陷阱?是否有意义保持你的服务/资源库层所有这些LoadProperty电话,并保持AutoMapper映射尽可能地简单?

Has anyone tried this before? What are potential pitfalls? Does it make sense to keep all these LoadProperty calls in your service/repository layers and keep AutoMapper mappings as simple as possible?

让我困扰的是,你可以使AutoMapper做很多繁琐的工作,为你喜欢类型转换和标识从视图模型转换为POCO当仰视的实体,但在同一时间这从移动这个逻辑服务/资源库AutoMapper配置。如果你有丰富的经验,用这个请你分享你的看法。

What bothers me is that you can make AutoMapper do a lot of tedious work for you like converting types and looking up entities by IDs when converting from view model to POCO, but at the same time this moves this "logic" from your service/repository to AutoMapper configuration. If you have extensive experience with this please do share your thoughts.

推荐答案

我一直在读更多层的领域驱动设计和责任在你的web应用程序,现在我知道我一直在滥用AutoMapper。只是因为我可以把它做这些事并不意味着我应该。其中一个缺陷是测试。因为我到AutoMapper来电最终击中数据库解决一些关系,我无法使用这些映射单元测试,我要创建一组不同的映射只是我的测试。

I've been reading more on domain driven design and responsibilities of layers in your web application and now I realize I have been abusing AutoMapper. Just because I can make it do these things doesn't mean I should. One of the pitfalls is testing. Because my calls to AutoMapper end up hitting the database to resolve some relationships I cannot use these mappings for unit-testing, I have to create a different set of mappings just for my test.

您库中,应当pre-负荷必要的关系。人们建议有方法接受控制是否有一定的关系应该用include加载标志。在我来说,我想我会永远载入很多一对一的关系,当实体的许多侧面和检索所有的关系,当一个单一的实体(例如详细视图)。

Your repositories should pre-load necessary relationships. People suggest having methods accept flags that control whether a certain relationship should be loaded using Include. In my case I think I will always load many-to-one relationships when the entity is on the many side and all relationships when retrieving a single entity (for detail view for example).

这篇关于AutoMapper和Entity Framework POCO具有明确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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