Using DTOs and BOs

ε祈祈猫儿з 提交于 2019-12-05 14:22:49

It depends on what you wish to achieve. I can tell you what I do myself - I have both the DTO and the BO mapped in NHibernate, but the DTOs are mapped as immutable, so I don't inadvertedly update the database without using a BO.

All queries accessible in the WebServices return/accept DTOs.

Whenever updating from a DTO, I do a UnitOfWork, where I load the BO, update the properties from the DTO and save it again if it is still valid.

On the client, I create the BO from the DTO (AutoMapper is definetly a valid choice here) whenever the client needs to modify it. The BO has a ctor that takes all arguments to create it similar to what NHibernate would do.

The benefits are: * Total control over the amount of data that goes over the wire (The DTO's are typically flattened, so only the Id of associated classes are sent in the first call). * I don't have to have the same properties in both * I can mix and match lazy-loading as I wish * I can utilize scalar queries and other calculated properties in the DTOs without creating them in the BO. * I can have several different DTOs per BO for different scenarios.

So, I guess that would qualify as mixing and matching, but with clear guidelines where I do which :-)

Hope this helps.

Maybe you will find this: http://automapper.codeplex.com/ useful too.

I know this is a quite old question, but let me give my "ten cents" about this subject.

When working with with any MVC project (even with Entity Framework or NHibernate), I use POCO for persistence and DTO/ViewModels for intermediate work, because of the flattened behaviour, less data on wire, and last (but not least valuable), they do not expose relations between objects in any way.

I know this may sound like a "silver bullet", but at least it's working for a while for me.

(forgive some english mistakes =) )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!