I just have a class that is designed for simple POCO - it just stores data. With one exception: contains a collection of notes. I want to download this collection so that I donโt have to retrieve Notes on pages that they donโt need. To do this, do the following:
public class MyDTOClass { private ICollection<Note> _notes = null; public ICollection<Note> Notes { get { if(_notes == null) {
Now I wonder how to get out of here. This is an ASP.net MVC application, and I use Injection Dependency to inject IRepositories into the classes they need, like my controllers. But since this class should really be a simple DTO here, I donโt want to inject INoteRepository into it, also because the caller does not have to worry or worry about being lazy.
So, I think that in my model there is another class that contains an INoteRepository.
public class MyDataAccessClass { private INoteRepository _noteRepo;
This will work, of course, but I wonder if this architecture is correct? I am connecting a simple DTOClass to another data access class and possibly also to my DI mechanism (since I need to instantiate the data access class in getter Notes).
Would you do it differently? Is there a better way to do this, also bearing in mind that I'm already using Ninject?
I guess this is not POCO or DTO anymore, as it now contains logic, but it is good. I want it to appear as POCO for the external caller, so I like to have the Notes property, not methods like GetNotesForProject in this or other classes.
My current solution is really ugly, since I need to get Ninject Kernel from my MvcApplication and use it to deploy the ProjectDataProvider class, which uses the INoteRepository constructor in it 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: Open generosity. Let the terminology โPOCOโ and โDTOโ be ignored, I will reorganize accordingly. So, here's what: what should the Lazy-Loading code look like in this situation, and can / should I avoid passing INoteRepository to MyDTOClass?