locked
Constraints on the Database RRS feed

  • Question

  • User-305496339 posted

    Hi friends,

    I was wondering what is the best practice if you have constraints on your tables. Do you put data annotations on the model ? Is this coded in the business rules? Or do you let the user enter data and then allow the database to return an error and you handle it? I am using Database first, but would also be interested in how this works using the code first approach as well. Thanks !!!

    Friday, March 8, 2019 6:56 AM

Answers

  • User475983607 posted

    rkrex

    I was wondering what is the best practice if you have constraints on your tables. Do you put data annotations on the model ? Is this coded in the business rules? Or do you let the user enter data and then allow the database to return an error and you handle it? I am using Database first, but would also be interested in how this works using the code first approach as well. Thanks !!!

    This is a design question that indicates the application requirements are not well defined,  Usually you think about this kind of stuff ahead of time and come up with a design that fits into your application.

    I built multi-tier applications that have HTTP services. I try my best to make the UI as easy as possible for the user by catching validation errors as close to the UI as possible.  This is just good UI design.  It does not protect HTTP services though.  Standard coding practices is to validate inputs at every service layer.  So I add those bit of logic too.  Then behind the HTTP service you have business rules that produces message and must be returned to the user.  Finally there are validation error thrown by the DB.  Handling all of these messages is generally part of the design that you come up with.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 12:37 PM
  • User475983607 posted

    But I was wondering why would we put a constraint on a table if validation is going to be handled at the UI level in the first place

    User experience.

     

    I am not trying to be confusing or convolute the discussion. I  was just wondering if a database or table should be perceived differently from an applications developers perspective than in an all purpose database when using sql to make bulk inserts and or updates when I could see where the use of constraints would be pertinent.

    It's up to you and your application requirements.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 8:51 PM

All replies

  • User475983607 posted

    rkrex

    I was wondering what is the best practice if you have constraints on your tables. Do you put data annotations on the model ? Is this coded in the business rules? Or do you let the user enter data and then allow the database to return an error and you handle it? I am using Database first, but would also be interested in how this works using the code first approach as well. Thanks !!!

    This is a design question that indicates the application requirements are not well defined,  Usually you think about this kind of stuff ahead of time and come up with a design that fits into your application.

    I built multi-tier applications that have HTTP services. I try my best to make the UI as easy as possible for the user by catching validation errors as close to the UI as possible.  This is just good UI design.  It does not protect HTTP services though.  Standard coding practices is to validate inputs at every service layer.  So I add those bit of logic too.  Then behind the HTTP service you have business rules that produces message and must be returned to the user.  Finally there are validation error thrown by the DB.  Handling all of these messages is generally part of the design that you come up with.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 12:37 PM
  • User-305496339 posted

    Thanks for your help. But I was wondering why would we put a constraint on a table if validation is going to be handled at the UI level in the first place. Wouldn't that negate the need for constraints? I am not trying to be confusing or convolute the discussion. I  was just wondering if a database or table should be perceived differently from an applications developers perspective than in an all purpose database when using sql to make bulk inserts and or updates when I could see where the use of constraints would be pertinent.

    Friday, March 8, 2019 8:21 PM
  • User475983607 posted

    But I was wondering why would we put a constraint on a table if validation is going to be handled at the UI level in the first place

    User experience.

     

    I am not trying to be confusing or convolute the discussion. I  was just wondering if a database or table should be perceived differently from an applications developers perspective than in an all purpose database when using sql to make bulk inserts and or updates when I could see where the use of constraints would be pertinent.

    It's up to you and your application requirements.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 8:51 PM