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 6193019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:02:44+00:00 2026-05-24T03:02:44+00:00

Say you have these two classes. public class Author { public int ID {get;

  • 0

Say you have these two classes.

public class Author
{
public int ID {get; set;}
public string Name{get; set;}
public virtual ICollection<Book> Books { get; set;}
}

public class Book
{
public int ID {get; set;}
public string Name{get; set;}
public int AuthorId {get; set;}
public virtual Author Author{ get; set;}
}

If I have a BooksController that has all its actions receive a AuthorId

I go to this address:

/Authors/1/Books

Does it make sense to just use a current AuthorRepository?

public ActionResult Index(int AuthorId)
    {
    return View(_AuthorRepository.GetById(AuthorId).Books)
    }

Or for the sake of keeping data access inside repositories I should create a BookRepository?

public ActionResult Index(int AuthorId)
    {
    return View(_BookRepository.GetByAuthorId(AuthorId))
    }

same thing happens with Create action. I have a hard time deciding which one makes more sense.

[HttpPost]
public ActionResult Create(int AuthorId, BookViewModel book)
{
if(ModelState.IsValid)
{
Book b= new Book();
b= AutomapperMagicMethodThatGivesMeABookFromA(book); //
_AuthorRepository.FindById(AuthorId).Books.Add(b);
_AuthorRepository.SaveChanges();
return RedirectToAction("Index");
}
return View(book)
}

Or this approach.

[HttpPost]
public ActionResult Create(int AuthorId, BookViewModel book)
{
if(ModelState.IsValid)
{
Book b= new Book();
b= AutomapperMagicMethodThatGivesMeABookFromA(book); //
b.AuthorId = AuthorId;
_BookRepository.Add(b);
_BookRepository.SaveChanges();
return RedirectToAction("Index");
}
return View(book)
}

I would appreciate some advice on handling scenarios like this.
Feel free to criticize my code as much as you want.

Please help thanks in advance.

ps. Im using EF, if it makes any difference.

  • 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-24T03:02:45+00:00Added an answer on May 24, 2026 at 3:02 am

    The general rule of thumb is to create one repository per aggregate root.

    Where an aggregate root can be thought of as like a “parent”, and a “child” cannot exist without a parent.

    In your example, the Author is the aggregate root, because a Book cannot exist without an Author. (the FK proves this).

    Therefore, you should just have a LibraryRepository, that is capable of working with both authors and books, with a single Entity Framework objectset.

    We actually enforce these aggregate boundaries in our Repository by typing the ObjectSet to the generic type on the Repository.

    So you might have:

    public class LibraryRepository : IRepository<Author>, GenericRepository<Author>
    {
       public Author FindById(int id)
       {
          return _context.SingleOrDefault(x => x.AuthorId == id);
       }
    
       public Book FindById(int bookId)
       {
          return _context
             .Where(x => x.Books.Any(y => y.BookId == bookId))
             .Select(x => x.Books.SingleOrDefault(x => x.BookId == bookId))
             .SingleOrDefault();
       }
    }
    

    In the above example, _context is a protected property in the generic repository, which is typed to ObjectSet<Author> (which is T on the Generic Repository).

    However, see how it’s kind of messy to find a book. If you find yourself needing to find a “child” on it’s own (e.g without retrieving the parent first), then you should consider creating a book repository as well.

    So it comes down to two things:

    1. Enforcing aggregate boundaries
    2. Creating repositories that serve your application

    Think about the user interface, what information does each screen need, what information does it have at hand to identify that piece of info (e.g URL).

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

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
Let's say I have these classes: public class BaseMapClass<T1, T2> { protected T1 Thing1;
Let's say I have two classes like the following: Class A { public: ..
Lets say I have a two classes: class A : public QObject {}; class
Say I have the following POCO classes: public class AuditableModel { public int ID
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
Let's say I have two classes. Each class has one parameter. Parameter of first
Say we have a class inheriting from two base classes (multiple inheritance). Base class
Say I have two classes like this: class A{ private static Random random =
Let say we have a base class and its two derived classes; The base

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.