Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Policy validation for entities implementing interface #4751


User avatar
0
easyest created

Let's say only specific OrganizationUnits are valid for my entities that implement IMustHaveOrganizationUnit and those valid OrganizationUnit Ids are store elsewhere in the database. How and where should I implement that validation?


1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
    • For DTO validation, you can implement ICustomValidate. To ensure that IMustHaveOrganizationUnit implements ICustomValidate, you can inherit it:
    public interface IMustHaveOrganizationUnit : ICustomValidate
    {
        long OrganizationUnitId { get; set; }
    }
    

    You can implement an extension method for convenience:

    public static class IMustHaveOrganizationUnitExtensions
    {
        public static void ValidateOrganizationUnit(this IMustHaveOrganizationUnit self, CustomValidationContext context)
        {
            var iocResolver = context.IocResolver;
            var organizationUnitId = self.OrganizationUnitId;
    
            // ...
        }
    }
    

    And use it like this:

    public class MyEntityDto : IMustHaveOrganizationUnit
    {
        public long OrganizationUnitId { get; set; }
    
        public void AddValidationErrors(CustomValidationContext context)
        {
            this.ValidateOrganizationUnit(context);
        }
    }