Say that I have a web application that can store Faces in a database. Each person must have a unique email address (or username or something else). If a user tries to add a Person with an existing email address, the form should be returned with an error message (for example, with a typical verification failure).
How does such a typical error most often bubble from the service level to the controller, and then to the presentation? Should a service method throw an exception for a controller to catch or return a value or some kind of result object?
If you ultimately want to use your service level to create a web service, will that change, how can I continue?
Any suggestions or links to best practices / sample applications would be appreciated.
Basically, there are two ways to do this: exceptions and returning the results of checking business rules. Each method has its advantages and disadvantages, but mainly:
Exceptions:
Validation of business rules:
, , , . -, .
- addPerson, . :
addPerson
class AddPersonResponse { private Person person; private AddPersonStatus status; private AddPersonFailureReason failureReason; } enum AddPersonStatus { SUCCESS, FAILURE } enum AddPersonFailureReason { DUPLICATE_EMAIL_ADDRESS, DUPLICATE_USER_NAME }
, , , , .
, . , Exception. , "" , "innerException". , , .
, . , - , API , API . . , - .
-, , . Django, RoR, JBoss Seam .. . , , . (, NoSuchObject) (404 Not Found), . , , , . Django - , Seam - , .
- , , YAGNI DRY, .
Layering , , , , , .
, " ".
Save() ( ). ControllerBase ( ) , .
ControllerBase MVC AddModelError. , "isValid" JSON.
, Spring MVC, - Validator. , , . , Errors . Person , , .
Source: https://habr.com/ru/post/1708459/More articles:Sort a DataGridView with zeros in a DateTime column - .netIs it possible to create JPA entity classes from a database schema at run time? - javaRoleManagerModule and RolePrincipal Object - authenticationWhy does my VB6 variant change depending on when it is assigned? - typesIQueryable where extension method with or - c #"Object" is called a class in Ruby - ruby | fooobar.comFlex: calling an actioncript function from htmlText anchor - flexOdd / incorrect behavior of sem_getvalue semaphore in OS X - c ++Custom event logging for Javascript frameworks - javascript301 URL redirect based on GET variables in .htaccess - query-stringAll Articles