Base solution for your next web application
Open Closed

Validation Data Annotations in Application Service Layer #2538


User avatar
0
bilalhaidar created

Hi, I've seen some code samples based on this template. When inside Application Layer, I don't see any code to check for validation or make use of Validation Data Annotation.

In ASP.NET I would do that in the Controllers before proceeding into checking business logic in the Domain Core.

What's the recommendation when using this template? Where shall I run those validation data annotations especially that I need to notify the client of such validitions.

Thank you, Regards


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    This document can be very helpful for you <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#DocCustomValidation">http://aspnetboilerplate.com/Pages/Docu ... Validation</a>.

  • User Avatar
    0
    bilalhaidar created

    Oh that's awesome. So, ASPNET Zero will automatically run my validations at the Application Service level.

    I have 2 related questions in here:

    1- If I want to return the validation errors to client side, how is that possible given that the template would through exception if inputs are invalid (something similar to ModelState.Errors, etc.)

    2- In some requirements I have, I need to validate based on "values" not fields. What I mean is that I have some reference data (ID, Name) to fill some Select elements. Based on a specific value, I will validate other fields. Is this a good pattern to follow or is there a workaround? That reference data could change also (Admin can edit). Is that supported or is that okay to do? I smell something wrong, as data might change and hence my validations might fail!

    many thanks,

    regards Bilal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    For the first question, you can throw UserFriendlyException. ABP will show this message to user directly. I couldn't understand your second question. Can you explaint it with a real life scenario and real life data ?

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    As far as I understood, the Framework would run the validation (data annotations) before calling my AppService method correct? Then, if that is true, how can I send back a message to client? Will the framework give me a way to know that the input didn't meet the validation rules?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In that case your app service method will not be called at all. If you want to send specific message to user by validation your inputs via attributes, you can use ErrorMessage property but it is not localizable.

    It is better to validate domain logic in your domain service I think and throw user friendly exceptions to user.