ServiceStack.Redis with F # does not store data. But almost the same code in C # works

I play today with F # and redis. I am using ServiceStack.redis to connect to MSOpenTech redis running on localhost. For testing purposes, I tried to save the price of bitcoin on redis with code like this:

let redis = new RedisClient("localhost") redis.FlushAll() let redisBitstamp = redis.As<BitstampLast>() let last = {Id = redisBitstamp.GetNextSequence(); Timestamp = 1386459953; Value=714.33M} redisBitstamp.Store(last) let allValues = redisBitstamp.GetAll() allValues.PrintDump() 

Unfortunately, the result of PrintDump was:

 [ { __type: "Program+BitstampLast, RedisSave", Id: 0, Timestamp: 0, Value: 0 } ] 

Just for testing, I used almost identical code in C # in the same redis instance:

 class BitstampLast { public Int64 Id { get; set; } public int Timestamp { get; set; } public decimal Value { get; set; } } class Program { static void Main(string[] args) { var redis = new RedisClient("localhost"); redis.FlushAll(); var redisBitstamp = redis.As<BitstampLast>(); var last = new BitstampLast() {Id = redisBitstamp.GetNextSequence(), Timestamp = 1386459953, Value=714.33M}; redisBitstamp.Store(last); var allValues = redisBitstamp.GetAll(); allValues.PrintDump(); } } 

And the result ...

 [ { __type: "CSharpRedis.BitstampLast, CSharpRedis", Id: 1, Timestamp: 1386459953, Value: 714.33 } ] 

So what am I missing? Why does it work in C # and not in F #?

EDIT: BitstampLast is defined as follows:

 type BitstampLast = {Id:int64; Timestamp:int; Value:decimal} 

which is wrong because it should be:

 type BitstampLast = {mutable Id:int64; mutable Timestamp:int; mutable Value:decimal} 

And now it works. Then the following questions - why should it be volatile? Can redis work with this object?

+7
c # f # servicestack c # -to-f # redis
source share
1 answer

Many of the ServiceStack libraries (for example, serialization, automatic matching, etc.) work with POCO, which have a default constructor and writable properties that are not present by default in immutable F # records.

The best way to create POCO in F # is to decorate it with the CLIMutable attribute F # 3.0 Language Feature , which creates a POCO type with public getters and seters for all properties available in C # code, but still behave like immutable types in F #, for example :

 [<CLIMutable>] type BitstampLast = { Id:int64; Timestamp:int; Value:decimal } 
+14
source share

All Articles