-
Notifications
You must be signed in to change notification settings - Fork 42
UpdateService
To update some data you need to do two things.
- First read the existing data to show to the user.
UpdateSetupService
provides that. - Update the data in the database from the edited data sent back.
UpdateService
does that.
For direct access to database class then these are very simple: the data back from the user replaces all the data in the original data item. For cases which use a DTO this can be more complicated because of two possible reasons:
- You might not have included all the properties in the DTO, so you want to leave the database properties missing from the DTO as they were.
- The DTO may have been 'fattened', i.e. refer to sub-classes to the main class. This means we need to update both the main class and its subclasses.
GenericServices handles direct and DTOs options appropriately, but does need a bit of help in DTO case 2. Read on for the full details.
UpdateSetupService reads the data of an existing item in the database using its primary key(s). You specify what type of class you want returned by providing the type T. If T is the data class then it simply returns the data found in the database.
If the item is a DTO then, like CreateSetupService, the data is copied from the
linked data class using convention-based object-object mapping(see AutoMapper)
and the method SetupSecondaryData
is optionally called. This allows the developer to
overide SetupSecondaryData
and calculate any other properties needed in the Dto.
See an example of a DTO based UpdateSetup/Update action in
PostsAsyncController.
The commands are:
-
ISuccessErrors<T> GetOriginal<T>( param object [] keys)
- sync -
Task<ISuccessErrors<T>> GetOriginalAsync<T>( param object [] keys)
- async
Note that the return status (see Introduction to Commands, Feedback)contains any errors caused by missing data or sql security errors. If there is an error then the result will be a new, empty class. This may throw an error somewhere else due to not filled in items, so checking the status is best.
This updates a data item in the database using the data handed to it. The data, which can be a data class or DTO which inherits from EfGenericDto/EfGenericDtoAsync, must have the primary key(s) of the item to update correctly set.
In the case of a data item it simply replaces all the data in the original data item.
In the case of a DTO then the method loads the original data item and then it copies over the
properties in the DTO that do not have the [DoNotCopyBackToDatabase]
attribute into the original data. (See note on DTO-to-data and data-to-DTO copying for more info).
The commands are:
-
ISuccessOrErrors Update( updatedData)
- sync -
Task<ISuccessOrErrors> UpdateAsync( updatedDto)
- async TDto ResetDto( dto)
TDto ResetDtoAsync( dto)
Note: if using a DTO with flattening (case 2 at the start) then you need to override the method FindItemTrackedForUpdate
to load both the main class and any related classes otherwise the update will fail. See ??? for an example of this.
Live example web sites!
Introduction/Overview
Commands and options
- Introduction to Commands
- Key interfaces
- Calculated properties
- DoNotCopyBackToDatabase attribute
- Configuration Options
Data Transfer Objects (DTOs)