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,
DTOs should be as simple as possible. It is not good to access DB in a DTO. You can make a class for this custom validation and call it in your app service or domain service method.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
OK, so a normal class that contains my validation rules (read from DB), if the validation fails, I throw an exception, any such sample in your samples online?
Thanks
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
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.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
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
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Yes, exactly. Because your validation logic is dynamic which is changeable by users. This solution seems good to me.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Thanks a lot :)
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Hi Bilal,
I am in a similar boat as well. Did you manage to implement this logic? Would it be possible for you to share some example code?
Thanks
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Hello, Well, the idea is simple. In your DomainService, you connect to the DB, get whatever data you want and use them inside an implementation of IPolicy.
Does that help?
Regards Bilal
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
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
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Here you go: [https://github.com/aspnetboilerplate/eventcloud/blob/master/src/EventCloud.Core/Events/EventRegistrationPolicy.cs])
Enjoy it.
Regards Bilal
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Thanks @drcgreece :).
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
I know how it feels when one is stuck and needs some help! We should all of us start helping each other more here on the forums.
Thanks Ismail for your efforts always.
Bilal
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Thank you so much, I really appreciate your help and yes you are right with regards to helping each other out.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)