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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:02:37+00:00 2026-05-12T11:02:37+00:00

Suppose I have public class Product: Entity { public IList<Item> Items { get; set;

  • 0

Suppose I have

public class Product: Entity
{
   public IList<Item> Items { get; set; }
}

Suppose I want to find an item with max something… I can add the method Product.GetMaxItemSmth() and do it with Linq (from i in Items select i.smth).Max()) or with a manual loop or whatever. Now, the problem is that this will load the full collection into memory.

The correct solution will be to do a specific DB query, but domain entities do not have access to repositories, right? So either I do

productRepository.GetMaxItemSmth(product)

(which is ugly, no?), or even if entities have access to repositories, I use IProductRepository from entity

product.GetMaxItemSmth() { return Service.GetRepository<IProductRepository>().GetMaxItemSmth(); }

which is also ugly and is a duplication of code. I can even go fancy and do an extension

public static IList<Item> GetMaxItemSmth(this Product product)
{
   return Service.GetRepository<IProductRepository>().GetMaxItemSmth();
}

which is better only because it doesn’t really clutter the entity with repository… but still does method duplication.

Now, this is the problem of whether to use product.GetMaxItemSmth() or productRepository.GetMaxItemSmth(product)… again. Did I miss something in DDD? What is the correct way here? Just use productRepository.GetMaxItemSmth(product)? Is this what everyone uses and are happy with?

I just don’t feel it is right… if I can’t access a product’s Items from the product itself, why do I need this collection in Product at all??? And then, can Product do anything useful if it can’t use specific queries and access its collections without performance hits?

Of course, I can use a less efficient way and never mind, and when it’s slow I’ll inject repository calls into entities as an optimization… but even this doesn’t sound right, does it?

One thing to mention, maybe it’s not quite DDD… but I need IList in Product in order to get my DB schema generated with Fluent NHibernate. Feel free to answer in pure DDD context, though.

UPDATE: a very interesting option is described here: http://devlicio.us/blogs/billy_mccafferty/archive/2007/12/03/custom-collections-with-nhibernate-part-i-the-basics.aspx, not only to deal with DB-related collection queries, but also can help with collection access control.

  • 1 1 Answer
  • 1 View
  • 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-12T11:02:37+00:00Added an answer on May 12, 2026 at 11:02 am

    Having an Items collection and having GetXXX() methods are both correct.

    To be pure, your Entities shouldn’t have direct access to Repositories. However, they can have an indirect reference via a Query Specification. Check out page 229 of Eric Evans’ book. Something like this:

    public class Product
    {
        public IList<Item> Items {get;}
    
        public int GetMaxItemSmth()
        {
            return new ProductItemQuerySpecifications().GetMaxSomething(this);
        }
    }
    
    public class ProductItemQuerySpecifications()
    {
       public int GetMaxSomething(product)
       {
          var respository = MyContainer.Resolve<IProductRespository>();
          return respository.GetMaxSomething(product);
       }
    }
    

    How you get a reference to the Repository is your choice (DI, Service Locator, etc). Whilst this removes the direct reference between Entity and Respository, it doesn’t reduce the LoC.

    Generally, I’d only introduce it early if I knew that the number of GetXXX() methods will cause problems in the future. Otherwise, I’d leave it for a future refactoring exercise.

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

Sidebar

Related Questions

Suppose I have the following: public class MyObject { public string Name {get; set;}
Suppose I have: public class Bob { public int Value { get; set; }
Suppose I have two classes: public class Student { public int Id {get; set;}
Say Suppose you have a class public class Person { public int PesronId{get;set;} public
public class Product { public List<DateTime> FailureDates { get; set; } } I have
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
Suppose I have a class public class Foo { public Bar Bar { get;
Suppose I have following class @Entity public class Customer extends Model { @Id public
Suppose I have a class: public class Vector { public float X { get;
Suppose I have public class Parent { private IList<Child> _children; public void AddChild(Child child)

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.