c# - Using dependency injection to lazy load domain model properties -
i using repository pattern have data models behind repository return domain models. using dependency injection of this. problem is, let's have domain model student studentrepository. student may have properties on use in places, not others, 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) { // need way load courserepository _courses = courserepository.getcoursesbystudentid(id); } return _courses; } }
therefore, want type of lazy loading set not know how can elegantly, , difficult me understand how can load implementation of courserepository (or icourserepository using di), repository looks like:
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)); } }
this not critical answer, if solution relative ninject since current di container.
Comments
Post a Comment