帮助通用 LINQ 插入方法 (Norm/MongoDB)

发布于 2024-10-21 14:32:18 字数 1906 浏览 2 评论 0原文

我正在尝试编写一个通用方法来将对象作为嵌入文档插入。下面的代码已经可以工作,但我需要创建一个通用方法,可以将其放入存储库中并从控制器调用。我需要传入根 ID 和需要插入的子文档对象。

class Post
{
    ...
    public IList<Comment> Comments { get; set; }
}

using (var db = Mongo.Create(session.ConnectionString()))
{
    IMongoCollection<Post> _collection = db.GetCollection<Post>("Post");
    var comment = new Comment();
    comment.InjectFrom(commentViewModel.comment);

    // Use below to add *NEW* embedded document
    _collection.UpdateOne(
        new { _id = commentViewModel.Id },
        new { Comments = M.AddToSet(comment) }
        );
}

举个例子,这就是我用来检索文档的方法,我正在寻找类似于 insert:

// Controller
var session = new MongoSession();
var post = session.Single<Post>(c => c.Id == id);

//Repository
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
{
    T retval = default(T);
    using (var db = Mongo.Create(_connectionString))
    {
        retval = db.GetCollection<T>().AsQueryable()
                    .Where(expression).SingleOrDefault();
    }
    return retval;
}

UPDATE

的内容,请注意,调用:

_collection.UpdateOne(
            new { _id = commentViewModel.Id },
            new { Comments = M.AddToSet(comment) }
            )

可以满足我的需要,我只是希望能够做到这一点对类似内容(工作版本除外)的通用调用(由于缺乏更好的词):

//Controller
var session = new MongoSession();
session.AddSubDocument<>(new Post { Id = commentViewModel.Id }, new { Comments = M.AddToSet(comment) });

// Repository ==> NOT WORKING SAMPLE
public void AddSubDocument<X, U>(X matchDocument, U valueDocument)
{
    using (var db = Mongo.Create(_connectionString))
    {
        db.GetCollection<X>().UpdateOne(matchDocument, valueDocument);
    }
}

希望这更有意义。

谢谢你, 麦克风

I'm trying to write a generic method to inset an object as an embedded document. The below code works already but I need to make a generic method that can be put in a repository and called from the controller. I'd need to pass in the root ID and the sub-document object that needs to be inserted.

class Post
{
    ...
    public IList<Comment> Comments { get; set; }
}

using (var db = Mongo.Create(session.ConnectionString()))
{
    IMongoCollection<Post> _collection = db.GetCollection<Post>("Post");
    var comment = new Comment();
    comment.InjectFrom(commentViewModel.comment);

    // Use below to add *NEW* embedded document
    _collection.UpdateOne(
        new { _id = commentViewModel.Id },
        new { Comments = M.AddToSet(comment) }
        );
}

As an example, this is what I use to retrieve a document, I'm looking for something similar to insert:

// Controller
var session = new MongoSession();
var post = session.Single<Post>(c => c.Id == id);

//Repository
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
{
    T retval = default(T);
    using (var db = Mongo.Create(_connectionString))
    {
        retval = db.GetCollection<T>().AsQueryable()
                    .Where(expression).SingleOrDefault();
    }
    return retval;
}

UPDATE

Note that calling:

_collection.UpdateOne(
            new { _id = commentViewModel.Id },
            new { Comments = M.AddToSet(comment) }
            )

does what I need, I just want to be able to make it a generic call (for lack of a better word) to something like this (except a working version):

//Controller
var session = new MongoSession();
session.AddSubDocument<>(new Post { Id = commentViewModel.Id }, new { Comments = M.AddToSet(comment) });

// Repository ==> NOT WORKING SAMPLE
public void AddSubDocument<X, U>(X matchDocument, U valueDocument)
{
    using (var db = Mongo.Create(_connectionString))
    {
        db.GetCollection<X>().UpdateOne(matchDocument, valueDocument);
    }
}

Hope that makes more sense.

Thank you,
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

寂寞笑我太脆弱 2024-10-28 14:32:18

这应该有效

public void Insert<T>( T entity )
{
     using (var db = Mongo.Create(_connectionString))
     {
         var collection = db.GetCollection<T>();
         collection.Save( entity );
     }
}

This should work

public void Insert<T>( T entity )
{
     using (var db = Mongo.Create(_connectionString))
     {
         var collection = db.GetCollection<T>();
         collection.Save( entity );
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文