本文介绍了映射域模型以查看模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个非常相似的点开始:。

I'm starting from a point very similar to: Domain Entities, DTO, and View Models.

建议使用DTO在域模型和MVC的ViewModel之间进行映射似乎是一致的期望。我寻求如何将域模型(基于实体框架的项目)桥接到WebAPI mvc项目的细节。

The advised use of DTOs to map between the domain model and the MVC's ViewModel seems consistent expectations. I seek details of how to bridge the domain model (Entity Framework-based project) to the WebAPI mvc project.

我从一个简单的POCO项目(生成由EF PowerTools反向工程我的存在的数据库),我想连接到一个MVC4 WebAPI项目。

I'm starting with a project of simple POCOs (generated by EF PowerTools to reverse engineer my existent db) that I want to connect to an MVC4 WebAPI project.

我希望随着我的解决方案的发展,我将为基准的POCO项目增加业务逻辑,也许这是这个问题的关键。将POCO转换成可以映射到MVC项目的业务逻辑。

I expect I'll be adding business logic to the baseline POCO project as my solution evolves and perhaps this is the crux of this issue. The business logic that transforms the POCOs into something that can be mapped to the MVC project.

正好如何将这些项目连接在一起,以便我可以在MVC中创建控制器了解EF项目实体的项目? Automapper?我们可以指向使用Automapper的特定功能的帖子/文档吗?

Exactly how do I wire these projects together so i can start creating controllers in the MVC project that knows about the entities of the EF project? Automapper? Can we point to posts/docs where this specific feature of Automapper is employed?

推荐答案

你不想让控制器知道EF实体 - 这就是这一点。 :)

You don't want controllers that knows about the EF entities - that's the whole point of this. :)

您自己说,应该使用DTO将您的域映射到您的视图模型,然后问如何使用mvc桥接我的域模型控制器?。您已经通过DTO回答了这一点!

You yourself say that the DTOs should be used to map your domain to your view model, and then you ask "how can I bridge my domain model with the mvc controllers?". You've already answered this - with DTOs!

DTO用作复杂业务对象和用于显示特定视图的模型之间的传输层。这两者都有特殊的要求,不与数据紧密相关 - 因此使用DTO将会使您更加脱节和分离问题。

The DTO serves as a transport layer between complex business objects and models used to display a certain view. Both of these have special requirements that don't strictly relate to "just data" - hence using DTOs will give you a greater decoupling and separation of concerns.

如果您不将域与视图模型分离,您将被迫直接在您的视图模型代码中引用您的EF对象,这会暴露不必要的数据和功能up链接。

If you don't decouple domain from view model, you will be forced to directly reference your EF objects in your view model code, which exposes unnecessary data and functions "up the chain".

现在,如果您使用WebAPI作为一种方式来发送数据,那么我认为您通常可以避免发送DTO,因为WebAPI数据通常不会实现视图模型逻辑。但是YMMV当然是取决于你如何计划使用你的控制器。

Now, if you use WebAPI as a way to ship data then I think you could usually get away with sending the DTOs, since WebAPI data usually wouldn't be implementing view model logic. But YMMV of course, depending on how you plan to use your controllers.

对于AutoMapper,我会说最好从自己的文档开始(他们甚至使用DTO的例子在其中):

For AutoMapper I'd say it's best to start with their own docs (they even use DTO examples in them): http://github.com/AutoMapper/AutoMapper/wiki/Getting-started

这篇关于映射域模型以查看模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:24
查看更多