在 F# Web 服务中传递对象与记录
Passing objects vs records in F# web service
据我所知,我可以在 WCF F# Web 服务中传递对象和记录:
[<DataContract>]
type Item(id : string, name : string) =
[<DataMember>]
member val ItemId = id with get, set
[<DataMember>]
member val ItemName = name with get, set
new() = new Item("", "")
type Person =
{
First : string
Last : string
Age : int
}
Web 服务实现:
type SimpleService() =
interface ISimpleService with
member x.GetItems() =
let sql = new SqlConnector()
let result = sql.GetItems()
result
member x.GetPerson() =
let result = { First = "Steve"; Last = "Holt"; Age = 17 }
result
member x.GetPeople() =
let a = { First = "Steve"; Last = "Holt"; Age = 17 }
let b = { First = "Dan"; Last = "Woller"; Age = 34 }
[| a; b |]
我只是想知道哪种方式更好 - 在 F# 服务中传递对象和传递记录?
据我所知,如果记录是用 C# 编写的,则可以在客户端将记录作为对象获取。
据我所知,我不需要注释我的记录类型即可将其作为 Web 服务的结果或参数传递。
我说得对吗?
DataContract
和 DataMember
属性 aren't required for WCF - 仅推荐(强烈)。
F# 记录在 IL 中编译为不可变 class,因此 WCF 会将其视为未注释的不可变 class。正如 Petr 在对问题本身的评论中所写,您可以将 [<CLIMutable>]
记录在案,如下所示:
[<CLIMutable>]
type Person =
{
First : string
Last : string
Age : int
}
这将使它看起来像一个可变的 class(具有无参数构造函数和可写属性)对于 F# 以外的其他代码。只要您只 send 数据(如上面的问题),您就不需要在 WCF 中使用它,但是如果您想要 receive数据(作为 XML 信息集到达),WCF 必须能够将 XML 信息集反序列化到您的记录中,并且它只能在 class 可变的情况下执行此操作。
您还可以将属性添加到您的记录中,如下所示:
[<DataContract; CLIMutable>]
type Person =
{
[<DataMember>]
First : string
[<DataMember>]
Last : string
[<DataMember>]
Age : int
}
在服务和客户端之间,没有 .NET 类型 - 只有 XML(信息集)。客户端代码是从 WSDL(XML 架构文档)生成的,因此 C# 客户端将在 Web 服务中生成 C# classes.
类型
据我所知,我可以在 WCF F# Web 服务中传递对象和记录:
[<DataContract>]
type Item(id : string, name : string) =
[<DataMember>]
member val ItemId = id with get, set
[<DataMember>]
member val ItemName = name with get, set
new() = new Item("", "")
type Person =
{
First : string
Last : string
Age : int
}
Web 服务实现:
type SimpleService() =
interface ISimpleService with
member x.GetItems() =
let sql = new SqlConnector()
let result = sql.GetItems()
result
member x.GetPerson() =
let result = { First = "Steve"; Last = "Holt"; Age = 17 }
result
member x.GetPeople() =
let a = { First = "Steve"; Last = "Holt"; Age = 17 }
let b = { First = "Dan"; Last = "Woller"; Age = 34 }
[| a; b |]
我只是想知道哪种方式更好 - 在 F# 服务中传递对象和传递记录?
据我所知,如果记录是用 C# 编写的,则可以在客户端将记录作为对象获取。
据我所知,我不需要注释我的记录类型即可将其作为 Web 服务的结果或参数传递。
我说得对吗?
DataContract
和 DataMember
属性 aren't required for WCF - 仅推荐(强烈)。
F# 记录在 IL 中编译为不可变 class,因此 WCF 会将其视为未注释的不可变 class。正如 Petr 在对问题本身的评论中所写,您可以将 [<CLIMutable>]
记录在案,如下所示:
[<CLIMutable>]
type Person =
{
First : string
Last : string
Age : int
}
这将使它看起来像一个可变的 class(具有无参数构造函数和可写属性)对于 F# 以外的其他代码。只要您只 send 数据(如上面的问题),您就不需要在 WCF 中使用它,但是如果您想要 receive数据(作为 XML 信息集到达),WCF 必须能够将 XML 信息集反序列化到您的记录中,并且它只能在 class 可变的情况下执行此操作。
您还可以将属性添加到您的记录中,如下所示:
[<DataContract; CLIMutable>]
type Person =
{
[<DataMember>]
First : string
[<DataMember>]
Last : string
[<DataMember>]
Age : int
}
在服务和客户端之间,没有 .NET 类型 - 只有 XML(信息集)。客户端代码是从 WSDL(XML 架构文档)生成的,因此 C# 客户端将在 Web 服务中生成 C# classes.
类型