如何在 RIA 服务中使用 NHibernate 和 DTO [英] How to use NHibernate and DTOs with RIA Services

查看:54
本文介绍了如何在 RIA 服务中使用 NHibernate 和 DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 NHibernate 与 RIA 服务和 Silverlight 4 一起使用.我创建了 DTO 以通过 RIA 服务传输数据,而不是分发我的域层对象(根据 Martin Fowler 的分布式对象设计第一定律:不要分发您的对象!").DTO 对象从域层中的五个对应层被展平为两个层.

I’m using NHibernate with RIA Services and Silverlight 4. I create DTOs for transferring the data via RIA Services rather than distributing my domain layer objects (as per Martin Fowler’s First Law of Distributed Object Design: "Don’t distribute your objects!"). The DTO objects are flattened down to two layers from five corresponding layers in the domain layer.

这是我的问题.在 Silverlight 4 中进行更改后,RIA Services 知道哪些 DTO 对象已被修改,但在服务器端更新代码中,我需要将更改传输回真实"的域层对象,以便 NHibernate 可以将这些更改应用回数据库.这样做的最佳方法是什么?

Here’s my problem. After making changes in Silverlight 4, RIA Services knows which DTO objects have been modified, but in the server-side update code I need to transfer the changes back to the "real" domain layer objects so that NHibernate can apply these changes back to the database. What’s the best way to do this?

由于 DTO 是轻量级的,只包含客户端所需的信息,我显然不想想要在 DTO 中嵌入相应的域对象.

Since the DTOs are intended to be lightweight, containing only the information that is needed on the client side, I obviously would not want to embed the corresponding domain objects inside the DTOs.

以下是我考虑过的几种可能性:

Here are a few of possibilities that I’ve considered:

1) 在 DTO 对象中保存对域对象的引用.只要引用被序列化并通过网络发送,不是整个引用对象,那么这可能是一种合理的方法.当然,引用在客户端是无效的,因为它们会指向不存在的内存位置,但在行程结束时,它们可以被服务器端使用.(?)

1) Hold references to the domain objects within the DTO objects. As long as only the references get serialized and sent across the wire, not the entire referenced objects, then this might be a reasonable approach. Of course, the references wouldn’t be valid on the client side because they would point to non-existent memory locations, but at the end of the trip they could be used server side. (?)

2) 同上,但只在 DTO 对象中保存对域聚合根的引用.然后使用对象关系遍历来获取其他相关的域对象.

2) Same as above but only save a reference to the domain aggregate root in the DTO object. Then use object relationship traversal to get to the other related domain objects.

3) 将域对象的 ID 存储在 DTO 中,并使用 NHibernate 的按 ID 获取"或按 ID"功能检索正确的域对象,以便应用更新.

3) Store the IDs of the domain objects in the DTOs and use NHibernate’s "Get" by ID or "Load" by ID functionality to retrieve the correct domain objects so that the updates can be applied.

4) 同上,但只对聚合根使用Get"或Load",然后对所有相关对象使用遍历.

4) Same as above but only use the "Get" or "Load" for the aggregate root and then use traversal for all related objects.

也许以上都不是理想的,有更好的方法......

Perhaps none of the above is ideal and there is a better approach…

推荐答案

每当我在 ORM 之上构建访问层时,我通常都会继续将实体的唯一键放在 DTO 中,以便对其进行跟踪,当然在添加的情况下支持 default(T) .

Whenever I build an access layer on top of ORM, I typically go ahead and put whatever the unique key is for the entity in the DTO, so that is tracked, and of course support for default(T) in the case of an add.

然后,当对象返回到服务器端时,我可以轻松地执行加载,从 DTO 编组更改的值,然后让会话保存它或执行显式保存.

Then, when the object comes back to the server side, I can easily do a Load, marshall the changed values over from the DTO and then either let the session save it or perform an explicit save.

这将是您的 3/4.

这篇关于如何在 RIA 服务中使用 NHibernate 和 DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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