'How to handle server validation/errors?
I'm dipping my toes into Blazor Server and I'm really unsure about how I should handle messages from the server back to the client in the event of an error or some server side validation. My brain instantly thinks "use a viewmodel", but is this the right answer?
I have this model:
public class Season
{
public int SeasonId { get; set; }
[Required]
public DateTime StartDate { get; set; } = DateTime.Today;
[Required]
[Display(Name = "Season Description")]
public string SeasonDescription { get; set; } = string.Empty;
[Required]
public bool Publish { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public virtual List<Game> Games { get; set; }
}
I have a Season repository:
public interface ISeasonRepository
{
public Task<List<Season>> GetSeasons();
public Task<Season> GetSeason(int SeasonId);
public Task<Season> UpdateSeason(Season season);
public Task<Season> AddSeason(Season season);
// TODO: Implement Delete
}
And, I have this service that is injected to my page:
public class SeasonsService
{
private readonly ISeasonRepository _seasonRepository;
public SeasonsService(ISeasonRepository seasonRepository)
{
_seasonRepository = seasonRepository;
}
public async Task<List<Season>> GetSeasons()
{
return await _seasonRepository.GetSeasons();
}
public async Task<Season> GetSeasonById(int SeasonId)
{
return await _seasonRepository.GetSeason(SeasonId);
}
public async Task<Season> UpdateSeason(Season season)
{
return await _seasonRepository.UpdateSeason(season);
}
public async Task<Season> AddSeason(Season season)
{
return await _seasonRepository.AddSeason(season);
}
}
Previously using MVC, I would be creating a viewmodel and probably passing a message to it if we had any server logic that indicated if our model failed any server side validation/logic or if we had an error.
How is it best to handle this with Blazor? Should I still be using viewmodels? I've read about ErrorBoundary but it feels like they're for more critical problems that might cause SignalR state issue.
I can't get my head away from viewmodels but hardly any examples I read use such flow. How can I best notify my users the logic failed or there is something such as a SQL concurrency issue?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
