Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3618174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:43:05+00:00 2026-05-18T22:43:05+00:00

Having read this question and it’s answers as well as some other resources, I

  • 0

Having read this question and it’s answers as well as some other resources, I think my application has 3 aggregate roots, and as such should have 3 matching repositories but I’m unsure how the repositories should be used when I need to fetch data which is one of the other aggregate roots.

Le’s look at an example. Imagine a more simple version of Stack Overflow which has the notions of Users, Tags and Questions. In my mind, these are all aggregates roots, because we need to list and query against them, and as such should each have it’s own repository.

Given the situation where you view a user profile and you also want to display all of the questions that the user has posted, should you use the QuestionsRepository from within the UsersRepository to populate a user.Questions array?

I believe this question is more a matter of separation of concerns but it addresses the point of how repositories should interact. As I see it, it becomes very easy to produce circular references between repositories.

E.g. The UsersRepository fetches all the users questions from the QuestionsRepositiory which for each question fetches the user data using an instance of UsersRepository and so on.

Now let’s take the converse (which I just touched on a little). To list all the questions we would use the QuestionsRepository and call a GetAll() method (or similar). In this listing, we want to display the user information so we need to fetch it from the database.

Ideally in this situation, we’d want to perform a join from Questions to Users rather than getting all the questions and iterating over them fetching the user each time, giving us an N+1 situation and a load of unnecessary trips to the database.

Is the QuestionsRespository allowed to instantiate Users to perform the join? or does it have to use the UsersRepository, preventing a single trip to the database.

The obvious follow-on from this is where you’re given a Tag and need to display all the Questions and their Users. Should the TagsRepository use the QuestionsRespository to fetch all questions where tag LIKE 'tag-name' which in turn uses the UsersRepository to get the user info for each question? or is TagsRepository allowed to instantiate Questions and Users to populate the object graph?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:43:06+00:00Added an answer on May 18, 2026 at 10:43 pm

    Try to consider using Repositories as such only for changing data (commands) and having separate service / “read repository” for query operations that are read-only – and bypasses the usual repositories, going straight to database, reading the data using sql queries optimized for a particular case. I would still use the usual Repositories when you need information that you can easily get using a Repository (i.e. displaying detail about one User), but use a read-only service for more complicated scenarios (i.e. screen that shows some summary that combines data from more Aggregate Roots in rather complex way). However make sure to ALWAYS use Repositories for changing data.

    You can go even further in this and look into a pattern called CQRS – Command Query Responsibility Segregation (http://www.cqrsinfo.com) which is basically about completely separating the parts of application that changes application state from the parts of application that read / query application state), although it takes some time to get a grasp on it. (Note that CQRS is something different than CQS).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read this question Immutable or not immutable? and reading answers to my previous
After googling and landing on SO and having read this other question Is it
I was wondering, after having read this question ... he has this code: public
This is another question about well disposing objects from .NET. After having read a
Having read the answer to this question: How to upload folder in web application
I am fairly certain this is a ten penny closure question. But having read
I have a .txt file having some text. I want to read this file
Having read this past question for git, I would like to ask if there
I'm asking this question despite having read similar but not exactly what I want
I've read answers in this question and tried many ways to resolve my issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.