最佳实践映射DTO到域对象? [英] Best Practices For Mapping DTO to Domain Object?

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

问题描述

我已经看到了很多有关DTO的映射域对象的问题,但我没觉得他们回答我的问题。我用过很多方法前,有我自己的意见,但我正在寻找的东西多一点具体的。

I've seen a lot of questions related to mapping DTOs to Domain Objects, but I didn't feel they answered my question. I've used many methods before and have my own opinions but I'm looking for something a little more concrete.

的情况:

我们有很多的域对象。我们使用的是里昂证券模型,使我们的域对象可以是pretty的复杂,它们包含自己的数据的访问。你不希望在电线上来回传递这些。我们将要写入一些新的服务,这将在多种格式(净,JSON等)返回数据。对于这个(以及其他原因),我们还要创建一支精干,数据传输对象在电线绕过。

We have many domain objects. We are using a CSLA model so our domain objects can be pretty complex and they contain their own data access. You do not want to pass these around on the wire. We are going to be writing some new services that will return data in a number of formats (.Net, JSON, etc.). For this (and other reasons) we are also creating a lean, data transfer object to pass around on the wire.

我的问题是,应该如何DTO和域对象进行连接?

我的第一反应就是用福勒,DTO模式型解决方案。我已经看到了这个做了很多次,感觉我的权利。域对象不包含引用DTO。外部实体(一个映射器或汇编)被调用来创建一个域对象DTO的。正常情况下的域对象侧的ORM。这种方法的缺点是,映射器往往会获得任何实际的情况非常复杂,可以是非常脆弱的。

My first reaction is to use a Fowler, DTO pattern-type solution. I've seen this done many times and it feels right to me. The domain object contains no reference to the DTO. An outside entity (a "mapper" or "assembler") is called to create a DTO from a Domain Object. Normally there is an ORM on the domain object side. The downside of this is that the "mapper" tends to get extremely complex for any real situation and can be very fragile.

提出另一个想法是域对象遏制的DTO,因为它只是一个精简的数据对象。域对象的属性将在内部参考DTO的属性,可能只是返回DTO,如果要求。我看不出有什么问题,这一点,但感觉不对。我看到一些文章,其中使用NHibernate的人出现了使用这种方法。

Another idea put forth is for the Domain Object to "contain" the DTO, since it's just a lean data object. The Domain Object properties would internally reference the DTO properties and could just return the DTO if asked for. I can see no problems with this but it feels wrong. I have seen some articles where people using NHibernate appeared to use this method.

是否还有其他方法吗?以上是值得使用的一种方式?如果是的话,或者如果没有,为什么?

感谢提前任何见解。

推荐答案

有一个映射你的域和你的DTO之间坐落着一个好处是不一样的时候,你只支持单一映射appearent,但作为映射关系的数量增大,具有code从域中分离出有助于保持域简单和精简。你不会弄乱你的域了很多额外的重量。

A benefit of having a mapper that sits between your domain and your DTO is not as appearent when you are only supporting a single mapping, but as the number of mappings increases, having that code isolated from the domain helps keep the domain simpler and leaner. You won't be cluttering your domain with a lot of extra weight.

就个人而言,我尽量保持映射出我的域名实体,并把责任就是我所说的管理/服务层。这是应用程序和程序存储库(IES)之间位于一层,并提供业务逻辑,如工作流程的协调(如果修改,可能也必须修改B以便服务A将与服务B)。

Personally, I try and keep the mapping out of my domain entities and put the responsibility in what I call "Manager / Service layer". This is a layer that sits between the application and the respository(ies), and provides business logic such as workflow coordination (If you modify A, you might have to also modify B so service A will work with Service B).

如果我有很多可能的结局格式,我可能会考虑创建,可以使用Visitor模式,例如改造我的实体可插式格式,但我还没有找到一个还需要对任何这种复杂的。

If I had a lot of possible ending formats, I might look at creating a plugable formatter that could use the Visitor pattern, for example to transform my entities, but I've not found a need yet for anything this complex.

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

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