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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:44:17+00:00 2026-05-12T09:44:17+00:00

I’m trying to figure out the best way to handle loading objects with different

  • 0

I’m trying to figure out the best way to handle loading objects with different graphs (related entities) depending on the context their being used.

For example Here’s a sample of my domain objects:

public class Puzzle
{
    public Id{ get; private set; }
    public string TopicUrl { get; set; }
    public string EndTopic { get; set; }
    public IEnumerable<Solution> Solutions { get; set; }
    public IEnumerable<Vote> Votes { get; set; }
    public int SolutionCount { get; set; }
    public User User { get; set; }
}
public class Solution
{
    public int Id { get; private set; }
    public IEnumerable<Step> Steps { get; set; }
    public int UserId { get; set; }
}  
public class Step
{
    public Id { get; set; }
    public string Url { get; set; }
}
public class Vote
{
    public id Id { get; set; }
    public int UserId { get; set; }
    public int VoteType { get; set; }
}

What I’m trying to understand is how to load this information differently depending on how I’m using it.

For example, on the front page I have a list of all puzzles. At this point I don’t really care about the solutions for the puzzle or for the steps in those solutions (which can get pretty hefty). All I want are the puzzles. I would load them from my controller like this:

public ActionResult Index(/*  parameters   */)
{
    ...
    var puzzles = _puzzleService.GetPuzzles();
    return View(puzzles);
}

Later on for the puzzle view I now care about only the solutions for the current user. I don’t want to load the entire graph with all of the solutions and all of the steps.

public ActionResult Display(int puzzleId)
{
   var puzzle = _accountService.GetPuzzleById(puzzleId);
   //I want to be able to access my solutions, steps, and votes. just for the current user.
}

Inside my IPuzzleService, my methods look like this:

public IEnumerable<Puzzle> GetPuzzles()
{
    using(_repository.OpenSession())
    {
        _repository.All<Puzzle>().ToList();
    }
}
public Puzzle GetPuzzleById(int puzzleId)
{
    using(_repository.OpenSession())
    {
        _repository.All<Puzzle>().Where(x => x.Id == puzzleId).SingleOrDefault();
    }
}

Lazy loading doesn’t really work in the real world, because my session is being disposed right after each unit of work. My controllers don’t have any concept of the repository and therefore do not manage session state and can’t hold on to it until the view is rendered.

I’m trying to figure out what the right pattern to use here is. Do I have different overloads on my service like GetPuzzleWithSolutionsAndVotes or more view specific like GetPuzzlesForDisplayView and GetPuzzlesForListView?

Am I making sense? Am I way off base? Please help.

  • 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-12T09:44:17+00:00Added an answer on May 12, 2026 at 9:44 am

    I had a similar case where I could not use Lazy loading.

    If you only need a one or two cases, then the easiest thing as you suggest, create separate GetPuzleWithXYZ() methods.

    You could also create a small query object with a fluent interface.

    Something like…

    public interface IPuzzleQuery
    {
        IPuzzleLoadWith IdEquals(int id);
    }
    
    public interface IPuzzleLoadWith
    {
        ISolutionLoadWith WithSolutions();
    
        IPuzzleLoadWith WithVotes();
    }
    
    public interface ISolutionLoadWith
    {
        IPuzzleLoadWith AndSteps();
    }
    
    public class PuzzleQueryExpressionBuilder : IPuzzleQuery, IPuzzleLoadWith, ISolutionLoadWith
    {
        public int Id { get; private set; }
        public bool LoadSolutions { get; private set; }
        public bool LoadVotes { get; private set; }
        public bool LoadSteps { get; private set; }
    
        public IPuzzleLoadWith IdEquals(int id)
        { 
            Id = id;
            return this;    
        }
    
        public ISolutionLoadWith WithSolutions()
        {
            LoadSolutions = true;
            return this;
        }
    
        public IPuzzleLoadWith WithVotes()
        {
            LoadVotes = true;
            return this;
        }
    
        public IPuzzleLoadWith AndSteps()
        {
            LoadSteps = true;
            return this;
        }
    }
    

    then your Repository Get() method can instantiate the expression builder and pass it to the caller

    public Puzzle Get(Action<IPuzzleQuery> expression)
    {
        var criteria = new PuzzleQueryExpressionBuilder();
    
        expression(criteria);
    
        var query = _repository.All<Puzzle>().Where(x => x.Id == criteria.Id)
    
        if(criteria.LoadSolutions) ....
    
        if(criteria.LoadSteps) ....
    
        if(criteria.LoadVotes) ....
    
        ...
        ... 
    
        return query.FirstOrDefault();
    }
    

    and typical calls would look like…

    Puzzle myPuzzle = Repository.Get(where => where.IdEquals(101).WithSolutions());
    
    Puzzle myPuzzle = Repository.Get(where => where.IdEquals(101).WithSolutions().AndSteps());
    
    Puzzle myPuzzle = Repository.Get(where => where.IdEquals(101).WithVotes().WithSolutions());
    

    it needs a little work, but you can see the basic idea.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is for taking a modulus. Basically, it is an… May 12, 2026 at 2:46 pm
  • Editorial Team
    Editorial Team added an answer If you are new to PHP and wordpress then you… May 12, 2026 at 2:46 pm
  • Editorial Team
    Editorial Team added an answer Can you use the ValidationGroup property on the validators to… May 12, 2026 at 2:46 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

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.