I am currently using a repository template where I have data models behind the repository that return my domain models. I use dependency injection to do all this. My problem: let's say I have a Student domain model that I get from my StudentRepository. A student may have properties on it that I use in some places, but not others, for example, Courses:
public class Student
{
public int ID { get; private set; }
public string Name { get; private set; }
public Student(int id, string name, IEnumberable courses = null)
{
ID = id;
Name = name;
_courses = courses;
}
private IEnumerable<Course> _courses;
public IEnumerable<Course> Courses()
{
if(_courses == null)
{
_courses = CourseRepository.GetCoursesByStudentID(ID);
}
return _courses;
}
}
So I want to install this type of lazy loading, but I don’t know how I can do it elegantly, and it’s hard for me to understand how I can load the implementation of CourseRepository (or ICourseRepository using DI), where my repository looks like this:
public class CourseRepository : ICourseRepository
{
private DbContext _db;
public CourseRepository(DbContext db)
{
_db = db;
}
public IEnumerable<DomainModels.Course> GetCoursesByStudentID(int studentID)
{
return CourseFactory.Build(_db.Courses.Where(c=>c.StudentID == studentID));
}
}
, , Ninject, DI.