What if the best way to return Option types by WCF service

那年仲夏 提交于 2020-02-04 08:22:26

问题


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.

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

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

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...


回答1:


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.

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).

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).

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.

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.

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.

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.



来源:https://stackoverflow.com/questions/28790446/what-if-the-best-way-to-return-option-types-by-wcf-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!