从DTO转换为实体和实体到DTO的简便方法?

时间:2021-02-11 17:33:19

I have worked on several distributed client/server projects recently, and one pain point that we always run into is translating the DTO objects into our entities and vice-versa. I was wondering if anyone has a "simple" solution to this time sink?

我最近参与了几个分布式客户端/服务器项目,我们经常遇到的一个痛点是将DTO对象转换为我们的实体,反之亦然。我想知道是否有人对这个时间下沉有一个“简单”的解决方案?

One thing I thought about was coming up with some sort of translation using reflection...I guess you'd have to make sure your property names were exactly the same on each side of the wire - but seems like it might work.

我想到的一件事就是使用反射来进行某种翻译...我想你必须确保你的属性名称在线的两侧完全相同 - 但看起来它可能会起作用。

Just looking for a way to avoid some of this time sink in my development.

只是想找到一种方法来避免一些时间沉入我的开发中。

Thanks!!

谢谢!!

4 个解决方案

#1


3  

I have been using AutoMapper recently, and it works like a charm.

我最近一直在使用AutoMapper,它就像一个魅力。

#2


3  

Pain and risk

of getting/setting properties by hand! Have you tried this: http://dozer.sourceforge.net/.

手动获取/设置属性!你试过这个:http://dozer.sourceforge.net/。

A flexible and configurable framework for translating bean to bean. Might help!

一个灵活且可配置的框架,用于将bean转换为bean。可能有帮助!

#3


0  

I've had success using tools like XDoclet (although it shouldn't be to hard to script) to automatically generate transfer objects and simple entity translations.

我已经成功地使用像XDoclet这样的工具(尽管不应该难以编写脚本)来自动生成传输对象和简单的实体转换。

That said, if you believe your Entity translations are simple enough to be done using reflection, is there a reason you can't just pass the Entity objects over the wire instead of DTO's? It might be better to have a slightly custom serialization than a full blown DTO.

也就是说,如果您认为您的实体翻译很简单,可以使用反射来完成,那么您是否有理由不能通过线路而不是DTO传递实体对象?有一个稍微自定义的序列化可能比完整的DTO更好。

#4


0  

use ValueInjecter, with it you can map anything to anything e.g.

使用ValueInjecter,你可以将任何东西映射到任何东西,例如

 object <-> object
 object <-> Form/WebForm
 DataReader -> object

and it has cool features like: flattening and unflattening

它具有很酷的功能,如:平整和不平整

the download contains lots of samples (sample applications for asp.net mvc, web-forms, winforms, unit tests)

下载包含大量示例(asp.net mvc的示例应用程序,Web表单,winforms,单元测试)

#1


3  

I have been using AutoMapper recently, and it works like a charm.

我最近一直在使用AutoMapper,它就像一个魅力。

#2


3  

Pain and risk

of getting/setting properties by hand! Have you tried this: http://dozer.sourceforge.net/.

手动获取/设置属性!你试过这个:http://dozer.sourceforge.net/。

A flexible and configurable framework for translating bean to bean. Might help!

一个灵活且可配置的框架,用于将bean转换为bean。可能有帮助!

#3


0  

I've had success using tools like XDoclet (although it shouldn't be to hard to script) to automatically generate transfer objects and simple entity translations.

我已经成功地使用像XDoclet这样的工具(尽管不应该难以编写脚本)来自动生成传输对象和简单的实体转换。

That said, if you believe your Entity translations are simple enough to be done using reflection, is there a reason you can't just pass the Entity objects over the wire instead of DTO's? It might be better to have a slightly custom serialization than a full blown DTO.

也就是说,如果您认为您的实体翻译很简单,可以使用反射来完成,那么您是否有理由不能通过线路而不是DTO传递实体对象?有一个稍微自定义的序列化可能比完整的DTO更好。

#4


0  

use ValueInjecter, with it you can map anything to anything e.g.

使用ValueInjecter,你可以将任何东西映射到任何东西,例如

 object <-> object
 object <-> Form/WebForm
 DataReader -> object

and it has cool features like: flattening and unflattening

它具有很酷的功能,如:平整和不平整

the download contains lots of samples (sample applications for asp.net mvc, web-forms, winforms, unit tests)

下载包含大量示例(asp.net mvc的示例应用程序,Web表单,winforms,单元测试)