使用 NoRM 从 F# 访问 MongoDB

发布于 2024-10-04 22:57:49 字数 973 浏览 3 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-10-11 22:57:49

您尝试过记录类型吗?

type products = {
    mutable _id : ObjectId
    mutable name : string
    }

我不知道它是否有效,但是当您只需要一个基本上是“一组字段”的类时,记录通常很好。

Have you tried a record type?

type products = {
    mutable _id : ObjectId
    mutable name : string
    }

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

筑梦 2024-10-11 22:57:49

出于好奇,您可以尝试向记录添加无参数构造函数。这绝对是一个 hack - 事实上,它使用了 F# 编译器中的一个错误 - 但它可能会起作用:

type Products = 
  { mutable _id : ObjectId
    mutable name : string }
  // Horrible hack: Add member that looks like constructor 
  member x.``.ctor``() = ()

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:

type Products = 
  { mutable _id : ObjectId
    mutable name : string }
  // Horrible hack: Add member that looks like constructor 
  member x.``.ctor``() = ()

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

玉环 2024-10-11 22:57:49

这是一个非常简单的方法来定义一个接近 C# 定义的类:它有一个默认构造函数,但使用公共字段而不是 getter 和 setter,这可能是一个问题(我不知道)。

type products =
    val mutable _id: ObjectId
    val mutable name: string
    new() = {_id = ObjectId() ; name = ""}

或者,如果您可以使用字段的默认值(在本例中,全部为空):

type products() =
    [<DefaultValue>] val mutable _id: ObjectId
    [<DefaultValue>] val mutable name: string

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

type products =
    val mutable _id: ObjectId
    val mutable name: string
    new() = {_id = ObjectId() ; name = ""}

or, if you can use default values for your fields (in this case, all null):

type products() =
    [<DefaultValue>] val mutable _id: ObjectId
    [<DefaultValue>] val mutable name: string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文