通过 ViewModel 门面 ASP.NET 解耦 WCF

发布于 2024-11-08 12:23:03 字数 1034 浏览 5 评论 0原文

我正在使用 C# 中的多个 ASP.NET 4、WCF 服务,并且想知道将我的代码与 WCF 结构解耦的最佳方法。如果这不是太多工作,因为客户可能不在乎。

我创建了一个外观层,以便我的 WebForms UI 可以使用字符串、整数和日期时间等平面简单类型与其通信。 Facade 生成并读取 WCF 所需的请求/响应对象。我读取了 Response 对象并将其转换为更简单的 Presenter 对象以供客户端使用。然而,Response对象返回一些复杂的对象类型,例如包含OrderItem的OrderHistory,其本身包含例如DeliveryAddress。

现在我应该将所有这些复杂对象重新创建为视图吗?即 OrderPresentation 包含 IEnumerable-OrderHistoryView-,其中包含 IEnumerable-OrderItemView-,其中包含 DeliveryAddressView 或者我误解了这一点?

对于一些更简单的对象,我已经能够使用 AutoMapper,但不确定如何或是否应该将其应用于嵌套的复杂对象。也许我不应该费心解耦这么多,而只需将我的外观和表示对象绑定到 WCF 模型?

请帮忙?有这方面好的教程示例吗?

我正在做一些事情:

        public static IList<OrderHistoryView> ConvertToOrderHistoryView(this OrderHistory[] orderHistory)
    {
        return Mapper.Map<OrderHistory[], IList<OrderHistoryView>>(orderHistory);
    }

            Mapper.CreateMap<OrderHistory, OrderHistoryView>()
            .ForMember(dest => dest.Items, opt => opt.MapFrom(src => src.Items));

I am consuming several ASP.NET 4, WCF services in C# and want to know the best way of decoupling my code from the WCF structure. If this is not too much work as the client probably doesn't care.

I created a facade layer so my WebForms UI can talk to it using flat simple types like string, int and datetime. The facade generates and read the Request/Response objects required by the WCF. I read the Response object and convert it into a simpler Presentation object for the client to use. However the Response object returns some complex object types, such as OrderHistory which contains OrderItem, which in itself contains for e.g. DeliveryAddress.

Now should I be recreating all these complex objects as Views? i.e. OrderPresentation contains IEnumerable-OrderHistoryView-, which contains IEnumerable-OrderItemView-, which contains DeliveryAddressView or am I misinterpreting this?

For some of the simpler objects I have been able to use AutoMapper but not sure how or if I should apply it to nested complex objects. Maybe I should not bother decoupling so much and just bind my facade and presentation objects to the WCF model?

Help please? any good tutorial examples on this?

I'm getting somewhere with:

        public static IList<OrderHistoryView> ConvertToOrderHistoryView(this OrderHistory[] orderHistory)
    {
        return Mapper.Map<OrderHistory[], IList<OrderHistoryView>>(orderHistory);
    }

and

            Mapper.CreateMap<OrderHistory, OrderHistoryView>()
            .ForMember(dest => dest.Items, opt => opt.MapFrom(src => src.Items));

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

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

发布评论

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

评论(1

萌面超妹 2024-11-15 12:23:03

如果您编写一些 DTO 类来将 WCF 中的数据转储到其中,那么您应该能够使用 AutoMapper 来处理映射。但我不太确定为什么。是否有任何现实的场景,您可以使用此应用程序,将其连接到不同的服务,而不必对客户端代码进行重大更改?

如果答案是否定的,并且您的客户非常依赖相关服务,那么您担心它只是浪费精力。我保证你的用户不会关心。

If you write some DTO classes to dump the data from WCF into, then you should be able to use AutoMapper to handle the mapping. I'm not really sure why, though. Is there any realistic scenario where you'd ever be able to take this app, connect it to a different service, and NOT have to make significant changes to the client code?

If the answer is no and your client is pretty well dependent on the services in question, then you're just wasting energy worrying about it. I guarantee that your users don't care.

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