Manage easyly your models validations in .NET by customizing different properties rules.
PM> Install-Package Valid4net> dotnet add package Valid4netThe library contains a class named "Valid4netObject" which your project model has to inherit and access to this base class methods, properties, etc.
public class Product : Valid4netObject<Product> { // TODO } public class Product : Valid4netObject<Product> { private string _id; public string Id { get => _id; set { SetProperty(ref _id, value); } } }Note: The function SetProperty(ref _id, value); will notify property changes to apply rules and is needed for each property which needs validations.
... public Product() { Rules.Add(nameof(Id), "Id cannot be empty!!", p => !string.IsNullOrEmpty(p.Id)); } ... ... // Object model var product = new Product(); // Rules definition product.AddRule(nameof(product.Id), "Id cannot be empty!!", p => !string.IsNullOrEmpty(p.Id)); product.AddRule(nameof(product.Title), "Title length must be between 5 and 10!!",p => (p.Title?.Length > 5) && (p.Title?.Length <= 20)); ... ... Product product = new Product(); bool result = product.HasErrors; ... ... // Get whole model errors product.GetErrors(); // Get single property errors product.GetErrors("Title"); ...