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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:16:13+00:00 2026-05-13T10:16:13+00:00

I have a simply Class that is intended to be a simple POCO –

  • 0

I have a simply Class that is intended to be a simple POCO – it just holds data. With one exception: It contains a Collection of Notes. I want to lazy-load this collection so that I don’t have to fetch the Notes on Pages that don’t need them. The stub for this is this:

public class MyDTOClass 
{
    private ICollection<Note> _notes = null;

    public ICollection<Note> Notes
    {
        get
        {
            if(_notes == null)
            {
                // Get an INoteRepository and initialize the collection
            }
            return _notes;
        }
    }
}

Now, I’m wondering how to proceed from here. It’s an ASP.net MVC application and I use Dependency Injection to inject the IRepositories in classes that need them, for example my controllers. But as this class here is supposed to be a really simple DTO, I’m reluctant to inject an INoteRepository into it, also because the caller shouldn’t worry or care about the fact that this is lazy-loaded.

So I’m thinking of having another Class in my Model that holds a INoteRepository.

public class MyDataAccessClass
{
    private INoteRepository _noteRepo;

    // Inject is part of Ninject and makes sure I pass the correct
    // INoteRepository automatically
    [Inject]
    public MyDataAccessClass(INoteRepository noteRepository)
    {
        _noteRepo = noteRepository;
    }

    public IEnumerable<Note> GetNotes(int projectId)
    {
        return _noteRepo.GetNotes(projectId);
    }
}

This would work of course, but I wonder if this is the correct architecture? I couple the simple DTOClass to another Data Access class and possibly also to my DI mechanism (as I need to create an Instance of the Data Access class in the getter of Notes).

Would you do it differently? Is there a better way to do this, also keeping in mind I already use Ninject?

I’m guessing that this is not a POCO or DTO anymore as it now contains logic, but that’s okay. I want it to appear like a POCO to outside caller so I like to have a Property “Notes” rather than methods like “GetNotesForProject” on this or other classes.

My current solution is really ugly, as I need to get the Ninject Kernel from my MvcApplication and use it to spin up the ProjectDataProvider class which takes an INoteRepository in it’s constructor, to avoid having to put the INoteRepository somewhere in my “DTO”-Class:

public ICollection<Note> Notes
{
    get
    {
        if(_notes == null)
        {
            var app = HttpContext.Current.ApplicationInstance as MvcApplication;
            if (app == null)
             throw new InvalidOperationException("Application couldn't be found");
            var pdp = app.Kernel.Get<ProjectDataProvider>();
            _notes = new List<Note>(pdp.GetNotes(Id));
        }
        return _notes;
    }
}

Edit: Opened a bounty. Let’s ignore the terminology of “POCO” and “DTO”, I’ll refactor accordingly. So this is about: How should the Lazy-Loading code look in such a situation, and can/should I avoid passing INoteRepository into the MyDTOClass?

  • 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-13T10:16:13+00:00Added an answer on May 13, 2026 at 10:16 am

    Your DTO doesn’t need to know about the repository itself. All it needs is a delegate that can provide it with the value for notes.

    How about something like this:

    public class MyDTOClass
    {
        private ICollection<Note> _notes = null;
    
        public ICollection<Note> Notes
        {
            get
            {
                if (_notes == null)
                {
                    if (notesValueProvider == null)
                        throw new InvalidOperationException("ValueProvider for notes is invalid");
                    _notes = notesValueProvider();
                }
                return _notes;
            }
        }
    
        private Func<ICollection<Note>> notesValueProvider = null;
    
        public MyDTOClass(Func<ICollection<Note>> valueProvider)
        {
            notesValueProvider = valueProvider;
        }
    }
    

    Since by definition, your repository is supposed to provide you with an instance of the DTO, we should be able to pass in the value provider delegate like so:

    public class Repository
    {
        public MyDTOClass GetData()
        {
            MyDTOClass dto = new MyDTOClass(FetchNotes);
            return dto;
        }
    
        public ICollection<Note> FetchNotes()
        {
            return new List<Note>(200);
        }
    }
    

    Will this work for you?

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

Sidebar

Related Questions

I have a simple class that essentially just holds some values. I have overridden
I have a really simple class with two methods; One that will be called
I have a class that represents some file data. The file contains some headers.
Very simply put: I have a class that consists mostly of static public members,
I have a really simple Java class that effectively decorates a Map with input
I have a very simple class with only one field member (e.g. String). Is
I have a few simple scripts that are intended to daisy chain together to
I have a very simple class that opens a file and creates a memory
I have a simple timer class in a game, intended to allow me to
I have one service that simply is for starting and stopping another service I

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.