如果通过WCF服务返回选项类型的最佳方法该怎么办 [英] What if the best way to return Option types by WCF service

查看:61
本文介绍了如果通过WCF服务返回选项类型的最佳方法该怎么办的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF方法,该方法在数据库中搜索记录,如果记录存在,则返回Some(object),否则,则返回None.

I have a WCF method which search the record in database and return Some(object) if the record exists and None if it doesn't.

我看到我无法调用通过WCF返回Type option的方法(我得到了异常).

As I see I can't call the method which returns Type option through WCF (I get exception).

以这种方式在F#中设计WCF服务的最佳方法是什么?

What is the best way to design WCF services in F# in this way?

例如,我可以返回空的Type

For example, I can return empty Type

Person
  {  
      Name = ""
      Age = 0
      // ....
  }

如果记录在数据库中不存在,但我正在寻找最好的主意...

if the record doesn't exist in DB, but I am looking for the best ideas...

推荐答案

WCF服务就像RESTful服务一样,公开了可以由远程客户端访问的API.这些客户端甚至不需要用.NET编写,但就我们所知,它们可以是Java客户端,也可以用完全不同的语言编写.

A WCF service, just like a RESTful service, exposes an API that can be accessed by remote clients. These clients need not even be written in .NET, but could be Java clients, or written in a completely different language, for all we know.

SOAP和REST的全部目的是启用那些互操作性方案,但这也意味着服务可能共享模式和协定,但不共享类(或共享任何其他类型)

The entire point of SOAP and REST is to enable those interoperability scenarios, but that also means that services may share schema and contract, but not class (or any other type, for that matter).

几年前,我写了一篇名为 At的文章边界,应用程序不是面向对象的 ,但是您可以阅读该文章,并用 Functional: 替换 Object-iented .在边界处,应用程序不起作用(编程).

A few years ago, I wrote an article called At the Boundaries, Applications are Not Object-Oriented, but you can take that article, and replace Object-Oriented with Functional: At the Boundaries, Applications are Not Functional (Programming).

即使您能以某种方式在WCF中序列化选项类型,也建议您不要这样做,因为客户端可能不理解其含义,或者能够以任何方式处理该序列化格式有意义的方式.

Even if you could somehow serialize option types in WCF, you'd be advised not to do so, because clients may not understand what it means, or be able to handle that serialized format in any meaningful way.

最后,一个选项可以被视为一个受约束的集合,其约束是它可以容纳0或1个元素.通过可互操作的服务返回数据时,无法对 constraint 进行建模,但仍可以返回集合/数组.

In the end, an option can be viewed as a constrained collection with the constraint that it can hold either 0 or 1 element. You can't model that constraint when you return data over an interoperable service, but you can still return a collection/array.

这就是我有时要做的事情:返回一个没有元素或只有一个元素的数组.大多数客户都明白这一点.

That's what I sometimes do: return an array with either no element, or a single element. Most clients understand that.

在构建RESTful服务时,您可以选择更好的选择:当资源不存在时返回 404 (未找到),但是我不确定使用SOAP来实现这一点的好方法

When building RESTful services, you have the better option of returning 404 (Not found) when a resource doesn't exist, but I'm not sure there's a good way to do this with SOAP.

但是,使用SOAP,您可以使用XSD定义数据结构,因此您可以使用

With SOAP, though, you can define data structures using XSD, so you might be able to use xsd.choice to model a data structure that can be either none or some.

这篇关于如果通过WCF服务返回选项类型的最佳方法该怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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