c# MongoDB (noRM) - 具有嵌入文档的存储库模式

发布于 2024-10-09 22:07:39 字数 1275 浏览 4 评论 0原文

我正在开发一个应用程序,其模型类似于 Stack Overflow(问题/答案等......) 使用 C# / ASP.net MVC 建模 NoSQL 论坛应用程序

该模型看起来像这样(简化)

class Question
{
    public string Title { get; set; }
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }

    public string UserName { get; set; }

    public List<Answer> Replies { get; set; }
}

class Answer
{
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }

    public string UserName { get; set; }
}

所以我的文档只是一个文档,其中嵌入了“答案”,

我正在尝试为这种方法设计我的存储库。

我应该有 2 个独立的存储库吗?例如:

interface IQuestionRepository
{
    void PutQuestion(Question question);
    Question GetQuestion(string questionID);
}  

interface IAnswerRepository
{
    void PutAnswer(string questionID, Answer Answer);
    Answer GetAnswer(string answerID);
}

或者是这样的:

interface IPostRepository
{
    void PutQuestion(Question question);
    Question GetQuestion(string questionID);
    void PutAnswer(string questionID, Answer Answer);
    Answer GetAnswer(string answerID);
}

I’m developing an application with a model similar to Stack Overflow (question / answer etc...)
Modelling a NoSQL Forum Application with C# / ASP.net MVC

The model looks something like this (simplified)

class Question
{
    public string Title { get; set; }
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }

    public string UserName { get; set; }

    public List<Answer> Replies { get; set; }
}

class Answer
{
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }

    public string UserName { get; set; }
}

So my documents are just one document, with the "answers" embedded in them

I’m trying to design my repositories for this approach.

Should I have 2 separate repositories? For example:

interface IQuestionRepository
{
    void PutQuestion(Question question);
    Question GetQuestion(string questionID);
}  

interface IAnswerRepository
{
    void PutAnswer(string questionID, Answer Answer);
    Answer GetAnswer(string answerID);
}

Or something like this:

interface IPostRepository
{
    void PutQuestion(Question question);
    Question GetQuestion(string questionID);
    void PutAnswer(string questionID, Answer Answer);
    Answer GetAnswer(string answerID);
}

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

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

发布评论

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

评论(3

放低过去 2024-10-16 22:07:39

你的模型本质上是有缺陷的。

问题应该是根文件。

答案应该是根文件。

虽然是针对 RavenDB 编写的,但文档建模信息大多可以直接供您使用:http://codeofrob.com/archive/2010/12/21/ravendb-document-design-with-collections.aspx

编辑: FWIW原因您的模型的缺陷在于,对于文档数据库,您希望文档对事务边界进行建模。想想堆栈溢出的编辑场景,以及与多人添加和更新答案(所有这些都改变了根文档)保持一致性将是多么一场噩梦,而发布者正在更新问题。单个对象上的争用量将非常成问题。

RavenDB 提供了他们所谓的“修补”功能,可以让您操作文档结构的一部分而不是整个文档来解决此类问题,但最好预先避免这种设计,而不是试图通过大大增加文档的复杂性来使其工作。持久化模型必须进行部分更新并处理复杂的并发情况。

要回答此后的具体问题,您将拥有一个 AnswersRepository 和一个 QuestionsRepository

Your model is inherently flawed.

Question should be a root document.

Answer should be a root document.

While written in regards to RavenDB the document modeling information is mostly directly usable by you: http://codeofrob.com/archive/2010/12/21/ravendb-document-design-with-collections.aspx

Edit: FWIW the reason why your model is flawed is with document databases you want your documents to model transaction boundaries. Think of the editing scenario with stack overflow and how much of a nightmare it would be to maintain consistency with multiple people adding and updating answers which all alter the root document, and the poster is updating the question. The amount of contention on the single object will very problematic.

RavenDB provides what they call "patching" that lets you manipulate part of a document structure instead of the entire document exactly to solve problems like this but this design is best avoided up front instead of trying to make it work by greatly increasing the complexity of your persistence model having to do partial updates and handle elaborate concurrency situations.

And to answer the specific question after this, then you would have an AnswersRepository and a QuestsionsRepository

几度春秋 2024-10-16 22:07:39

我认为最好为每个聚合路由创建存储库(仅适用于问题文档)

I think that it will be better to create repository for each aggregate rute(only for Question Document)

泪之魂 2024-10-16 22:07:39

您不需要答案的存储库。从领域的角度来看,您应该将答案添加到问题对象中。问题存储库应该完成这项工作,因为问题看起来像一个聚合根,并且您应该为每个聚合根(而不是每个实体)拥有一个存储库。

您应该小心,不要创建贫血域模型

You don't need an Answer's repository. From a domain point of view, you should just add the answer to your Question object. The question repository should do the job, as Question looks like an aggregate root and you should have a repository per aggregate root (not per entity).

You should be careful not to create a Anemic Domain Model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文