简体   繁体   English

NHibernate和WCF 3.0版

[英]NHibernate & WCF in version 3.0

I've just started a new project which requires a WCF service to handle a distributed environment. 我刚刚开始了一个新项目,该项目需要WCF服务来处理分布式环境。 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. 我想使用NHibernate,但是我已经看到了几种解决序列化的方法。 Is this handled in 3.0? 这是在3.0中处理的吗? I noticed wcf_context inside the truck :D 我注意到卡车里面的wcf_context:D

If it isn't handled could someone point me in the right direction? 如果不处理,有人可以指出我正确的方向吗?

Thanks everyone 感谢大家

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. 通常,如果要从服务返回数据,则需要返回特定于该服务目的的类,其中包含与该服务调用有关的类,DTO(数据传输对象)或WCF中的DataContract世界。

One tool that is particularly helpful for mapping between entities and DTO's is AutoMapper . AutoMapper是对实体与DTO之间的映射特别有用的一种工具。 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. 无论您使用AutoMapper还是仅使用“左右”编码,这都可以防止延迟加载/延迟执行问题,因为执行映射将导致执行。

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 根据您的持久性(在您的情况下为nhib),您可能有行为(延迟执行)或状态附加到您的实体,而这些行为或状态无法在其他应用或服务器中正确执行
  • 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 返回实体会导致一个服务层,通常会导致呼叫非常类似于CRUD,会导致一个非常闲聊的服务层,并且会导致非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. 不同的调用可能只需要一个实体就需要更多或更少的数据,DTO使您能够准确包装所需内容,而不用不需要任何内容​​。
  • 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. 它们可能是用其他应用程序,其他语言等编写的。如果要使用实体来移动数据,那么您将倾向于忘记这一点。

You can't pass Lazy loaded object using WCF. 您不能使用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) 有一些解决方法,但是有一个错误会在下一WCF版本中修复(即将在2010年4月发布)

Other than that, they leave happily together, as long as you define the objects with the rigth DataContract. 除此之外,只要您使用严格的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) - 列表的序列化还有一个问题-您需要使用带有特定标志的svcutil生成代理,否则会发生不好的事情(列表变成数组,并且您无法添加更多项)(除非您使用某种类型的列表WCF和NHibernate都同意)-查找(Nhiberate和WCF列表)-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM