简体   繁体   English

WCF数据合同和枚举共享

[英]WCF Data Contracts and Sharing of Enums

We currently have a WCF service that has been setup with its own DataContracts for the enumerations. 我们目前有一个WCF服务,它已经为枚举设置了自己的DataContracts。 We then have a mapping layer between the DataContract Enums and the Common Enums available in our business layer. 然后,我们在DataContract枚举和业务层中可用的公共枚举之间有一个映射层。 The same thing happens on the client end - a mapping layer between the client Enum and the Data contract Enum 同样的事情发生在客户端 - 客户端枚举和数据协定枚举之间的映射层

We have been speaking this morning about exposing our common enums through the WCF service and then onto the client and we do not know if this is a best practice or not. 我们今天早上一直在谈论通过WCF服务公开我们的常见枚举,然后到客户端,我们不知道这是否是最佳实践。 This Question therefore comes down to whether it is a good thing to allow cross cutting of concerns for enumerations that stems from our backend, through a service and into a client system or if we should continue to keep our data contracts separate from our base code library. 因此,该问题归结为是否允许交叉关注来自我们后端,通过服务和客户端系统的枚举或者我们是否应该继续将我们的数据合同与基本代码库分开是一件好事。 we are trying to achieve a best practice SOA for our service. 我们正在努力为我们的服务实现SOA的最佳实践。

what are peoples thoughts on this? 人们对此有何看法?

If you want best-practice, the current setup sounds pretty reasonable - you can manage versioning and other validation/mappings at the boundary pretty easily with a separate DTO layer. 如果您想要最佳实践,那么当前的设置听起来非常合理 - 您可以使用单独的DTO层轻松管理边界处的版本控制和其他验证/映射。

This applies doubly if you have an entire DTO layer on the boundary (rather than exposing your regular/transactional domain entities on the boundary), which it sounds like you might. 如果你在边界上有一个完整的DTO图层(而不是在边界上暴露你的常规/事务域实体),这会很有用,听起来你可能会这样。

The downside is increased maintenance, but it makes them very flexible, and avoids any unexpected issues. 缺点是增加维护,但它使它们非常灵活,并避免任何意外问题。 For example, it doesn't apply to WCF usually, but a classic error with regular web-services is adding a non-default constructor (thus removing the compiler-generated default constructor). 例如,它通常不适用于WCF,但常规 Web服务的典型错误是添加非默认构造函数(从而删除编译器生成的默认构造函数)。 Oops! 哎呀! no more web-service. 没有更多的网络服务。 There is a similar theme of bugs caused by innocent-looking changes that can be avoided by separating out the DTOs. 有一个类似的主题是由无辜的变化引起的错误,可以通过分离出DTO来避免。

I would go for having separate data contract enum at service level that maps to BL enum from version compatibility POV. 我会在服务级别使用单独的数据协定枚举,从版本兼容性POV映射到BL枚举。 This will allow in future to keep same service enum values and interpretation even if enum values from BL changes. 即使来自BL的枚举值发生变化,这将允许将来保持相同的服务枚举值和解释。 For example, you may start with 4-5 same values (of 0, 1, 2, 3, 4) at both service and business level. 例如,您可以在服务和业务级别以4-5个相同的值(0,1,2,3,4)开始。 In future, you decided to modify business enum to a flag based interpretation - so having separate service enum will allow to map those values to new business enum values (hoping that same interpretation would be available at that level) - for examle 3 will now may get mapped to 8 in business enum. 将来,您决定将业务枚举修改为基于标志的解释 - 因此,具有单独的服务枚举将允许将这些值映射到新的业务枚举值(希望在该级别可以获得相同的解释) - 对于考试3现在可以在业务枚举中映射到8。

I am currently in the same situation. 我目前处于相同的情况。 One solution is to have another thin layer you can call ServiceManager. 一种解决方案是使用另一个可以调用ServiceManager的薄层。 The methods in this layer accepts the data contract enums then this converts the data contract enum type to a BL enum type upon calling the BL methods. 此层中的方法接受数据协定枚举,然后在调用BL方法时将数据协定枚举类型转换为BL枚举类型。

But I have decided that my solution is to remove the data contract enums and just use string constants. 但我已经决定我的解决方案是删除数据契约枚举,只使用字符串常量。

Please share if you have a better solution. 如果您有更好的解决方案,请分享。

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

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