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

Custom Validation for Entities/DTO with Database Access #2583


User avatar
0
bilalhaidar created

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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    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.

  • User Avatar
    0
    bilalhaidar created

    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

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.

  • User Avatar
    0
    bilalhaidar created

    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

  • User Avatar
    0
    ismcagdas created
    Support Team

    Yes, exactly. Because your validation logic is dynamic which is changeable by users. This solution seems good to me.

  • User Avatar
    0
    bilalhaidar created

    Thanks a lot :)

  • User Avatar
    0
    fawad29 created

    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

  • User Avatar
    0
    bilalhaidar created

    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

  • User Avatar
    0
    fawad29 created

    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

  • User Avatar
    0
    bilalhaidar created

    Here you go: [https://github.com/aspnetboilerplate/eventcloud/blob/master/src/EventCloud.Core/Events/EventRegistrationPolicy.cs])

    Enjoy it.

    Regards Bilal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @drcgreece :).

  • User Avatar
    0
    bilalhaidar created

    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

  • User Avatar
    0
    fawad29 created

    Thank you so much, I really appreciate your help and yes you are right with regards to helping each other out.