❔ Is saving data from DB into private fields is a good idea?

Lets say i have this class:
public class LessonAppService :ApplicationService,
ILessonService //implement the IBookAppService
{

private List<Lesson> _UserLessons;
}
public class LessonAppService :ApplicationService,
ILessonService //implement the IBookAppService
{

private List<Lesson> _UserLessons;
}
And this method:
public async Task<List<LessonDTO>> GetUserLessons()

{
var queryable = await _lessonRepository.WithDetailsAsync();
var lessonsQuery= queryable.Where(x=>x.CreatorId == CurrentUser.Id).OrderBy(x=>x.LessonNumber).Take(20);
var lessons = await lessonsQuery.ToListAsync();
_UserLessons = lessons;
return ObjectMapper.Map<List<Lesson>, List<LessonDTO>>(lessons);

}
public async Task<List<LessonDTO>> GetUserLessons()

{
var queryable = await _lessonRepository.WithDetailsAsync();
var lessonsQuery= queryable.Where(x=>x.CreatorId == CurrentUser.Id).OrderBy(x=>x.LessonNumber).Take(20);
var lessons = await lessonsQuery.ToListAsync();
_UserLessons = lessons;
return ObjectMapper.Map<List<Lesson>, List<LessonDTO>>(lessons);

}
Would it be good to operate on that private field of entites instead of making additional DB calls? Same for example :
private Lesson _activeLesson;
private Lesson _activeLesson;
Any ideas?
3 Replies
phaseshift
phaseshift2y ago
That would be private field on the service, not the entity ? As long as it's within the same request scope, if you already have the information outside the db, then sure, use that
antimatter8189
antimatter8189OP2y ago
yeah Just sounds odd thats the proper way idk
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?