We can now create dedicated types to represent our requests, but we should also consider adding validation. Validation is important if we wish to prevent messy data and possible exceptions in our web service. ASP.NET Core provides an out of the box way for us to implement validation in our controllers and services: System.ComponentModel.DataAnnotations. The namespace provides a set of attributes that can be used to describe the validation of model fields. For example, consider the following code snippet:
using System.Collections.Generic;
namespace SampleAPI.Requests
{
public class OrderRequest
{
public IEnumerable<string> ItemsIds { get; set; }
public string Currency { get; set; }
}
}
In this case, the [Required] attribute specifies that both the ItemsIds and Currency attributes should not be null or empty; otherwise...