Hi,
I need to add custom validation to DTOs and Entities. This validation cannot be satisfied by the usual Data Annotations.
Is it okay to access DB when inside DTO or Entity to get some data from the database and then validate the object?
Is there a better way of doing it, the boilerplate way?
Thanks
13 Answer(s)
-
0
Hi,
This might be an example of that <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/dev/src/MyCompanyName.AbpZeroTemplate.Core/Chat/ChatFeatureChecker.cs">https://github.com/aspnetzero/aspnet-ze ... Checker.cs</a>. But this is a simple class which gets inputs and throws an exception if validation fails.
-
0
I can still inject an IXXXPolicy in the ApplicationService, and then I create run a method for example IXXXPolicy.Validate().
Inside that method I can contact DB to get for instance, if a field is required (this info should be stored in DB as client wants to dynamically change hide/show and required/optional on fields so I cannot use Data Annotations in this case). So this function would grab that info and accordingly validate. In case of something missing and it is required I can through AbpValidationException.
On the Domain Service level, still I can do other types of validations (business related ones). Then in this case I an through UserFriendlyExceptions.
Does this make sense?
Thanks
-
0
Hi,
Thank you so much for your reply.
Yes, it does help. I am new to boiler plate, it is very good frame work. I am reading about IPolicy. If you have a small example code with may be a couple of fields that you would like to share then that would be very much appreciated as it would speed up my learning curve.
Regards