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

问题描述

我有一个POCO域模型和NHibernate的存储库层的ASP.NET MVC 2应用程序。我的域模型没有我的ViewModels的认识,所以我用automapper从视图模型到实体和副/反之亦然。

I have an ASP.NET MVC 2 application with a POCO domain model and an NHibernate repository layer. My domain model has no awareness of my viewmodels so I use automapper to go from viewmodel to entity and vice/versa.

当我介绍WCF到我的项目(​​一晚的要求),我开始处理断开连接的对象。也就是说,从我与NHibernate数据库中检索一个实体,一旦该实体被序列变成断开,也不管我是否不打算使用它意味着我做了很多不必要的数据库的工作每个子集被加载。

When I introduced WCF to my project (a late requirement), I started having to deal with disconnected objects. That is, I retrieve an entity from the database with NHibernate and once that entity is serialized it becomes disconnected and each child collection is loaded regardless of whether or not I plan on using it meaning I'm doing alot of unnecessary database work.

在这读了之后,我看到它强烈建议您不要暴露你的实体域项目之外,你应该使用的DTO。

After reading up on this, I see that it is highly recommended that you not expose your entities outside of your domain project and you should instead use DTOs.

我看到这样做的原因,但我有麻烦搞清楚如何实现它。

I see the reason for this but I'm having trouble figuring out how to implement it.

我从视图模型到DTO在ASP.NET MVC映射,通过服务层发送的DTO,并从映射到DTO中的实体服务层?我应该在哪里定义我的DTO的?

Do I map from viewmodel to DTO in ASP.NET MVC, send DTOs through the service layer, and map from DTO to entity in the service layer? Where should I define my DTOs?

推荐答案

我喜欢我的服务层保持在它里面包裹的实体,和返回/只接收的DTO。我把服务合同,以及该DTO的一个单独的组件,其无论是MVC项目和服务实现的参考。

I like to have my service layer keep entities encapsulated within it, and return/receive only DTOs. I keep the service contracts as well as the DTO's in a separate assembly which both the MVC project and the Service implementation reference.

在服务调用执行,服务映射DTO的实体,那么确实有仓库和其他实体的互动,因为它需要。

Inside the service call implementation, the service maps dto's to entities, then does the interaction with repositories and other entities as it needs to.

在应用程序/ MVC项目我有时会偷懒,只使用DTO是为某些操作(尤其是那些CRUDy)的车型。如果我需要的投影或类似的东西,然后我会做一个视图模型和DTO和视图模型之间的转换与automapper等。

On the app/mvc project I sometimes will get lazy and just use DTO's as the models for certain actions (especially CRUDy ones). If i need a projection or something like that, then I'll make a viewmodel and convert between DTO and viewmodel with automapper etc.

如何暴露你的实体是非常有争议的话题。有些人会推动他们一路视图/应用层。我preFER,让他们在服务层。我发现,当实体离开服务层,你会发现自己在任何地方做生意的逻辑类型的东西,他们正在与,的东西,也许应该驻留在服务互动。

How exposed your entities are is a subject of much debate. Some people will push them all the way to the view/app layer. I prefer to keep them in the service layer. I find that when the entities leave the service layer, you find yourself doing business logic type stuff anywhere where they're interacted with, stuff that should probably reside in a service.

这篇关于域实体,DTO和视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:25
查看更多