Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
The update of an entity in the database uses CrudServices's
In web applications this has a typical two-stage update consisting of
ReadSingle<EntityOrDto>(key)to show the user the current state, some of which will be editable.
UpdateAndSave(Data)to update the entity.
The RazorAppPage application contains multiple examples of the use of the
Updates of DDD-styled entity classes
- Pages/Home/ChangePubDate - updates book's PublishedOn property.
- Pages/Home/AddReview - adds a new review to the book.
- Pages/Home/AddPromotion - adds/updates a price promotion on a book.
- Pages/Home/RemovePromotion - removes a price promotion on a book (See note 4).
Updates of standard-styled entity classes
Pages/Authors/Edit - updates an
Authorentity using the AutoMapper Save mapping.
Potential security issue with standard-styled entity classes
For standard-styled entity classes, or any updates done by AutoMapper it is really important to use the
[ReadOnly(true)] attribute to mark those properties you DON'T want updated in the database (see
Otherwise you have a vulnerability, as the values that you showed but didn't expect to change, like the title, could be changed by someone hacking the HTTP request.
- If the entity class has any methods that return
IStatusGenericthen it will look at these to do the update. Otherwise it tries to use AutoMapper.
- If there are methods in the entity it will try to match the DTOs name, (minus a set of possible DTO endings) with a method. If there is a match it will set this as the default method to use.
- You can state exactly what type/name of method/AutoMapper you want to use, by providing a second parameter to the command, e.g.
_service.UpdateAndSave(Data, "UpdatePublishedOn"). This is useful if there are multiple methods that will match the DTO non-read-only properties. The options for the second parameter are:
- methodName - use a specific named static method, e.g. "AddPromotion"
- methodName(n) - use a specific named static method with n parameters, e.g. "AddPromotion(3)"
- AutoMapper - use AutoMapper's save mapping to copy the DTO into the entity
- For methods with no parameters, e.g.
Bookentity, you must define the method name either by:
- Creating a DTO name that will select the method (see note 2)
- Providing a second parameter of the call, e.g.
- or by providing a PerDtoConfig to the DTO with the
UpdateMethodoverriden with the name.