使用 NoRM 从 F# 访问 MongoDB
从 F# 测试 NoRM https://github.com/atheken/NoRM 并尝试找到一种好方法使用它。这是基本的 C#:
class products
{
public ObjectId _id { get; set; }
public string name { get; set; }
}
using (var c = Mongo.Create("mongodb://127.0.0.1:27017/test"))
{
var col = c.GetCollection<products>();
var res = col.Find();
Console.WriteLine(res.Count().ToString());
}
这工作正常,但以下是我从 F# 访问它的方法:
type products() =
inherit System.Object()
let mutable id = new ObjectId()
let mutable _name = ""
member x._id with get() = id and set(v) = id <- v
member x.name with get() = _name and set(v) = _name <- v
是否有更简单的方法来创建类或类型以传递给泛型方法?
它的名字如下:
use db = Mongo.Create("mongodb://127.0.0.1:27017/test")
let col = db.GetCollection<products>()
let count = col.Find() |> Seq.length
printfn "%d" count
Testing out NoRM https://github.com/atheken/NoRM from F# and trying to find a nice way to use it. Here is the basic C#:
class products
{
public ObjectId _id { get; set; }
public string name { get; set; }
}
using (var c = Mongo.Create("mongodb://127.0.0.1:27017/test"))
{
var col = c.GetCollection<products>();
var res = col.Find();
Console.WriteLine(res.Count().ToString());
}
This works OK but here is how I access it from F#:
type products() =
inherit System.Object()
let mutable id = new ObjectId()
let mutable _name = ""
member x._id with get() = id and set(v) = id <- v
member x.name with get() = _name and set(v) = _name <- v
Is there an easier way to create a class or type to pass to a generic method?
Here is how it is called:
use db = Mongo.Create("mongodb://127.0.0.1:27017/test")
let col = db.GetCollection<products>()
let count = col.Find() |> Seq.length
printfn "%d" count
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试过记录类型吗?
我不知道它是否有效,但是当您只需要一个基本上是“一组字段”的类时,记录通常很好。
Have you tried a record type?
I don't know if it works, but records are often good when you just need a class that is basically 'a set of fields'.
出于好奇,您可以尝试向记录添加无参数构造函数。这绝对是一个 hack - 事实上,它使用了 F# 编译器中的一个错误 - 但它可能会起作用:
member
声明添加了一个具有用于构造函数的特殊 .NET 名称的成员,因此.NET认为它是一个构造函数。我会非常小心地使用它,但它可能适用于您的场景,因为该成员通过反射显示为构造函数。如果这是获得适用于 MongoDB 等库的简洁类型声明的唯一方法,那么它将有望激励 F# 团队在该语言的未来版本中解决该问题(例如,我可以轻松想象一些特殊属性会强制 F#编译器添加无参数构造函数)。
Just out of curiosity, you can try adding a parameter-less constructor to a record. This is definitely a hack - in fact, it is using a bug in the F# compiler - but it may work:
The
member
declaration adds a member with a special .NET name that is used for constructors, so .NET thinks it is a constructor. I'd be very careful about using this, but it may work in your scenario, because the member appears as a constructor via Reflection.If this is the only way to get succinct type declaration that works with libraries like MongoDB, then it will hopefuly motivate the F# team to solve the problem in the future version of the language (e.g. I could easily imagine some special attribute that would force F# compiler to add parameterless constructor).
这是一个非常简单的方法来定义一个接近 C# 定义的类:它有一个默认构造函数,但使用公共字段而不是 getter 和 setter,这可能是一个问题(我不知道)。
或者,如果您可以使用字段的默认值(在本例中,全部为空):
Here is a pretty light way to define a class close to your C# definition: it has a default constructor but uses public fields instead of getters and setters which might be a problem (I don't know).
or, if you can use default values for your fields (in this case, all null):