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

  • Home
  • SEARCH
  • 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 994269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:32:59+00:00 2026-05-16T06:32:59+00:00

I’m in the process of building my first web application using ASP.NET MVC 2

  • 0

I’m in the process of building my first web application using ASP.NET MVC 2 and the Entity Framework. I am using the repository pattern. From information I have gathered on Stack Overflow and other websites it appears that the consensus is to have one one controller per domain model object and one repository per aggregate root object.

The question I have is how I should be accessing non-root aggregate objects through the aggregate root repository. In the example I am dealing with there is a Customer model and a Boat model. Boats can only exist with a FK reference to a Customer and Boats only need to be referenced within the context of a Customer. This led me to believe that I had an aggregate with these two objects and that the Customer was the root.

Now in my Boats controller I have an Edit action method:

public class BoatsController : Controller
{
    private ICustomersRepository customersRepository;
    public BoatsController(ICustomersRepository customersRepository)
    {
        this.customersRepository = customersRepository;
    }   
    public JsonResult Edit(int id, FormCollection collection)
    {
        var boat = customersRepository.GetBoat(id);
        // Update boat
    }
}

My question is how do I want to retrieve the Boat object in the repository?

public class SQLCustomersRepository : ICustomersRepository
{
    DatabaseEntities db = new DatabaseEntities();

    public Boat GetBoat(int id)
    {
        return db.Boats.SingleOrDefault(x => x.ID == id);

        // OR

        return db.Customers.Where(x => x.Boats.Any(y => y.ID == id)
                           .Select(x => x.Boats.FirstOrDefault(y => y.ID == id);
    }
}

Is it acceptable for me to reference db.Boats from the Customers Repository? It seems to be the cleanest, but it would mean that I would have to alter my FakeCustomersRepository to have a List of Customers AND Boats.

It seemed more reasonable to me to access the Boat through db.Customers, however I couldn’t figure out how to return a single Boat object without repeating myself in the LINQ query which really didn’t sit well with me.

I know I still have a lot to learn so hopefully you can point me in the right direction!

Thanks!

  • 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-16T06:32:59+00:00Added an answer on May 16, 2026 at 6:32 am

    In my opinion you are talking about implementation details, not about your abstract interfaces and your model. Your interface seems to look like this:

    public interface ICustomersRepository
    {
        //... other methods
        Boat GetBoat(int id);
        //... other methods
    }
    

    and the model like this:

    public class Customer
    {
        //... other stuff
        ICollection<Boat> Boats; // or another collection type
        //... other stuff
    }
    

    I my opinion it isn’t against any design rules to implement the ICustomerRepository completely different for your SQLCustomersRepository and for your FakeCustomersRepository and to find an implementation which is optimal with respect to your underlying data storage.

    Since you seem to have a primary key on the Boat table in your SQL-DB I would definitely take advantage of this and implement the first option: return db.Boats.SingleOrDefault(x => x.ID == id);

    But it’s not necessary to have a Boat collection in your FakeCustomersRepository. If it is easier for you just to have a list of Customers and to populate the Boats collection in each Customer with test boats, why not? In this case your GetBoat implementation in the FakeCustomersRepository could be implemented with the second LINQ query you described.

    Keep in mind that Unit testing in MVC doesn’t intend to test your specific Repository implementations but your business logic, and a business action is more a chain of various domain object and repository method calls. If a single repository method does what it is supposed to do (for instance if GetBoat really returns a boat with the correct ID) is more a matter of a later integration test to prove that your database calls work properly.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.