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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:57:53+00:00 2026-05-26T05:57:53+00:00

I have a domain model that includes something like this: public class Customer :

  • 0

I have a domain model that includes something like this:

public class Customer : EntityBase<Customer>, IAggregateRoot
{
    public IList<Comment> Comments { get; set; }
}

public class Comment : EntityBase<Comment>
{
    public User CreatedBy { get; set; }
    public bool Private { get; set; }
}

I have a service layer through which I retrieve these entities, and among the arguments passed to that service layer is who the requesting user is.

What I’d like to do is be able to construct a DetachedCriteria in the service layer that would limit the Comment items returned for a given customer so the user isn’t shown any comments that don’t belong to them and are marked private.

I tried doing something like this:

criteria.CreateCriteria("Comments")
    .Add(Restrictions.Or(Restrictions.Eq("Private", false),
                         Restrictions.And(Restrictions.Eq("Private", true),
                                          Restrictions.Eq("CreatedBy.Id", requestingUser.Id))));

But this doesn’t flow through to the lazy-loaded comments.

I’d prefer not to use a filter because that would require either interacting with the session (which isn’t currently exposed to the service layer) or forcing my repository to know about user context (which seems like too much logic in what should be a dumb layer). The filter is a dirty solution for other reasons, too — the logic that determines what is visible and what isn’t is more detailed than just a private flag.

I don’t want to use LINQ in the service layer to filter the collection because doing so would blow the whole lazy loading benefit in a really bad way. Lists of customers where the comments aren’t relevant would cause a storm of database calls that would be very slow. I’d rather not use LINQ in my presentation layer (an MVC app) because it seems like the wrong place for it.

Any ideas whether this is possible using the DetachedCriteria? Any other ways to accomplish this?

  • 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-26T05:57:54+00:00Added an answer on May 26, 2026 at 5:57 am

    Having the entity itself expose a different set of values for a collection property based on some external value does not seem correct to me.

    This would be better handled, either as a call to your repository service directly, or via the entity itself, by creating a method to do this specifically.

    To fit in best with your current model though, I would have the call that you currently make to get the the entities return a viewmodel rather than just the entities;

    public class PostForUser
    {
        public Post Post {get; set;}
        public User User {get; set;}
        public IList<Comment> Comments}
    }
    

    And then in your service method (I am making some guesses here)

    public PostForUser GetPost(int postId, User requestingUser){
    
       ...
    }
    

    You would then create and populate the PostForUser view model in the most efficient way, perhaps by the detached criteria, or by a single query and a DistinctRootEntity Transformer (you can leave the actual comments property to lazy load, as you probably won’t use it)

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

Sidebar

Related Questions

I have a class in my domain model root that looks like this: namespace
I have a domain model that looks like this: Instruction | \ Money Other
I have a Domain Model for Countries and States that looks like the following(see
Let's say i have a Domain Model that im trying to make compatible with
I have two tables that are considered a single entity in my domain model.
I currently have a client that wants me to 'abstract' out a domain model
My domain model is this: we have a bunch of schools as the root
I have a domain model that is currently heavily relying on inheritance, and due
I have a domain model that has the concept of an Editor and a
I have a domain model that is trivially represented by the following. IMyInterface ClassA

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.