在 WCF 合约中添加泛型类型的 AutoMapper 类型映射约定

发布于 2024-10-17 17:44:11 字数 1032 浏览 0 评论 0原文

例如,我有一个在其数据协定中使用泛型的 WCF 服务(简化):

public GetDetails(StatusField<string> status);

现在,WCF 通过为泛型中 T 的每个可能值创建非泛型等效类型来支持泛型。因此,对于上面的示例,使用 WCF 服务的客户端将看到上述函数的以下签名:

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

现在客户端拥有 StatusField 类的通用版本的副本。我们希望在客户端中使用 AutoMapper,在这个通用 StatusField 和 WCF 上面生成的类型(例如 stringStatusField)之间进行映射,以便我们可以调用该服务。我们可以通过在客户端启动时手动创建映射来完成此操作,如下所示:

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

但是,这很费力,因为 WCF 已转换了 50 多个可能的值。扩展这个想法,我们可以使用反射来自动为所有类型创建映射,这就是我们当前正在使用的解决方案。

理想情况下,我希望看到的是一个与 AutoMapper 架构相关联的解决方案,以避免手动进行反射。从概念上讲,这需要某种方式定义 AutoMapper 用来将两种类型绑定在一起的约定,类似于它允许在匹配属性时指定自定义约定的方式。到目前为止,我还没有找到一种方法来做到这一点,这就是我想在这里回答的问题,如果有人知道如何做到这一点,特别是与上述场景相关的问题。

顺便说一句,我知道有些人可能会认为 Mapper.DynamicMap() 作为这个问题的解决方案。首先,我们不想使用它,因为这意味着调试可能会更困难(正如其他类似文章中的一些文章所指出的那样),而且如果 StatusField 深深嵌套在传递给 WCF 方法的对象图中,我不确定该解决方案可行,并且可能会导致类型映射不正确以及其他此类问题。如果可能的话,我真的很想具体定义允许的映射。

I have a WCF service that uses generics in its data contract, for example (simplified):

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. So, for the above example, the client consuming the WCF service will see the following signature for the above function:

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

Now the client has a copy of the generic version of the StatusField class. 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. 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. 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. 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. 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. 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. I would really like to concretely define the allowable mappings if possible.

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

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

发布评论

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

评论(1

怂人 2024-10-24 17:44:11

不确定 AutoMapper 是否提供您想要的支持,但如果它提供了,它将按照您的建议使用反射。

如果您出于性能考虑而反对反射解决方案(这应该是一次性启动成本),那么也许基于 T4 模板的代码生成解决方案值得考虑?

Unsure if AutoMapper provides the support you are after, but if it did it would be using reflection as you propose.

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?

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