F#多属性CLIMutable DataContract [英] F# Multiple Attributes CLIMutable DataContract

查看:114
本文介绍了F#多属性CLIMutable DataContract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将ServiceStack.Redis与f#一起使用时,我遇到了合并属性的问题.也许我在想这个错误,但是现在我希望将我的类型序列化为JSON,但也可以传递给ServicvStack. f#类型的问题在于,没有默认的构造函数,添加到我的Redis实例中的数据是空的,那里有记录,但是里面没有任何数据.

I am running into an issue with combining attributes when using ServiceStack.Redis with f#. Maybe I am thinking about this wrong but right now I'd like my type to be seralized to JSON but also passable to ServicvStack. The issue with f# types is that there is no default constructor and this the data added to my Redis instance is emtpy, well the record is there but none of the data inside it is there.

这里是一个例子:

open System
open ServiceStack.Redis

[<CLIMutable>]
[<DataContract>]
type Car = {
    [<field: DataMember(Name="ID")>]
    ID : int64
    [<field: DataMember(Name="Make")>]
    Make : string
    [<field: DataMember(Name="Model")>]
    Model : string
}
let redisCar = redis.As<Car>()

let redisAddCar car : Car =
    let redisCar = redis.As<Car>()
    redisCar.Store({ car with ID = redisCar.GetNextSequence() })

let car1 = redisAddCar { ID = 0L; Make = "Honda"; Model = "Accord LX" }
let car2 = redisAddCar { ID = 0L; Make = "Honda"; Model = "Accord EX" }
let car3 = redisAddCar { ID = 0L; Make = "Honda"; Model = "Accord SX" }

let cars = redisCar.GetAll()
cars |> Seq.iter (fun car -> printfn "ID: %i, Make: %s, Model: %s" car.ID car.Make car.Model)

这将打印出来:

ID: 0, Make: , Model:
ID: 0, Make: , Model:
ID: 0, Make: , Model:

但是,如果我将类型更改为此:

However if I change the type to this:

[<CLIMutable>]
[<DataContract>]
type Car = {
    [<field: DataMember(Name="ID")>]
    mutable ID : int64
    [<field: DataMember(Name="Make")>]
    mutable Make : string
    [<field: DataMember(Name="Model")>]
    mutable Model : string
}

然后它将打印出:

ID: 1, Make: Honda, Model: Accord LX
ID: 2, Make: Honda, Model: Accord EX
ID: 3, Make: Honda, Model: Accord SX

为什么即使通过属性定义了可变属性,也必须向每个属性添加可变属性?我希望它不可变,因为我不希望状态改变.也许我考虑得太多了,应该只创建一个新类型并将不可变类型转换为可变类型,以便可以在ServiceStack.Redis中使用它.

Why do I have to add mutable to each property even if it was defined through the attribute? I'd prefer it to not be mutable, as I don't want the state to change. Maybe I am thinking about this too much and should just make a new type and translate the immutable type to the mutable type so it can be consumed with ServiceStack.Redis?

推荐答案

CLIMutable属性不会影响记录行为.对于F#代码,它仍然是不可变的记录.参见此处: http://blogs.msdn.com/b/fsharpteam/archive/2012/07/19/more-about-fsharp-3.0-language-features.aspx

CLIMutable attribute doesn't affect record behavior when record is used from F# code. For F# code it is still immutable record. See here: http://blogs.msdn.com/b/fsharpteam/archive/2012/07/19/more-about-fsharp-3.0-language-features.aspx

"在F#3.0中,我们添加了CLIMutableAttribute.如果您附加了此内容 属性设置为F#记录类型,则F#编译器会发出默认值 此类型的生成的IL中的构造方法和属性设置方法 (尽管这些功能没有公开给F#代码使用."

"In F# 3.0, we’ve added CLIMutableAttribute. If you attach this attribute to an F# record type, then the F# compiler emits a default constructor and property setters into the generated IL for this type (though those features are not exposed to F# code)."

这篇关于F#多属性CLIMutable DataContract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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