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)
-
0
- 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); } }
- For entity validation, override ApplyAbpConcepts(EntityEntry entry, ...) in your DbContext.