WCF DataContracts和基础数据结构 [英] WCF DataContracts and underlying data structures

查看:99
本文介绍了WCF DataContracts和基础数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道与通过WCF服务公开哪些对象有关的意义-我应该向我的业务实体添加WCF序列化规范,还是应该实现一个将我的业务实体映射到我想公开的DataContracts的转换器通过我的WCF服务?



现在我有不同级别的实体:DataAccess,Business和Contract。我有适当的转换器,可以将实体从DataAccess映射到业务,从业务映射到合同,反之亦然。实施和维护这些程序非常耗时且乏味。与此相关的最佳做法是什么?



如果我使用的是诸如NHibernate或Entity Framework之类的OR / M,我应该直接从ORM中公开实体还是应该以与我相同的方式对其进行抽象现在?

解决方案

通常,我认为从最佳实践的角度来看,您不应将业务对象的结构公开为数据合同,而是定义特定于数据合同的数据类并将业务转换为合同。它可能需要额外的工作,但是从关注点和保护与变更的角度出发,额外的工作可能是值得的。



Microsoft模式& Service Factory Modeling Edition实践实现了这一点,并且还提供了自动生成Business< =>合同转换器类的工具-这是一个出色的VS外接程序,并且代表了Microsoft的WCF最佳实践。 / p>

I am wondering what makes sense in relation to what objects to expose through a WCF service - should I add WCF Serialization specifications to my business entities or should I implement a converter that maps my business entities to the DataContracts that I want to expose through my WCF service?

Right now I have entities on different levels: DataAccess, Business and Contract. I have converters in place that can map entities from DataAccess to Business and from Business to Contract and vice versa. Implementing and Maintaining those is time consuming and pretty tedious. What are best practices in relation to this?

If I were using an OR/M such as NHibernate or Entity Framework should I be exposing the entities from the ORM directly or should I abstract them the same way I am doing now?

解决方案

In general, I think from a best practices standpoint, you should not expose the structure of your business objects as data contracts, but rather define "data contract-specific" classes and convert Business to Contract. It may require extra work, but from a separation of concerns and protection from change standpoint the extra work is probably worth it.

The Microsoft patterns & practices "Service Factory Modeling Edition" implements this, and also provides tooling to auto-generate Business <=> Contract converter classes -- it's an excellent VS add-in, and also represents Microsoft's best practices for WCF.

这篇关于WCF DataContracts和基础数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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