2033
Member
- Joined
- May 11, 2025
- Messages
- 17
- Programming Experience
- 1-3
@Skydiver, did it the other way; now it's better. Thanks.
I wanted to solve one problem a long time ago, but I lack experience in programming, so I'll ask here.
In my home controller, I have two methods. As you can see, they share some data between them. (_set and _currentWord - static fields). Did it in order to not ask DB to bring data all the time. But I heard that having static fields is not a good idea in an ASP app. Maybe you have something in mind with your experience on how I can avoid them and not bother DB every time.
Because the SwitchWord method is called pretty often, it's not an option to get set from DB every time.
StudySelectedSet, on the other hand, is called to render an entire view, so it's not a big deal to fetch some data in this method.
I wanted to solve one problem a long time ago, but I lack experience in programming, so I'll ask here.
In my home controller, I have two methods. As you can see, they share some data between them. (_set and _currentWord - static fields). Did it in order to not ask DB to bring data all the time. But I heard that having static fields is not a good idea in an ASP app. Maybe you have something in mind with your experience on how I can avoid them and not bother DB every time.
Because the SwitchWord method is called pretty often, it's not an option to get set from DB every time.
StudySelectedSet, on the other hand, is called to render an entire view, so it's not a big deal to fetch some data in this method.
C#:
[HttpGet]
public async Task<IActionResult> StudySelectedSet(int id)
{
if (id is 0)
return BadRequest();
_set = mapper.Map<SetViewModel>(await dataManager.SetRepository.GetAsync(id));
if (_set is null)
return NotFound();
_currentWord.Count = _set.Words.Count;
_currentWord.CurrentWord = _set.Words[0];
return View(_currentWord);
}
[HttpGet]
public IActionResult SwitchWord(int index = 0)
{
if (index < 0 || index >= _set.Words.Count)
{
return NotFound();
}
return Json(_set.Words[index]);
}