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
  • 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-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

Ask A Question

Stats

  • Questions 266k
  • Answers 266k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need 4 backslashes to represent 1 in regex because:… May 13, 2026 at 12:37 pm
  • Editorial Team
    Editorial Team added an answer Maybe you could take a look to utf8_encode() and utf8_decode() May 13, 2026 at 12:37 pm
  • Editorial Team
    Editorial Team added an answer Here it is: var text = "Java (77) Javascript (22)… May 13, 2026 at 12:37 pm

Related Questions

Suppose I want to allow to select our entity (from a dropdown, etc) on
I've defined the following generic class public class ManagedClass<T> where T : ManagedClass<T> {
Suppose the following model public class Product { private Integer requiredQuantity; private Integer allowedQuantity;
Suppose I have a class that represents a product to be priced using one

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.