简体   繁体   English

为WCF合同中的通用类型添加AutoMapper类型映射约定

[英]Adding AutoMapper Type Mapping Conventions For Generic Types in WCF Contract

I have a WCF service that uses generics in its data contract, for example (simplified): 我有一个在其数据协定中使用泛型的WCF服务,例如(简化):

public GetDetails(StatusField<string> status);

Now WCF supports generics by creating a non-generic equivalent type for every possible value of T in the generic. 现在,WCF通过为泛型中的每个可能的T值创建非泛型等效类型来支持泛型。 So, for the above example, the client consuming the WCF service will see the following signature for the above function: 因此,对于上面的示例,使用WCF服务的客户端将看到上述函数的以下签名:

public GetDetails(stringStatusField status);
//...

Now the client has a copy of the generic version of the StatusField class. 现在,客户端具有StatusField类的通用版本的副本。 We want to use AutoMapper in the client, to map between this generic StatusField and the types generated above by WCF (such as stringStatusField) so we can call the service. 我们想在客户端中使用AutoMapper,在这个通用StatusField和WCF上面生成的类型(例如stringStatusField)之间进行映射,以便我们可以调用该服务。 We could do this by manually creating the maps at client startup, like so: 我们可以通过在客户端启动时手动创建映射来完成此操作,如下所示:

Mapper.CreateMap<StatusField<string>, stringStatusField>();

However this is laborious as there are 50+ possible values of that WCF has converted. 然而,这很费力,因为WCF已经转换了50多个可能的值。 Extending this idea, we could use reflection to automatically create maps for all the types and this is the solution we are currently using. 扩展这个想法,我们可以使用反射自动为所有类型创建地图,这是我们目前使用的解决方案。

Ideally what i would like to see is a solution that ties into the architecture of AutoMapper to avoid having to do the reflection manually. 理想情况下,我希望看到的是一个与AutoMapper架构相关联的解决方案,以避免必须手动进行反射。 conceptually, this would require some way of defining a convention that AutoMapper would use to allow it to tie the two types together, similar to how it allows custom conventions to be specified when matching properties. 从概念上讲,这需要界定AutoMapper将使用以允许它以配合两种类型在一起,类似于它是如何使匹配性能时需要指定定制公约公约的一些方法。 As yet, i have not seen a way to do this and this is the question i would like answered here, if anyone knows how this can be done, specifically in relation to the above scenario. 到目前为止,我还没有看到这样做的方法,这是我想在这里回答的问题,如果有人知道如何做到这一点,特别是与上述情况有关。

BTW i am aware that some may be thinking of Mapper.DynamicMap() as a solution to this problem. BTW我知道有些人可能会想到Mapper.DynamicMap()作为这个问题的解决方案。 Firstly, we dont want to use this as it means debugging could potentially be harder (as indicated by some in other posts similar to this) and also if the StatusField is deeply nested in an object graph being passed to the WCF method, im not sure this solution would work and could potentially lead to a type being incorrectly mapped and other such issues. 首先,我们不想使用它,因为它意味着调试可能更难(如其他帖子中的一些类似的指示)以及如果StatusField深度嵌套在传递给WCF方法的对象图中,我不确定此解决方案将起作用,并可能导致类型错误映射和其他此类问题。 I would really like to concretely define the allowable mappings if possible. 如果可能的话,我真的想具体定义允许的映射。

Unsure if AutoMapper provides the support you are after, but if it did it would be using reflection as you propose. 不确定AutoMapper是否提供您所支持的支持,但如果确实如此,则会按照您的建议使用反射。

If you are opposed to the reflection solution due to performance concerns (which should be a one-time startup cost), then maybe a T4 template-based code generation solution might be worth considering? 如果由于性能问题(这应该是一次性启动成本)而反对反射解决方案,那么基于T4模板的代码生成解决方案可能值得考虑吗?

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

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