NHibernate 和 NHibernate WCF 3.0版本

发布于 2024-08-27 21:09:33 字数 180 浏览 3 评论 0原文

我刚刚开始一个新项目,需要 WCF 服务来处理分布式环境。我仍在努力寻找实现事物的最佳方法。

我想使用 NHibernate,但我已经看到了几种不同的方法来解决序列化问题。 3.0有处理这个问题吗?我注意到卡车内的 wcf_context :D

如果没有处理,有人可以指出我正确的方向吗?

谢谢大家

I've just started a new project which requires a WCF service to handle a distributed environment. I'm still trying to find the best way to implement things.

I want to use NHibernate, but I've seen a few different ways to address the serialization. Is this handled in 3.0? I noticed wcf_context inside the truck :D

If it isn't handled could someone point me in the right direction?

Thanks everyone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-09-03 21:09:33

通常,如果要从服务返回数据,则需要返回特定于服务目的的类,其中包含与该服务调用相关的内容、DTO(数据传输对象)或 WCF 中的 DataContract世界。

对于实体和 DTO 之间的映射特别有用的一种工具是 AutoMapper。无论您使用 AutoMapper 还是仅使用“左右”编码,这都将防止延迟加载/延迟执行问题,因为进行映射将导致执行发生。

从服务返回实体可能不是一个好主意的原因有很多,这里有一些(对其中大部分有不同的意见)

  • 根据您的坚持(在您的情况下为 nhib),您可能会出现以下行为: (延迟执行)或附加到实体的状态无法在另一个应用程序或服务器中正确执行
  • 返回服务层中的实体结果通常会导致非常类似于 CRUD 的调用,导致服务层非常繁琐,而且非常不方便-SOA
  • 不同的调用可能需要比实体更多或更少的数据,DTO 使您能够准确地包装您需要的数据,而不是您不需要的数据。
  • 如果您尝试构建可重用的服务层,则不应假设您的客户端可以访问除服务中的内容之外的实体或域逻辑。它们可以用另一种应用程序、另一种语言等编写。如果您的实体是您用来移动数据的实体,您可能会忘记这一点。

Typically if you're going to return data from a service, you'll want to return a class specific for the purpose of the service, containing what is pertinent to that service call, a DTO (Data transfer object) or DataContract in the WCF world.

One tool that is particularly helpful for mapping between entities and DTO's is AutoMapper. Whether you use AutoMapper or just "left-right" coding, this will prevent the lazy loading / delayed execution issue because doing the mapping will cause the execution to occur.

There are a number of reasons why it might not be a good idea to return an entity from a service, here are a few (there are varying opinions on most of this)

  • Depending on your persistence (in your case nhib) you may have behavior (delayed execution) or state attached to your entities which won't execute correctly in another app or server
  • Returning entity results in a service layer often results in calls that are very CRUD-like, results in a very chatty service layer, and very un-SOA
  • Different calls might require more or less data than just what's an entity, a DTO gives you the ability to wrap up exactly what you need, and nothing you don't.
  • If you're trying to build a reusable service layer, you should not assume your clients have access to your entity or domain logic other than what's in your service. they could be written in another app, another language, etc. If your entities are what you'r using to move data around, you'll be inclined to forget this.
樱&纷飞 2024-09-03 21:09:33

您无法使用 WCF 传递延迟加载的对象。

有一些方法可以解决这个问题,但是有一个错误将在下一个 WCF 版本(即将推出,2010 年 4 月)中修复。

除此之外,只要您使用正确的 DataContract 定义对象,它们就会愉快地一起离开。

列表的序列化还存在一个问题 - 您需要使用带有特定标志的 svcutil 生成代理,否则会发生不好的事情(列表变成数组,并且您无法添加更多项目)(除非您使用某种类型的列表) WCF 和 NHibernate 都同意) - 查找(Nhiberate 和 WCF 列表) -

You can't pass Lazy loaded object using WCF.

there are some ways around it, but there is a bug which will be fixed in the next WCF version (coming soon, apr 2010)

Other than that, they leave happily together, as long as you define the objects with the rigth DataContract.

There is also an issue in serialization of Lists - You need to generate the proxy using svcutil with a certain flag or bad things happens (the lists becomes arrays and you can't add more items) (unless you use a certain kind of list that both WCF and NHibernate agrees) - look that up (Nhiberate and WCF Lists) -

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文